-1

我想同时执行两个查询插入和更新查询当我使用更新查询时我的插入查询不起作用但更新查询工作请帮助解决这个问题。

  <?php



    $con=mysqli_connect("localhost","root","","my");
   // Check connection
   if (mysqli_connect_errno())
   {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
     }
  $sql = "INSERT INTO `kyc`( `date`,`agentid`,`formno`, `bank`, `ptype`,
   `cardno`,      `cname`, `fname`, `mname`, `dob`, `phone`, `votarid`,`sex`, 
   `email`, `district`, `thana`)
   VALUES('$date','$agentid',$formno,'$bank','$ptype','$cardno','$cname','$fname',
    '$mname','$dob',$phone,$votarid,'$sex','$email','$district','$thana')";
    $sql="UPDATE card SET status='1'WHERE $cardno=`cardno`";
    $result = mysqli_query($con,$sql);

   echo "One raw Successfully inserted";




    mysqli_close($con);
   }
 ?>
4

4 回答 4

0

您正在尝试连续运行 2 个 SQL 语句:

第一条语句是 INSERT,第二条语句是 UPDATE。

目前您只执行 UPDATE,因为您将 $sql 重置为 UPDATE 语句而没有先运行 INSERT 查询

您应该一个接一个地执行它们:

$sql = "INSERT INTO kyc( date, agentid, formno, bank, ptype, cardno, cname, fname, mname, dob, phone, votarid, sex, email, district, thana)
VALUES('$date','$agentid',$formno,'$bank','$ptype','$ cardno','$cname','$fname','$mname','$dob',$phone,$votarid,'$sex','$email','$district','$thana')" ;

$result = mysqli_query($con,$sql); //检查错误

$sql="UPDATE card SET status='1' WHERE $cardno= cardno";

$result = mysqli_query($con,$sql);

或用分号';'分隔它们

$sql = "INSERT INTO kyc( date, agentid, formno, bank, ptype, cardno, cname, fname, mname, dob, phone, votarid, sex, email, district, thana)
VALUES('$date','$agentid',$formno,'$bank','$ptype','$ cardno','$cname','$fname','$mname','$dob',$phone,$votarid,'$sex','$email','$district','$thana')" ;

$sql .="; UPDATE card SET status='1' WHERE $cardno= cardno";

$result = mysqli_query($con,$sql);

于 2013-10-27T09:28:30.563 回答
0

您可以编写它们并用 ;

   $con=mysqli_connect("localhost","root","","my");
   // Check connection
  if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
 }
 $sql_insert = "INSERT INTO `kyc`( `date`,`agentid`,`formno`, `bank`, `ptype`,
 `cardno`,      `cname`, `fname`, `mname`, `dob`, `phone`, `votarid`,`sex`, 
 `email`, `district`, `thana`)
 VALUES('$date','$agentid',$formno,'$bank','$ptype','$cardno','$cname','$fname',
'$mname','$dob',$phone,$votarid,'$sex','$email','$district','$thana')";

$sql_update="UPDATE card SET status='1'WHERE $cardno=`cardno`";

$sql=$sql_insert.";".$sql_update;

  $result_insert = mysqli_query($con, $sql);


 echo "One raw Successfully inserted";




  mysqli_close($con);

}

于 2013-10-27T09:32:42.130 回答
0

用这个:

$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

$sql_insert = "INSERT INTO `kyc`( `date`,`agentid`,`formno`, `bank`, `ptype`,
   `cardno`,      `cname`, `fname`, `mname`, `dob`, `phone`, `votarid`,`sex`, 
   `email`, `district`, `thana`)
   VALUES('$date','$agentid',$formno,'$bank','$ptype','$cardno','$cname','$fname',
    '$mname','$dob',$phone,$votarid,'$sex','$email','$district','$thana')";

$sql_update="UPDATE card SET status='1'WHERE $cardno=`cardno`";

$sql=$sql_insert.";".$sql_update;

$mysqli->multi_query($sql);

使用 multi_query,您可以在一句话中运行多个查询。

查看:http
://docs.php.net/mysqli.multi-query 了解更多信息!

于 2013-10-27T09:19:20.713 回答
0

您可以通过编写触发器来实现这一点。所以每当插入发生触发器都会执行。

这些链接将帮助您学习触发器 http://dev.mysql.com/doc/refman/5.0/en/create-trigger.html http://net.tutsplus.com/tutorials/databases/introduction-to-mysql -触发器/

如果您真的想将更新作为查询执行,您可以通过;在插入查询末尾添加来尝试查询。

 $sql = "INSERT INTO `kyc`( `date`,`agentid`,`formno`, `bank`, `ptype`,
   `cardno`,      `cname`, `fname`, `mname`, `dob`, `phone`, `votarid`,`sex`, 
   `email`, `district`, `thana`)
   VALUES('$date','$agentid',$formno,'$bank','$ptype','$cardno','$cname','$fname',
    '$mname','$dob',$phone,$votarid,'$sex','$email','$district','$thana');";
                                                                         ^
于 2013-10-27T09:14:59.347 回答