Mysql Update无法使用隐藏字段

时间:2012-07-31 01:04:56

标签: php mysql

我有一个问题,我有一个相当基本的crud应用程序网站。我在编辑部分遇到问题,我有文本输入并在页面上选择下拉列表。对于所有文本输入,我能够使值等于该行的数据库中已有的值,因此,如果我通过edit.php并且不编辑某些字段,则仍然保留该值并没有为我没碰过的田地擦干净。不幸的是,我正在使用Jquery来填充一些选择/下拉列表,因此这种方法不起作用。

示例场景:

我是一名用户,添加第一类是餐馆的子公司,子类别是美国食品,然后第二类是家庭服务和子类别家庭维修。然后我点击“保存”按钮,我的类别被插入。然后我意识到我忘记将我的姓名和电话号码添加到我的列表中,所以我编辑了列表,这次只插入我的姓名和电话号码,并将所有其他字段留空。根据我现在的情况,我第一次保存的类别和子类别现在设置为0或空白。

我的解决方案是填充一些隐藏的字段(命名为beforecat1,beforesubcat1,依此类推),这些字段存储了字段之前的值,然后如果未触及选择,则插入值的隐藏的领域。不幸的是,有些事情没有用。

<? 
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'])){
   $cselect3 = $_REQUEST['cselect3'];

   $query="SELECT cat FROM cat WHERE id='$cselect3'";
   $result = mysql_query ($query);
   while($catselect=mysql_fetch_array($result)){
   $catselect3 = $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'”));

&GT;

这是我表格的一部分:

<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>";
 ?>


 <?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>";
 ?>

 <?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>";
?>

出了什么问题?感谢所有的帮助,这是一个困扰我很长时间的问题。谢谢!

1 个答案:

答案 0 :(得分:0)

我认为您的解决方案可能是将<select>的默认选定值设置为当前从数据库中提取的值。

让我们展开这个:

 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'];
  }

虽然我不是100%肯定你正在做什么,看起来你根据提交的ID抓取一个值,并将var $ catselect1设置为类别名称?

我们可以使用其中之一,但由于这个问题归结为$ catselect1,让我们使用它。

所以在这里,在这个领域:

<?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>";
?>

在你的同时,我们需要将$ catselect1值与我认为你的类别名称进行比较,使用$ catinfo ['cat'](这也是一个名字?)并添加如下条件:

if (  $catselect1 == $catinfo['cat'] )
  //this one is selected, because the values match!
  echo "<option default selected value=\"".htmlspecialchars($catinfo['number'])."\">".$catinfo['cat']."    </option>";
} else {
  //do normally
  echo "<option  value=\"".htmlspecialchars($catinfo['number'])."\">".$catinfo['cat']."     </option>";
}

因此,当您的表单被加载时,您的选择将默认为DB中设置的值,如果重新提交,则会传递相同的值。

希望这有点道理!

PS:我更喜欢这种方法用于简单的真/假回声:

 echo ( $comparison == $comparison ) ? 'true' : 'false';