行未更新

时间:2013-06-27 15:28:01

标签: php mysql regex

大家好,我有一个小问题,我的数据库没有更新我的简单推荐应用程序! 使用此解析脚本将条目插入数据库时​​,我的应用程序正常工作..

<?php
      $testtitle = preg_replace('#[^a-z0-9""-. ]#i', '', $_POST['ts_tt']);
      $testbody = preg_replace('#[^a-z0-9""-. ]#i', '', $_POST['ts_tb']);
      $compowner = preg_replace('#[^a-z0-9 ]#i', '', $_POST['ts_co']);
      $ownertitle = preg_replace('#[^a-z0-9 ]#i', '', $_POST['ts_ot']);
      $compname = preg_replace('#[^a-z0-9 ]#i', '', $_POST['ts_cn']);
      $compwebsite = preg_replace('#[^a-z0-9 .-]#i', '', $_POST['ts_cw']);

      include_once "../php_includes/db_conx.php";

      $sql = "INSERT INTO testimonials (testtitle, testbody, compowner, ownertitle, compname, compwebsite)
              VALUES ('$testtitle', '$testbody', '$compowner', '$ownertitle', '$compname', '$compwebsite')";
      if (!mysql_query($sql, $connection)){
          die('Error: ' . mysql_error());
  }
  exit();
?>

我发现我的更新脚本没有更新!我在这里错过了什么吗?非常感谢......

<?php
      $pid = $_POST['pid'];
      $testtitle = preg_replace('#[^a-z0-9""-. ]#i', '', $_POST['ts_tt']);
      $testbody = preg_replace('#[^a-z0-9""-. ]#i', '', $_POST['ts_tb']);
      $compowner = preg_replace('#[^a-z0-9 ]#i', '', $_POST['ts_co']);
      $ownertitle = preg_replace('#[^a-z0-9 ]#i', '', $_POST['ts_ot']);
      $compname = preg_replace('#[^a-z0-9 ]#i', '', $_POST['ts_cn']);
      $compwebsite = preg_replace('#[^a-z0-9 .-]#i', '', $_POST['ts_cw']);

      include_once "../php_includes/db_conx.php";

      $sql = "UPDATE testimonials SET testtitle='$testtitle', testbody='$testbody', compowner='$compowner', ownertitle='$ownertitle', compname='$compname', compwebsite='$compwebsite' WHERE id='$pid' LIMIT 1";

      if (!mysql_query($sql, $connection)){
          die('Error: ' . mysql_error());
  }
  exit();
?>

更新 这就是我现在拥有的......

<?php
$sql = "SELECT * FROM testimonials WHERE id='$pid'"; 

      $pid = $_POST['pid'];
      $testtitle = preg_replace('#[^a-z0-9""-. ]#i', '', $_POST['ts_tt']);
      $testbody = preg_replace('#[^a-z0-9""-. ]#i', '', $_POST['ts_tb']);
      $compowner = preg_replace('#[^a-z0-9 ]#i', '', $_POST['ts_co']);
      $ownertitle = preg_replace('#[^a-z0-9 ]#i', '', $_POST['ts_ot']);
      $compname = preg_replace('#[^a-z0-9 ]#i', '', $_POST['ts_cn']);
      $compwebsite = preg_replace('#[^a-z0-9 .-]#i', '', $_POST['ts_cw']);

      include_once "../php_includes/db_conx.php";

      $sql = "UPDATE testimonials SET testtitle='$testtitle', testbody='$testbody', compowner='$compowner', ownertitle='$ownertitle', compname='$compname', compwebsite='$compwebsite' WHERE id='$pid'";

      if (!mysql_query($sql, $connection)){
          die('Error: ' . mysql_error());
  }
  exit();
?>

大家都按照要求提供了我的HTML表格!是的,我知道它在表中。我在表格上面也有一些PHP代码就是这个......

