简体   繁体   English

更新MYSQL记录错误

[英]UPDATING MYSQL record error

I am working on a script to update a record. 我正在使用脚本来更新记录。 The script is fine in my view and I am also getting a success message but the record is not being updated in the database. 我认为脚本很好,我也收到一条成功消息,但记录未在数据库中更新。 Please help me sort this out. 请帮我解决这个问题。

Here is my Form page. 这是我的表单页面。

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

And Here is my edit_ac.php 这是我的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";
}

?> 

It is showing "0 record updated successfully". 显示“ 0条记录更新成功”。 So its not updated in the database. 因此其未在数据库中更新。 Whats the problem please help. 有什么问题请帮忙。

You didn't declare the $id variable. 您没有声明$ id变量。 I added this line: $id = $_POST['id']; 我添加了这一行:$ id = $ _POST ['id'];

Anyway, mysql functions are deprecated: use mysqli instead. 无论如何,不​​建议使用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";
}

?>

The problem, I think, is that you're in both cases not passing $id variable to the query correctly: 我认为问题在于,在两种情况下,您都没有将$id变量正确传递给查询:

You do: 你做:

WHERE id='$id'"

You should: 你应该:

WHERE id=" . $id .""

EDIT 1: Try checking by printing $id variable, if you really have it set at all: 编辑1:如果真的设置过,尝试通过打印$id变量进行检查:

var_dump($id);

EDIT 2: 编辑2:

Do the rest with other variables: 使用其他变量进行其余操作:

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

Also try printing and checking if you really get $name, $email, $phone and etc.. 另外,尝试打印并检查您是否真的得到了$name, $email, $phone等。

Store your id value in Session so you can use it on different page as well. 将您的id值存储在Session中,以便您也可以在其他页面上使用它。 Here the edit file doesn't have the ID. 这里的编辑文件没有ID。

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM