Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 尝试插入/更新具有多个值的两个不同表_Php_Mysql_Foreach_Isset_Multiple Tables - Fatal编程技术网

Php 尝试插入/更新具有多个值的两个不同表

Php 尝试插入/更新具有多个值的两个不同表,php,mysql,foreach,isset,multiple-tables,Php,Mysql,Foreach,Isset,Multiple Tables,我试图用PHP代码更新mySQL数据库上的两个不同表。一个块工作得很好,但是从“if(isset…”我得到了“Error querying database”消息,因此很明显我试图添加数组值(复选框)的代码不起作用。我没有看到什么 EDIT.PHP <h3>Edit Profile: <?php echo $_REQUEST['first_name'];?></h3> <form enctype="multipart/form-data" m

我试图用PHP代码更新mySQL数据库上的两个不同表。一个块工作得很好,但是从“if(isset…”我得到了“Error querying database”消息,因此很明显我试图添加数组值(复选框)的代码不起作用。我没有看到什么

EDIT.PHP

   <h3>Edit Profile: <?php echo $_REQUEST['first_name'];?></h3>
   <form enctype="multipart/form-data" method="POST" action="change.php"> 
   <table border="0" width="60%">

   <tr><td width="30%">First Name: </td><td><input type="text"
   name="upd_first_name" value="<?php echo $_REQUEST['first_name'];?>" maxlength="20"> </td></tr>

   <tr><td width="30%">Last Name: </td><td><input type="text"
   name="upd_last_name" value="<?php echo $_REQUEST['last_name'];?>" maxlength="20"> </td></tr>

   <tr><td width="30%">Email: </td><td><input type="text"
   name="upd_email" value="<?php echo $_REQUEST['email'];?>" maxlength="45"> </td></tr>

   <tr><td width="30%">Password: </td><td><input type="password"
   name="upd_password" id="upd_password" value="<?php echo $_REQUEST['password'];?>" maxlength="20"> </td></tr>

   <tr><td width="30%">Confirm Password: </td><td><input type="password"
   name="upd_cpassword" id="upd_cpassword" value="<?php echo $_REQUEST['password'];?>" maxlength="20" onkeyup="checkPass(); return false;"> </td></tr>

   <tr><td width="30%">Profile Visbility: </td><td><input type="radio"    name="upd_profilevis" value="1" id="1" checked> Private <input type="radio"   name="upd_profilevis" value="2" id="2" > Public </td></tr>

  <<tr><td width="30%">Industries: </td><td>
  <input type="checkbox" name="industries[]" value="1"/>None</br>
  <input type="checkbox" name="industries[]" value="2"/>Film</br>
  <input type="checkbox" name="industries[]" value="3"/>Television</br>
  <input type="checkbox" name="industries[]" value="4"/>Music</br>
  <input type="checkbox" name="industries[]" value="5"/>Gaming</br>
  <input type="checkbox" name="industries[]" value="6"/>Books</br>
  <input type="checkbox" name="industries[]" value="7"/>Comic Books</br>
  </td></tr>

  <tr><td width="30%">Link: </td><td><input type="text"
  name="upd_link" value="<?php echo $_REQUEST['profile_link'];?>" maxlength="45"> </td></tr>

  <tr><td width="30%">Bio: </td><td><input type="text"
  name="upd_bio" value="<?php echo $_REQUEST['bio'];?>" maxlength="500"> </td></tr>

  <input type="hidden" name="MAX_FILE_SIZE" value="10000000">
  <tr><td width="30%">Picture: </td><td><input type="file" id="image" name="image"></tr>
  </table>
  <span id="confirmMessage" class="confirmMessage"></span><br /> 

  <input type="submit" value="Save & Update"/>
  <input type="hidden" name="id" value="<?php echo $_REQUEST['id'];?>">
  </form>
编辑配置文件:

名字:您需要在事务中包装所有操作

if (isset($_POST['industries'])) {
    $industry_ids = $_POST['industries'];
    $id = $_POST['id'];

    try {
        $dbc->begin_transaction();
        $dbc->query("delete from industry_has_member where member_idmember=$id") or throw new Exception($dbc->error);

        foreach ($industry_ids as $industry_id) {
            $dbc->query("insert ignore into industry_has_member (industry_idindustry, member_idmember) values ($industry_id, $id)" or throw new Exception($dbc->error);
        }
        $dbc->commit();
    } catch (Exception $e) {
        echo $e->getMessage();
        $dbc->rollback();
    }
}

试着扩展你的插入问题

on duplicate key update

请参阅:

您能否将
die('Error querying database')
替换为
die(mysqli\u Error($dbc))
显示mysql错误消息?因此,我用您建议的语句替换了这两个die语句,它可以工作…但仅适用于新条目。如果数据库中已有数据,则该数据无法工作,并显示此信息…键“PRIMARY”的重复条目“2-6”,因此我的问题是使用新的更新更新现有条目…您建议我向代码中添加什么以确保这两种情况都有效?您的代码中还有两个问题:SQL注入(在“…值(“$industries”,“$id”)…”)和跨站点脚本(在“…值”中)="请看一下我在下面最后一篇文章中的评论,让我知道你的想法。我想这篇文章回答了我的问题:如果你想覆盖,请使用上面的内容。如果你不想覆盖,请使用user3678068的答案。我真正想做的是更新。例如,如果一个用户的mysql数据库中已经有industry 2-3-4,那么当用户更新时它与他选择1-4-6的形式相反,我只希望1-4-6与此用户一起显示在db中。现在,使用REPLACE-INTO或UPDATE-ON-DUPLICATE语句,db将显示1-2-3-4-6与此用户ID。我认为我需要以不同的方式处理此项。在选中项目和取消选中项目时,我需要创建不同的名称所以我创建了逻辑来删除未检查的项并添加检查的项。你们认为呢?在查看了我的代码和逻辑之后,我终于找到了我的逻辑和代码的答案并对其进行了更新(change.php)它很有效!!!谢谢你们的帮助,它帮助我解决了这个问题。请看我在下面最后一篇文章中的评论,让我知道你的想法。@EmmanuelHenri,是的,现在它有意义了。我已经更新了答案。我假设这个函数属于if(isset)和foreach()函数?谢谢。您能快速查看我上面更新的代码(change.php)并告诉我这是否正确吗?@EmmanuelHenri,很抱歉我误解了您的数据结构。请参阅更新的答案
on duplicate key update