如果选择/下拉列表更改,则更新Mysql

时间:2012-07-26 17:37:58

标签: php javascript mysql sql-update

我正在构建一个相当简单的crud网站。我有三个选择/下拉列表都基于Mike Tuupola's Chained Selects Project

这是我的代码:

<? 
include('../config.php'); 
if (isset($_GET['id']) ) { 
   $id = (int) $_GET['id']; 
   if (isset($_POST['submitted'])) { 
       foreach($_POST AS $key => $value) { $_POST[$key] = mysql_real_escape_string($value); }
       $cselect1 = $_REQUEST['cselect1'];
       $cselect2 = $_REQUEST['cselect2'];
       $cselect3 = $_REQUEST['cselect3'];

       $query="SELECT cat FROM cat WHERE id='$cselect1'";
       $result = mysql_query ($query);
       while($catselect=mysql_fetch_array($result)){
       $catselect1 = $catselect['cat'];
       }
       $query="SELECT cat FROM cat WHERE id='$cselect2'";
       $result = mysql_query ($query);
       while($catselect=mysql_fetch_array($result)){
       $catselect2 = $catselect['cat'];
       }
       $query="SELECT cat FROM cat WHERE id='$cselect3'";
       $result = mysql_query ($query);
       while($catselect=mysql_fetch_array($result)){
       $catselect3 = $catselect['cat'];
       }
   $sql = "UPDATE `company` SET  `name` =  '{$_POST['name']}' ,  `phone` =  '{$_POST['phone']}' ,  `cat1` =  '$catselect1' ,  `cat2` =  '$catselect2' ,  `cat3` =  '$cselect3' ,  `zipcode` =  '{$_POST['zipcode']}' ,  `city` =  '{$_POST['city']}' ,  `address` =  '{$_POST['address']}' ,  `address2` =  '{$_POST['address2']}' ,  `website` =  '{$_POST['website']}' ,  `product1` =  '{$_POST['product1']}' ,  `product2` =  '{$_POST['product2']}' ,  `product3` =  '{$_POST['product3']}' ,  `product4` =  '{$_POST['product4']}' ,  `product5` =  '{$_POST['product5']}' ,  `product6` =  '{$_POST['product6']}' ,  `product7` =  '{$_POST['product7']}' ,  `subcat1` =  '{$_POST['sselect1']}' ,  `subcat2` =  '{$_POST['sselect2']}' ,  `subcat3` =  '{$_POST['sselect3']}'   WHERE `id` = '$id' "; 
   mysql_query($sql) or die(mysql_error());
   $sql = "UPDATE `company_secondary` SET  `company_description` =  '{$_POST['description']}' ,  `since` =  '{$_POST['since']}' ,  `smoking` =  '{$_POST['select3']}' ,  `delivery` =  '{$_POST['select5']}' ,  `alcohol` =  '{$_POST['select6']}' ,  `kids` =  '{$_POST['select1']}' ,  `wheelchair` =  '{$_POST['select2']}' ,  `twitter` =  '{$_POST['twitter']}' ,  `facebook` =  '{$_POST['facebook']}' ,  `youtube` =  '{$_POST['youtube']}' ,  `premium` =  '{$_POST['premium']}' ,  `creditcards` =  '{$_POST['select4']}' ,  `outdoor` =  '{$_POST['select7']}' ,  `featured` =  '{$_POST['featured']}' ,  `shortdesc` =  '{$_POST['shortdesc']}' WHERE company_id = '$id' "; 
   mysql_query($sql) or die(mysql_error());
   echo "Edited Row<br/>";
   echo "<a href='allbiz.php'>Back To Listing</a>"; 
  } 

  $row = mysql_fetch_array ( mysql_query("SELECT * FROM `company` WHERE `id` = '$id' "));

?>

这是我的表格:

<form action='' method='POST'> 
    <p><b>Name:</b><br /><input type='text' name='name' value='<?= $row['name'] ?>' /> 
    <p><b>Phone:</b><br /><input type='text' name='phone' value='<?= stripslashes($row['phone']) ?>' />
    <?php
        $cat1 = stripslashes($row['cat1']);
        $cat2 = stripslashes($row['cat2']);
        $cat3 = stripslashes($row['cat3']);
        $subcat1 = stripslashes($row['subcat1']);
        $subcat2 = stripslashes($row['subcat2']);
        $subcat3 = stripslashes($row['subcat3']);
    ?>
    <p><b>Cat1:</b><br />
   <?php
       $query="SELECT * FROM cat";
       $result = mysql_query ($query);
       echo"<select name='cselect1' id='cat1'><option value='0'>Please Select A Category</option>";
       // printing the list box select command
       while($catinfo=mysql_fetch_array($result)){//Array or records stored in $nt
            echo "<option value=\"".htmlspecialchars($catinfo['number'])."\">".$catinfo['cat']."</option>";

       }
       echo"</select>";
   ?>
   <?php
       $query="SELECT * FROM subcat";
       $result = mysql_query ($query);
   echo"<select name='sselect1' id='subcat1'><option value=''>Sub Category</option>";
   echo $subcat1;
   echo"'>$sucat1</option>";
       // printing the list box select command
       while($catinfo=mysql_fetch_array($result)){//Array or records stored in $nt
           echo "<option value='".htmlspecialchars($catinfo['subcat'])."' class='".$catinfo['catnumber']."'>".$catinfo['subcat']."</option>";
       }

       echo"</select>";
  ?>

  <p><b>Cat2:</b><br />
  <?php
      $query="SELECT * FROM cat";
      $result = mysql_query ($query);
  echo"<select name='cselect2' id='cat2'><option value='0'>Please Select A Category</option>";
      // printing the list box select command
      while($catinfo=mysql_fetch_array($result)){//Array or records stored in $nt
          echo "<option value=\"".htmlspecialchars($catinfo['number'])."\">".$catinfo['cat']."</option>";

      }
     echo"</select>";
   ?>
   <?php
     $query="SELECT * FROM subcat";
     $result = mysql_query ($query);
 echo"<select name='sselect2' id='subcat2'><option value=''>Sub Category</option>";
 echo $subcat1;
 echo"'>$sucat1</option>";
     // printing the list box select command
     while($catinfo=mysql_fetch_array($result)){//Array or records stored in $nt
         echo "<option value='".htmlspecialchars($catinfo['subcat'])."' class='".$catinfo['catnumber']."'>".$catinfo['subcat']."</option>";
     }

     echo"</select>";
  ?>
  <p><b>Cat3:</b><br />
  <?php
     $query="SELECT * FROM cat";
     $result = mysql_query ($query);
 echo"<select name='cselect3' id='cat3'><option value='0'>Please Select A Category</option>";
     // printing the list box select command
     while($catinfo=mysql_fetch_array($result)){//Array or records stored in $nt
         echo "<option value=\"".htmlspecialchars($catinfo['number'])."\">".$catinfo['cat']."</option>";

     }
     echo"</select>";
  ?>
  <?php
     $query="SELECT * FROM subcat";
     $result = mysql_query ($query);
 echo"<select name='sselect3' id='subcat3'><option value=''>Sub Category</option>";
     // printing the list box select command
     while($catinfo=mysql_fetch_array($result)){//Array or records stored in $nt
     echo "<option value='".htmlspecialchars($catinfo['subcat'])."' class='".$catinfo['catnumber']."'>".$catinfo['subcat']."</option>";
     }

     echo"</select>";
  ?>

我遗漏了其余的表单代码(但如果需要,我可以发布)。现在这是我的问题:在名称和电话等文本输入上,它们的值默认为数据库中的内容,这就是为什么它回显value='<?= $row['name'] ?>'。这意味着如果用户不编辑这些,则默认值将发送到mysql更新并保持不变。不幸的是,选择/下拉的值设置为Mike Tuupola的Chained选择,并且在用户点击它之前没有值,这意味着 - &gt;用户进入编辑,只想编辑业务名称并提交,这意味着所有选择都保存为0.

如果我不将选择/下拉菜单更改为一个不会保存为0并且保持原样的值,我该怎么做呢?

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

两个选项:

在处理POST数据之前,从数据库中获取当前信息。对于任何设置为0的POST值,请将它们更改为当前存储的值。

根据提交的值动态构建SQL Update查询,检查每个值是否设置为0,例如

$update_query = "";
foreach(Array('company_name', 'company_description', 'started') as $key) {
    if(isset($_POST[$key]) && $_POST[$key] !== 0) {
        if($update_query) {
            $update_query .= ", ";
        }
        $update_query .= "`{$key}`='". mysql_real_escape_string($_POST[$key]) ."'";
    }
}
if($update_query) {
    mysql_query("UPDATE `table` SET {$update_query} WHERE `id`='$id' LIMIT 1");
}