0

我正在建立一个允许用户编辑业务信息的网站。有 6 个选择,3 个用于类别,3 个用于子类别。我正在使用隐藏字段来获取每个类别和子类别之前的值,然后如果选择 isset,则插入选择/下拉列表的值。这是我的代码:对于 MYSQL 更新:

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

       if(isset($_REQUEST['cselect1'])){
       $cselect1 = $_REQUEST['cselect1'];



       $query="SELECT cat FROM cat WHERE id='$cselect1'";
       $result = mysql_query ($query);
       while($catselect=mysql_fetch_array($result)){
      $catselect1 = $catselect['cat'];
       }
       } else {
       $catselect1 = $_REQUEST['beforecat1'];
       }
       if(isset($_REQUEST['cselect2'])){
       $cselect2 = $_REQUEST['cselect2'];

       $query="SELECT cat FROM cat WHERE id='$cselect2'";
       $result = mysql_query ($query);
       while($catselect=mysql_fetch_array($result)){
          $catselect2 = $catselect['cat'];
       }
       } else {
       $catselect2 = $_REQUEST['beforecat2'];
       }
       if(isset($_REQUEST['cselect3'])){
       $cselect1 = $_REQUEST['cselect3'];

       $query="SELECT cat FROM cat WHERE id='$cselect3'";
       $result = mysql_query ($query);
       while($catselect=mysql_fetch_array($result)){
       $catselect1 = $catselect['cat'];
       }
       } else {
       $catselect3 = $_REQUEST['beforecat3'];
       }

   $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']);
    ?>
    <input type='hidden' value='<?php echo $cat1;?> name='beforecat1'/>
    <input type='hidden' value='<?php echo $cat2;?> name='beforecat2'/>
    <input type='hidden' value='<?php echo $cat3;?> name='beforecat3'/>
    <input type='hidden' value='<?php echo $subcat1;?> name='beforesubcat1'/>
    <input type='hidden' value='<?php echo $subcat2;?> name='beforesubcat2'/>
    <input type='hidden' value='<?php echo $subcat3;?> name='beforesubcat3'/>
    <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>";
  ?>

我的希望是,如果用户更改了 Cat 和 Subcat 选择/下拉菜单,那么它将在 mysql 查询中更新。否则,如果它们保持不变,那么从数据库中选择的隐藏输入将像以前一样在 mysql 表中更新。但是有些东西不起作用,当您提交表单而不触摸选择/下拉菜单时,猫和子猫就会被清除干净。我该如何解决?

感谢大家的帮助!

4

1 回答 1

0

我认为为您的表单寻找客户端解决方案,例如 Jquery 将解决您的问题。

于 2012-07-26T19:17:27.093 回答