如何使用multi_query插入和删除两个表的记录

How to use multi_query for insert and delete records of two tables

我想将两条不同的记录插入到两个不同的表中并删除原来的一个。像这样

$msgid = POST_['roll_id'];
$query  = "INSERT INTO del_subscription SELECT * from subscription WHERE mem_id='$msgid'";
$query1 = "INSERT INTO del_user_data SELECT * from user_data WHERE  mem1_id='$msgid'";
$query2 ="DELETE FROM subscription WHERE mem_id='$msgid'";
$query3 ="DELETE FROM user_data WHERE mem_id='$msgid'";

此查询可以使用 multi_query。

我使用了多查询,但第一个查询只有效

$msgid = POST_['roll_id'];

if (strlen($msgid) > 0)
{
    $query   = "INSERT INTO del_subscription SELECT * FROM subscription WHERE name='$msgid' AND renewal='yes';" ;
    $query  .= "INSERT INTO del_user_data SELECT * FROM user_data WHERE name='$msgid'AND wait='no';";
    $query  .= "DELETE FROM subscription WHERE name='$msgid' AND renewal='yes';" ;
    $query  .= "DELETE FROM user_data WHERE name='$msgid' AND wait='no'";

    if (mysqli_multi_query($con, $query)) 
    {
        do {
            /* store first result set */
            if ($result = mysqli_store_result($con)) {
                 while ($row = mysqli_fetch_row($result)) {
                     echo "null";
                 }

                 if($result) { mysqli_free_result($result); }
           }

           /* print divider */
           if (mysqli_more_results($con)) {
              echo "<html><head><script>alert('Member Deleted');</script></head></html>";
              echo "<meta http-equiv='refresh' content='0; url=view_mem.php'>"; 
           }
       } while (mysqli_next_result($con));
   }
}   
else {
   echo "<html><head><script>alert('ERROR! Delete Operation Unsuccessful');</script></head></html>";
   echo "<meta http-equiv='refresh' content='0; url=view_mem.php'>";
}

/* close connection */
mysqli_close($con);

这里我们要运行 Member Deleted 一次而不是四次直到完成查询。

我完全重写了这个答案。

由于很难将我写的和你写的拼接起来,我会完整地使用这段代码,或者根本不使用。

free_resultfetch_row 命令不应该存在。它们将导致错误,因为 INSERTDELETE 查询没有结果集。

$msgid = $_POST['roll_id'];
if(strlen($msgid) > 0):

    $queries = array();
    $queries[] = "INSERT INTO del_subscription SELECT * FROM subscription WHERE `name`='".$msgid."' AND `renewal`='yes';";
    $queries[] = "INSERT INTO del_user_data SELECT * FROM user_data WHERE `name`='".$msgid."' AND `wait`='no';";
    $queries[] = "DELETE FROM subscription WHERE `name`='".$msgid."' AND `renewal`='yes';";
    $queries[] = "DELETE FROM user_data WHERE `name`='".$msgid."' AND `wait`='no';";


    //  Set flag TRUE unless we find otherwise
    $ALL_SUCCESSFUL = TRUE;


    //  Executes query and enters if the first query was successful.
    if ( mysqli_multi_query($con, implode(' ',$queries)) ):


        //  This do-while tests if all other queries were successful

        do

            if( $result = mysqli_store_result($con) ):
                if(!$result):
                    $ALL_SUCCESSFUL = FALSE ;
                endif;
            endif;

        //  Changes internal pointer to next result.
        while( mysqli_next_result($con) );


    endif;  //  If first query was successful


    if( $ALL_SUCCESSFUL ):
        echo showMessage('Member Deleted!');
    else:
        echo showMessage('ERROR! Delete Operation Unsuccessful');
    endif;


else:

    echo showMessage('ERROR! No roll_id given');

endif;  //  If roll_id is valid

/* close connection */
mysqli_close($con);

function showMessage($msg){
    $html = '';
    $html.= "<html><head>";
    $html.= "<meta http-equiv='refresh' content='0' url='view_mem.php' />";
    $html.= "<script>alert('".$msg."');</script>";
    $html.= "</head></html>";
    return $html;
}

如果您发现第一个查询执行但没有像您之前所说的任何其他查询,那不是因为 php 代码。这是因为您编写的查询。在 mysqli_multi_query() 中,所有查询都在同一个调用中执行,因此如果执行了一个,则它们都被执行了......即使不是所有结果都成功。

基本上这意味着 multi_query 之后的任何 PHP 代码根本不会影响其他 SQL 查询的结果。我们基本上可以扔掉所有东西,只写这个让它工作:

$msgid = $_POST['roll_id'];

