douzhiling3166 2017-05-23 11:50 采纳率: 100%
浏览 28

数据未在我的sql表中更新

I was trying to implement this code but few of the things are not working as stated:

1.I want to update EmailID,Phone and CallStatus fields only but it fetches data from 1st column. 2.When clicked on edit, it shows textboxes and on clicking update button "Record saved successfully" message appears but it is not updated in mysql table.

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>
  • 写回答

0条回答 默认 最新

    报告相同问题?