0

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

$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一次而不是四次,直到完成查询。

4

3 回答 3

0

我完全重写了这个答案。

由于很难将我写的内容拼接到您写的内容中,因此我会全部使用此代码,或者根本不使用。

free_resultandfetch_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()中,所有查询都在同一个调用中执行,所以如果一个被执行,它们都被执行了……即使不是全部都成功了。

基本上这意味着后面的任何 PHP 代码multi_query都不会影响其他 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);

此外,对于没有定义列的直接INSERT查询SELECT *,请仔细检查您的数据库以确保两个表具有相同的列结构并且列字段匹配。但是,这不太可能破坏查询。

于 2015-03-08T09:29:07.980 回答
0

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

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);
于 2015-04-21T04:54:18.797 回答
-1

这是带有数组的工作代码

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);

}

这给出了我们预期的确切结果

于 2015-03-12T10:15:36.500 回答