简体   繁体   English

如何在PHP中更新表单

[英]How to update form in PHP

I want to update record in php using GET method but whenever i click on "update button" it does not change anything and just refresh the page.and data remains the same.Here are the codes. 我想使用GET方法更新php中的记录,但每当我点击“更新按钮”时它不会改变任何东西,只刷新页面。数据保持不变。这是代码。 Kindly tell me where im doing it wrong. 请告诉我哪里做错了。 Thanks 谢谢

<?php
include_once"dbconfig.php";
if(isset($_GET['edit']))
{
    $id=$_GET['edit'];
    echo $id;
    $sql_query="SELECT * FROM users WHERE user_id='$id' ";

    $result_set=mysql_query($sql_query);
    $fetched_row=mysql_fetch_array($result_set);


}
 if(isset($_POST['btn-update']))
 {
     // variables for input data
     $first_name=$_POST['first_name'];
     $last_name=$_POST['last_name'];
     $city_name=$_POST['city_name'];

     //sql query to update into database
    $sql_query="UPDATE users SET first_name='$first_name',last_name='$last_name',user_city='$city_name' WHERE user_id='$id' ";
    mysql_query($sql_query);
    //if(!$sql_query)
        //die('data can not update'.mysql_error());
    //else
        //echo 'data updated successfully';


 }
 ?>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
 <link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<center>

<form method="post">
<table align="center">
<tr>
<td><input type="text" name="first_name" placeholder="First Name" value="<?php echo $fetched_row['first_name']; ?>"required />  </td>
</tr>
<tr>
<td><input type="text" name="last_name" placeholder="Last Name" value="<?php echo $fetched_row['last_name']; ?>" required /> </td>
</tr>
<tr>
<td><input type="text" name="city_name" placeholder="City" value="<?php echo $fetched_row['user_city']; ?>" required /> </td>
</tr>
<tr>
<td>
<button type="submit" name="btn-update"><strong>UPDATE</strong></button>
</td>
</tr>
</table>
</form>
</center>
</body>
</html>

Second File 第二档

<?php
include_once"dbconfig.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> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>CRUD Operations With php and MySql </title>
<link rel="stylesheet" href="style.css" type="text/css"/>
</head>
<body>
<center>

<div id="body">
 <div id="content">
    <table align="center">

<table align="center">
<tr>
<th colspan="9"><a href="add_data.php">Add data Here ! </a></th>
</tr>
<tr>
<th colspan="2">FirstName </th>
<th colspan="8">LastName</th>
<th colspan="4">CityName </th>
<th>Operations</th>

<!--<th colspan="2">Operations</th> -->
</tr>

<?php
 $sql_query="SELECT * FROM users";
  $result_set=mysql_query($sql_query);
  while($row=mysql_fetch_row($result_set))
  {
      ?>
    <tr>
    <td><?php echo $row[0]; ?> </td>
    <td colspan="4"><?php echo $row[1]; ?></td>
    <td colspan="6"><?php echo $row[2];?></td>
    <td colspan="4"><?php echo $row[3];?></td>
    <td> <a href='edit_data.php?edit=$row[0]'>EDIT</a> </td>
    <!--<td align="center"><a href="javascript edt_id('<//?php echo $row[0]; ?><img src="b_edit.png" align="EDIT" /></a></td>
     <td align="center"><a href="javascript:delete_id('<//?php echo $row[0];?><img src="b_drop.png" align="DELETE" /></a></td>
     </tr>   -->
     <?php

  }
     ?>

    </table>
    </div>
    </div>
    </center>
    </body>
    </html>

Change 更改

mysql_query($sql_query);

To

mysql_query($sql_query) or die (mysql_error()) ;

You're not sending the id (the form action property is missing 你没有发送id (表单动作属性丢失了

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

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