尝试一次更新数据库中的多个字段

时间:2014-05-27 17:29:16

标签: php mysqli

我创建了一个PHP,可以在另一个页面上从原始表单成功收集数据。我知道这是因为我运行了自己的调试技术,它显示所有变量都成功通过。

我的问题出现在下面的脚本中。我试图让它更新表格中的多个字段,但当$firstop$secondop及其值出现时,它只会更新$firstop选项。怎么解决这个问题?

<?php 
 $fname         = trim($_POST['upname']); 
 $sname         = trim($_POST['upsurename']);
 $firstop       = trim($_POST['firstup']);
 $update1       = trim($_POST['update1']);
 $secondop      = trim($_POST['secondup']);
 $update2    = trim($_POST['update2']);
 $var = trim($_POST['submit']);
 $con=mysqli_connect("localhost","root","","fypannadale");
 if (mysqli_connect_errno())
     {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
     }
 if (isset($firstop)) {
   switch ($firstop) {
     case "FirstName":
        mysqli_query($con,"UPDATE members SET FirstName='$update1'
         WHERE FirstName='$fname' AND LastName='$sname'");
         break;
     case "LastName":
       mysqli_query($con,"UPDATE members SET  LastName='$update1'
         WHERE FirstName='$fname' AND LastName='$sname'");
         break;
   }
 }
 if (isset($secondop)) {
   switch ($secondop) {
     case "FirstName1":
       mysqli_query($con,"UPDATE members SET FirstName='$update2' WHERE FirstName='$fname' AND LastName='$sname'");
       break;
     case "LastName1":
       mysqli_query($con,"UPDATE members SET LastName='$update2' WHERE FirstName='$fname' AND LastName='$sname'");
       break;
   }
 }?>

0 个答案:

没有答案