$queries = array();
$queries[] = "INSERT INTO del_subscription SELECT * FROM subscription WHERE `name`='".$msgid."' AND `renewal`='yes';";
$queries[] = "INSERT INTO del_user_data SELECT * FROM user_data WHERE `name`='".$msgid."' AND `wait`='no';";
$queries[] = "DELETE FROM subscription WHERE `name`='".$msgid."' AND `renewal`='yes';";
$queries[] = "DELETE FROM user_data WHERE `name`='".$msgid."' AND `wait`='no';";

mysqli_multi_query($con, implode(' ',$queries));

mysqli_close($con);

另外,对于 tabletable 直接 INSERT 查询没有定义列就像你写的那样使用 SELECT * 时,仔细检查你的数据库以确保两个 table 具有相同的列结构并且列字段匹配。但是,这不太可能中断查询。

这是数组的工作代码

if(isset($_POST['submit']))
{
 $msgid = $_POST['roll_id'];

 if(strlen($msgid) > 0):

$queries = array();
$queries[] = "INSERT INTO del_subscription SELECT * FROM subscription WHERE `mem_id`='".$msgid."' AND `renewal`='yes';";
$queries[] = "INSERT INTO del_user_data SELECT * FROM user_data WHERE `newid`='".$msgid."' AND `wait`='no';";
$queries[] = "DELETE FROM subscription WHERE `mem_id`='".$msgid."' AND `renewal`='yes';";
$queries[] = "DELETE FROM user_data WHERE `newid`='".$msgid."' AND `wait`='no';";


//  Set flag TRUE unless we find otherwise
$ALL_SUCCESSFUL = TRUE;


//  Executes query and enters if the first query was successful.
if ( mysqli_multi_query($con, implode(' ',$queries)) ):


    //  This do-while tests if all other queries were successful

    do

        if( $result = mysqli_store_result($con) ):
            if(!$result):
                $ALL_SUCCESSFUL = FALSE ;
            endif;
        endif;

    //  Changes internal pointer to next result.
    while( mysqli_next_result($con) );


 endif;  //  If first query was successful

 if( $ALL_SUCCESSFUL ):
       echo "<html><head><script>alert('Member Deleted');</script></head></html>";
       echo "<meta http-equiv='refresh' content='0; url=view_mem_del.php'>"; 
 else:

       echo "<html><head><script>alert('ERROR! Delete Operation Unsuccessful');</script></head></html>";
       echo "<meta http-equiv='refresh' content='0; url=view_mem.php'>"; 
 endif;

 else:

     echo showMessage('ERROR! No roll_id given');

  endif;  //  If roll_id is valid

 /* close connection */
mysqli_close($con);

}

给出了我们预期的准确结果

我有太多缺陷无法提供逐项解决方案,所以我会提供重写,假设您的查询有效(而且我相当确定它们需要一些工作):

if(isset($_POST['submit'])){
    $msgid=$_POST['roll_id'];
    if(strlen($msgid)>0){
        $esc_msqid=mysqli_real_escape_string($con,$msgid);
        $queries[] = "INSERT INTO del_subscription SELECT * FROM subscription WHERE `mem_id`='$esc_msqid' AND `renewal`='yes'";
        $queries[] = "INSERT INTO del_user_data SELECT * FROM user_data WHERE `newid`='$esc_msqid' AND `wait`='no'";
        $queries[] = "DELETE FROM subscription WHERE `mem_id`='$esc_msqid' AND `renewal`='yes'";
        $queries[] = "DELETE FROM user_data WHERE `newid`='$esc_msqid' AND `wait`='no'";

        //  Executes query and enters if the first query was successful.
        if(mysqli_multi_query($con,implode(';',$queries))){
            do{
                list($current_key,$current_query)=each($queries);   //advances array pointer to first or next element
                if(mysqli_affected_rows($con)<1){
                    $alert="Query Logic Error @ Query#$current_key with id=$msgid";
                }
            } while(mysqli_more_results($con) && mysqli_next_result($con));
        }else{
            list($current_key,$current_query)=each($queries);   //advances array pointer to first element
        }
        if($error_mess=mysqli_error($con)){
            $alert="Syntax Error @ Query#$current_key with id=$msgid";  // str_replace("'","\'",$error_mess);
        }

        if(!$alert){
            echo "<html><head><script>alert('Member Deleted');</script></head></html>";
            echo "<meta http-equiv='refresh' content='0; url=view_mem_del.php'>";
        }else{
           echo "<html><head><script>alert('ERROR! Delete Operation Unsuccessful\n$alert');</script></head></html>";
           echo "<meta http-equiv='refresh' content='0; url=view_mem.php'>";
        }
    }else{
        showMessage('ERROR! No msgid given');
    }
}else{
     echo showMessage('ERROR! No roll_id given');
}
mysqli_close($con);