0

Hello all I have a small problem where by my database is not updating my simple testimonial app! My app works fine when inserting an entry into the database with this parsing script..

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

I find that my Updating script does not update! Am I missing something here guys? Many thanks...

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

UPDATE This is what i have now guys...

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

Hi all as requested here is my HTML form as requested! Yea I know it's in a Table. I also have some php code above the form which is this...

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

And then my form is...

<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>
4

2 回答 2

0

Remove LIMIT 1 in UPDATE it should be fine. You do not need LIMIT in UPDATE syntax except when you want to count row

于 2013-06-27T15:39:09.990 回答
0

Firstly, I would do this with PDO.... because...mysql_* functions are deprecated and will be removed from PHP soon. PDO is more secure, and you don't have to mysql_real_escape_string everything, its handled for you in prepared statements.

  $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));

But to answer your question.....I suspect your $_POST['pid'] isn't really passing properly, hence the row isn't updating.

You need to show us your HTML form that submits this info, if you want more help

EDIT

As I suspected, you aren't passing the $pid anywhere in that form, hence the script on the other side has no idea of which ID to update.

You need to populate a hidden field with the ID from this record

<input type="hidden" name="pid" id="pid" value="<?echo $idfromdatabase;?>">
于 2013-06-27T16:04:24.127 回答