0

我正在建立一个相当简单的 crud 网站。我有三个基于Mike Tuupola 的 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>";
  ?>

我省略了其余的表单代码(但如果需要我可以发布)。现在这是我的问题:在诸如姓名和电话之类的文本输入中,它们的值默认为数据库中已有的值,这就是它 echo 的原因value='<?= $row['name'] ?>'。这意味着如果用户不编辑这些,默认值将发送到 mysql 更新并保持不变。不幸的是,选择/下拉列表的值设置类似于 Mike Tuupola 的链式选择,并且在用户单击它之前没有值,这意味着 -> 用户进行编辑,只想编辑业务名称并提交,这意味着所有选择都保存为0。

如果我不将选择/下拉菜单更改为不会保存为 0 并且保持原样的值,我该如何获得它?

感谢大家的帮助!

4

1 回答 1

0

两种选择:

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

根据提交的值动态构建 SQL 更新查询,检查是否每个都未设置为 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");
}
于 2012-07-26T17:48:09.970 回答