<?php 
$pid = ereg_replace("[^0-9]", "", $_POST['pid']);
include_once "../php_includes/db_conx.php";
$sql = "SELECT testtitle, testbody, compowner, ownertitle, compname, compwebsite FROM testimonials WHERE id='$pid' LIMIT 1"; 
$query = mysql_query($sql, $connection) or die (mysql_error()); 
while ($row = mysql_fetch_array($query)) { 
    $testtitle = $row["testtitle"];
    $testtitle = str_replace("<br />", "", $testtitle);
    $testbody = $row["testbody"];
    $testbody = str_replace("<br />", "", $testbody);
    $compowner = $row["compowner"];
    $ownertitle = $row["ownertitle"];
    $compname = $row["compname"];
    $compwebsite = $row["compwebsite"];
} 
mysql_free_result($query); 
?>

然后我的表格就是......

<form method="post" action="testimonial_edit_parse.php" onsubmit="return validate_form ( );">
  <tr>
    <td width="12%" align="right" bgcolor="#F5E4A9">Testimonial Full Title</td>
    <td width="88%" bgcolor="#F5E4A9"><input name="ts_tt" id="testtitle" type="text" size="80" maxlength="64" value="<?php echo $testtitle; ?>" /></td>
  </tr>
  <tr>
    <td align="right" valign="top" bgcolor="#DAEAFA">Testimonial Body</td>
    <td bgcolor="#DAEAFA"><textarea name="ts_tb" id="testbody" cols="60" rows="16"><?php echo $testbody; ?></textarea></td>
  </tr>
  <tr>
    <td align="right" bgcolor="#D7EECC">Company Owner</td>
    <td bgcolor="#D7EECC"><input name="ts_co" id="compowner" type="text" maxlength="64" size="80" value="<?php echo $compowner; ?>" /></td>
  </tr>
  <tr>
    <td align="right" bgcolor="#D7EECC">Owner Title</td>
    <td bgcolor="#D7EECC"><input name="ts_ot" id="ownertitle" type="text" maxlength="64" size="80" value="<?php echo $ownertitle; ?>"/></td>
  </tr>
  <tr>
    <td align="right" bgcolor="#D7EECC">Company Name</td>
    <td bgcolor="#D7EECC"><input name="ts_cn" id="compname" type="text" maxlength="64" size="80" value="<?php echo $compname; ?>" /></td>
  </tr>
  <tr>
    <td align="right" bgcolor="#D7EECC">Company Website</td>
    <td bgcolor="#D7EECC"><input name="ts_cw" id="compwebsite" type="text" maxlength="64" size="80" value="<?php echo $compwebsite; ?>" /></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><input type="submit" name="ts_button" value="Submit this edit" /></td>
  </tr>
  </form>

2 个答案:

答案 0 :(得分:0)

在UPDATE删除LIMIT 1应该没问题。 除非要计算行

,否则在UPDATE语法中不需要LIMIT

答案 1 :(得分:0)

首先,我会用PDO执行此操作....因为...不推荐使用mysql_ *函数,很快就会从PHP中删除它们。 PDO更安全,你不需要mysql_real_escape_string所有内容,它都是在准备好的语句中为你处理的。

  $conn = new PDO("mysql:host=localhost;dbname=$yourdbname",$yourdbuser,$yourdbpass);

  $sql = "UPDATE testimonials 
    SET testtitle=?, testbody=?, compowner=?, ownertitle=?, compname=?, compwebsite=?,
    WHERE id=?";
  $q = $conn->prepare($sql);
  $q->execute(array($testtitle,$testbody, $compowner, $ownertitle, $compname, $compwebsite, $pid));

但要回答你的问题.....我怀疑你的$_POST['pid']并没有真正通过,因此该行不会更新。

如果您需要更多帮助,则需要向我们展示提交此信息的HTML表单

修改

正如我所怀疑的那样,你并没有在该表单中的任何地方传递$ pid,因此另一方的脚本不知道要更新哪个ID。

您需要使用此记录中的ID填充隐藏字段

<input type="hidden" name="pid" id="pid" value="<?echo $idfromdatabase;?>">