我正在尝试实现此代码,但很少有事情没有按照规定运行:
1.我想仅更新EmailID,Phone和CallStatus字段,但它从第1列获取数据。 2.当点击编辑时,它会显示文本框,并在单击更新按钮时出现“已成功保存记录”消息但在mysql表中未更新。
index_data.php
<?php
mysql_connect("localhost","root");
mysql_select_db("contacts");
$sql_query="SELECT * FROM contact";
$result_set=mysql_query($sql_query);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PHP Update Data From MySql</title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<center>
<div id="header">
<div id="content">
<label>PHP Update Data From MySql</label>
</div>
</div>
<div id="body">
<div id="content">
<table align="center" width="100%">
<tr>
<th>EmailID</th>
<th>Phone</th>
<th>CallStatus</th>
<th>Edit</th>
</tr>
<?php
if(mysql_num_rows($result_set)>0)
{
while($row=mysql_fetch_row($result_set))
{
?>
<tr>
<td><?php echo $row[1]; ?></td>
<td><?php echo $row[2]; ?></td>
<td><?php echo $row[3]; ?></td>
<td align="center"><a href="edit_data.php?edit_id=<?php echo $row[7]; ?>"><img src="b_edit.png" alt="Edit" /></a></td>
</tr>
<?php
}
}
else
{
?>
<tr>
<th colspan="3">There's No data found !!!</th>
</tr>
<?php
}
?>
</table>
</div>
</div>
<div id="footer">
<div id="content">
<hr /><br/>
</div>
</div>
</center>
</body>
</html>
edit_data.php
<?php
mysql_connect("localhost","root");
mysql_select_db("contacts");
if(isset($_GET['edit_id']))
{
$sql_query="SELECT EmailID,Phone,CallStatus FROM contact WHERE First_name=".$_GET['edit_id'];
$result_set=mysql_query($sql_query);
$fetched_row=mysql_fetch_array($result_set);
}
if(isset($_POST['btn-update']))
{
// variables for input data
$EmailID = $_POST['EmailID'];
$Phone = $_POST['Phone'];
$CallStatus = $_POST['CallStatus'];
// variables for input data
// sql query for update data into database
$sql_query = "UPDATE contact SET EmailID='$EmailID',Phone='$Phone',CallStatus='$CallStatus' WHERE First_name=".$_GET['edit_id'];
// sql query for update data into database
// sql query execution function
if(mysql_query($sql_query))
{
?>
<script type="text/javascript">
alert('Data Are Updated Successfully');
window.location.href='index_data.php';
</script>
<?php
}
else
{
?>
<script type="text/javascript">
alert('error occured while updating data');
</script>
<?php
}
// sql query execution function
}
if(isset($_POST['btn-cancel']))
{
header("Location: index_data.php");
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PHP Update Data From MySql</title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<center>
<div id="header">
<div id="content">
<label>PHP PHP Update Data From MySql</label>
</div>
</div>
<div id="body">
<div id="content">
<form method="post">
<table align="center">
<tr>
<td><input type="text" name="EmailID" placeholder="EmailID" value="<?php echo $fetched_row['EmailID']; ?>" required /></td>
</tr>
<tr>
<td><input type="text" name="Phone" placeholder="LastName" value="<?php echo $fetched_row['Phone']; ?>" required /></td>
</tr>
<tr>
<td><input type="text" name="CallStatus" placeholder="CallStatus" value="<?php echo $fetched_row['CallStatus']; ?>" required /></td>
</tr>
<tr>
<td>
<button type="submit" name="btn-update"><strong>UPDATE</strong></button>
<button type="submit" name="btn-cancel"><strong>Cancel</strong></button>
</td>
</tr>
</table>
</form>
</div>
</div>
<div id="footer">
<div id="content">
<hr /><br/>
</div>
</div>
</center>
</body>
</html>