在单个查询php mysql中更新多行

时间:2018-05-21 09:37:48

标签: php mysql

我正在尝试在单个查询中更新多行。我的代码中的数据没有更新。我想加入这两张桌子。当用户输入号码时将显示来自2个表的数据,这些数据通过外键连接。来自table1的数据得到更新。表2中的列未更新的位置。我需要根据唯一ID

更新第二个表
    if($_REQUEST["profile"] == "profile") 
{
    $Id = $_REQUEST["id"];
    $firstname  = mysql_real_escape_string($_REQUEST["firstname"]);
    $serial  = mysql_real_escape_string($_REQUEST["serial"]);
    $dom  = mysql_real_escape_string($_REQUEST["dom"]);
    $idno = $_REQUEST["idno"];
    $pow = mysql_real_escape_string(stripslashes($_REQUEST["pow"]));
    $address = mysql_real_escape_string(stripslashes($_REQUEST["address"]));
    $bookno = mysql_real_escape_string(stripslashes($_REQUEST["bookno"]));
    $zone = mysql_real_escape_string(stripslashes($_REQUEST["zone"]));
    $mobile = mysql_real_escape_string(stripslashes($_REQUEST["phone"]));
    $phone = mysql_real_escape_string(stripslashes($_REQUEST["mobile"]));
    $mothertongue=mysql_real_escape_string(stripslashes($_REQUEST["mothertongue"]));
    $nof=mysql_real_escape_string(stripslashes($_REQUEST["nof"]));
    $email=mysql_real_escape_string(stripslashes($_REQUEST["email"]));
    $nom=$_REQUEST["nom"];
    $nofemale=$_REQUEST["nofemale"];
mysql_query("UPDATE profile SET  firstname='".$firstname."',serial='".$serial."',dom='".$dom."',idno='".$idno."',pow='".$pow."',address='".$address."',bookno='".$bookno."',
zone='".$zone."',phone='".$mobile."',mobile='".$phone."',mothertongue='".$mothertongue."',email='".$email."',nof='".$nof."',nom='".$nom."',nofemale='".$nofemale."' WHERE id = '".$_POST['id']."' " ) or die(mysql_error());

    for($i=0;$i<count($_REQUEST['slno1']);$i++)
    {
        $mid=$_REQUEST['mid'][$i];
        $slno1 = mysql_real_escape_string(stripslashes($_REQUEST["slno1"][$i]));
    $name1 =  mysql_real_escape_string(stripslashes($_REQUEST["name1"][$i]));
    $rhof1 =  mysql_real_escape_string(stripslashes($_REQUEST["rhof1"][$i]));
    $dob1 =  mysql_real_escape_string(stripslashes($_REQUEST["dob1"][$i]));
    $dobapt1 =  mysql_real_escape_string(stripslashes($_REQUEST["dobapt1"][$i]));
    $doc1 =  mysql_real_escape_string(stripslashes($_REQUEST["doc1"][$i]));
    $doconf1 =  mysql_real_escape_string(stripslashes($_REQUEST["doconf1"][$i]));
    $qualification1 =  mysql_real_escape_string(stripslashes($_REQUEST["qualification1"][$i]));
    $school1 =  mysql_real_escape_string(stripslashes($_REQUEST["school1"][$i]));
    $occupation1 = mysql_real_escape_string(stripslashes($_REQUEST["occupation1"][$i]));



$run=mysql_query("UPDATE member SET
slno1='".$slno1."',name1='".$name1."',rhof1='".$rhof1."',dob1='".$dob1."',dobapt1='".$dobapt1."',doc1='".$doc1."',doconf1='".$doconf1."',qualification1='".$qualification1."' WHERE mid = '".$mid."' " ) or die(mysql_error()); 

}



}

3 个答案:

答案 0 :(得分:2)

请使用PDO,这样您就不必转义字符串,因此您的代码更易于阅读。您的查询使用了过多的引号,仅此一项就可以轻松实现失败。请使用以下示例,这可以帮助您取得成功。

基本PDO更新: https://www.w3schools.com/php/php_mysql_update.asp

绑定参数: https://www.w3schools.com/php/php_mysql_prepared_statements.asp

答案 1 :(得分:1)

在您的查询中,您使用$ _POST [&#39; mid&#39;]而不是您应该使用$ mid,您已经在阅读

$mid=$_REQUEST['mid'][$i];

答案 2 :(得分:1)

根据我的理解,UPDATE查询用于在使用where条件时更新有限数量的记录。因此,我能想到的唯一方法是使用带有ON DUPLICATE KEY UPDATE子句的INSERT查询。请尝试以下代码:

for($i=0;$i<count($_REQUEST['mid']);$i++) {
    $mid[] = $_REQUEST['mid'][$i];
    $slno1[] = mysql_real_escape_string(stripslashes($_REQUEST["slno1"][$i]));
    $name1[] =  mysql_real_escape_string(stripslashes($_REQUEST["name1"][$i]));
    $rhof1[] =  mysql_real_escape_string(stripslashes($_REQUEST["rhof1"][$i]));
    $dob1[] =  mysql_real_escape_string(stripslashes($_REQUEST["dob1"][$i]));
    $dobapt1[] =  mysql_real_escape_string(stripslashes($_REQUEST["dobapt1"][$i]));
    $doc1[] =  mysql_real_escape_string(stripslashes($_REQUEST["doc1"][$i]));
    $doconf1[] =  mysql_real_escape_string(stripslashes($_REQUEST["doconf1"][$i]));
    $qualification1[] =  mysql_real_escape_string(stripslashes($_REQUEST["qualification1"][$i]));
    $school1[] =  mysql_real_escape_string(stripslashes($_REQUEST["school1"][$i]));
    $occupation1[] = mysql_real_escape_string(stripslashes($_REQUEST["occupation1"][$i]));
}

$query = "INSERT INTO `member` (`mid`,`slno1`,`name1`,`rhof1`,`dob1`,`dobapt1`,`doc1`,`doconf1`,`qualification1`) VALUES ";

for ($i = 0; $i < count($mid); $i++) {

    $query .= "('".$mid[$i]."','".$slno1[$i]."','".$name1[$i]."','".$rhof1[$i]."','".$dob1[$i]."','".$dobapt1[$i]."','".$doc1[$i]."','".$doconf1[$i]."','".$qualification1[$i]."')";

    if ($i != (count($mid) - 1)) {
        $query .= ',';
    }
}

$query .= ' ON DUPLICATE KEY UPDATE `slno1` = VALUES(`slno1`), `name1` = VALUES(`name1`), `rhof1` = VALUES(`rhof1`), `dob1` = VALUES(`dob1`), `dobapt1` = VALUES(`dobapt1`), `doc1` = VALUES(`doc1`), `doconf1` = VALUES(`doconf1`), `qualification1` = VALUES(`qualification1`);';

$run=mysql_query($query) or die(mysql_error());

希望这有助于。