0

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. Kindly tell me where i m 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>
Asad Basit
  • 9
  • 1
  • 2
  • 1
    Every time you use [the `mysql_`](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) database extension in new code **[a Kitten is strangled somewhere in the world](http://2.bp.blogspot.com/-zCT6jizimfI/UjJ5UTb_BeI/AAAAAAAACgg/AS6XCd6aNdg/s1600/luna_getting_strangled.jpg)** it is deprecated and has been for years and is gone for ever in PHP7. If you are just learning PHP, spend your energies learning the `PDO` or `mysqli` database extensions. [Start here](http://php.net/manual/en/book.pdo.php) – RiggsFolly Jan 03 '17 at 01:06
  • ` EDIT `, you can't embed php variables like this. – Marko Mackic Jan 03 '17 at 01:06
  • Your script is at risk of [SQL Injection Attack](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) Have a look at what happened to [Little Bobby Tables](http://bobby-tables.com/) Even [if you are escaping inputs, its not safe!](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) Use [prepared parameterized statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) – RiggsFolly Jan 03 '17 at 01:06
  • The best thing to use when interfacing db is already written framework for that, it's much safer, and much easier :) – Marko Mackic Jan 03 '17 at 01:08
  • Aside from all the the above highly valid statements. The short answer is you can't because PHP is server side. The longer more indepth and will require more research answer is... AJAX. You need to call the php up again with a new request without a page refresh so you need a method to do that. ajax will help. – CodingInTheUK Jan 03 '17 at 01:13

1 Answers1

0

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