更新MYSQL记录错误

时间:2015-02-10 10:12:47

标签: php mysql pdo mysqli phpmyadmin

我正在编写一个脚本来更新记录。我的视图中的脚本很好,我也收到了成功消息,但记录没有在数据库中更新。请帮我解决这个问题。

这是我的表格页面。

<!-- Inquiry Table Stars here -->

<?php
$id=$_GET['id'];
$sql = "Select * From inquiry WHERE id='$id'";
$result = mysql_query($sql);


?>

<?php
while($row=mysql_fetch_array($result))
{
$name = $row['cname'];;
$email = $row['email'];;
$phone = $row['phone'];;
$sdate = $row['sdate'];;
$edate = $row['edate'];;
$fdate = $row['fdate'];;

?>




<!-- Code Begins -->
<center>
<div class="vpb_main_wrapper">

<br clear="all">
<form method="post" action="edit_ac.php">
<h2 align="left" style="margin-top:0px;">Edit Operator</h2><br />

<div align="left" style="font-family:Verdana, Geneva, sans-serif; font-size:11px; margin-bottom:10px;">Edit operator Details.</div><br />

<div style="width:115px; padding-top:10px;float:left;" align="left"> Full Name:</div>
<div style="width:300px;float:left;" align="left"><input type="text" name="cname" id="cname" value="<?php echo $name; ?>" class="vpb_textAreaBoxInputs" required>
</div><br clear="all"><br clear="all">

<div style="width:115px; padding-top:10px;float:left;" align="left"> Email:</div>
<div style="width:300px;float:left;" align="left"><input type="text" name="email" id="email" value="<?php echo $email; ?>" class="vpb_textAreaBoxInputs" required>
</div><br clear="all"><br clear="all">


<div style="width:115px; padding-top:10px;float:left;" align="left">Mobile:</div>
<div style="width:300px;float:left;" align="left"><input type="text" name="phone" id="phone" value="<?php echo $phone; ?>" required class="vpb_textAreaBoxInputs">
</div><br clear="all"><br clear="all">


<div style="width:115px; padding-top:10px;float:left;" align="left">Start Date:</div>
<div style="width:300px;float:left;" align="left"><input type="text" name="sdate" id="sdate" value="<?php echo $sdate; ?>" required class="vpb_textAreaBoxInputs">
</div><br clear="all"><br clear="all">


<div style="width:115px; padding-top:10px;float:left;" align="left">End Date:</div>
<div style="width:300px;float:left;" align="left"><input type="text" name="edate" id="edate" value="<?php echo $edate; ?>" required class="vpb_textAreaBoxInputs">
</div><br clear="all"><br clear="all">


<div style="width:115px; padding-top:10px;float:left;" align="left">Date of Journey:</div>
<div style="width:300px;float:left;" align="left"><input type="text" name="fdate" id="fdate" value="<?php echo $fdate; ?>" class="vpb_textAreaBoxInputs" required>
</div><br clear="all"><br clear="all">

<input name="id" type="hidden" id="id" value="<?php echo $row['id']; ?>">

<div style="width:115px; padding-top:10px;float:left;" align="left">&nbsp;</div>
<div style="width:300px;float:left;" align="left">
<input type="submit" name="submit" id="submit" value="Submit" style="margin-left: 100px" class="vpb_general_button">

</div>

</form>
<br clear="all"><br clear="all">

</div><?php } ?>
</center>
<!-- Code Ends -->


<!-- Inquiry Table Ends here -->

这是我的edit_ac.php

  <?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="pro1"; // Database name

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$name = $_POST['cname'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$sdate = $_POST['sdate'];
$edate = $_POST['edate'];
$fdate = $_POST['fdate'];

// update data in mysql database
$sql="UPDATE inquiry SET 
      cname = '$name', 
      email = '$email',
      phone = '$phone', 
      sdate = '$sdate', 
      edate = '$edate', 
      fdate = '$fdate'  
      WHERE id='$id'";

$result=mysql_query($sql);

// if successfully updated.
if($result){
echo "Successfull";

}

else {
echo "Error";
}

?> 

显示&#34; 0记录已成功更新&#34;。所以它没有在数据库中更新。问题是什么,请帮助。

3 个答案:

答案 0 :(得分:1)

您没有声明$ id变量。我添加了这一行:$ id = $ _POST ['id'];

无论如何,不​​推荐使用mysql函数:改用mysqli。

<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="pro1"; // Database name

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$id = $_POST['id'];
$name = $_POST['cname'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$sdate = $_POST['sdate'];
$edate = $_POST['edate'];
$fdate = $_POST['fdate'];

// update data in mysql database
$sql="UPDATE inquiry SET 
  cname = '$name', 
  email = '$email',
  phone = '$phone', 
  sdate = '$sdate', 
  edate = '$edate', 
  fdate = '$fdate'  
    WHERE id='$id'";

$result=mysql_query($sql);

// if successfully updated.
if($result){
echo "Successfull";

}

else {
echo "Error";
}

?>

答案 1 :(得分:0)

我认为,问题是您在两种情况下都没有正确地将$id变量传递给查询:

你这样做:

WHERE id='$id'"

你应该:

WHERE id=" . $id .""

编辑1:尝试通过打印$id变量进行检查,如果您确实设置了该变量:

var_dump($id);

编辑2:

用其他变量完成剩下的工作:

$sql = "UPDATE 
          inquiry
        SET 
          cname = " . $name . ", 
          email = " . $email . ",
          phone = " . $phone . ", 
          sdate = " . $sdate . ", 
          edate = " . $edate . ", 
          fdate = " . $fdate . "  
        WHERE 
          id=" . $id . "";

同时尝试打印并检查您是否真的得到$name, $email, $phone等等。

答案 2 :(得分:-3)

将您的ID值存储在Session中,以便您也可以在不同的页面上使用它。这里的编辑文件没有ID。

希望它有所帮助。