0

I am creating simple PHP update script. In that, whenever I update data form does not shows updated data. Means update query work and record is updated successfully but updated data does not reflects in text box in form. But whenever I refresh/redirect page again then it shows updated text in text box. Below is the code.

<?php
    mysql_connect("localhost","root","");
    mysql_select_db("test");

    if(isset($_POST['update']))
    {
      $txt =  $_POST['txt'];
      $que= mysql_query("update test set test='$txt' where id='1'");
    }
    $que= mysql_query("select * from test where id=1");
    $data=mysql_fetch_array($que); 
    $test = $data[0];

?>
  <form method="post">
   <input type="text" name="txt" value="<?php echo $test; ?>">
   <button name="update">Update</button>
 </form>
vishuB
  • 4,173
  • 5
  • 31
  • 49
Rahul
  • 1

2 Answers2

0

You forget to write type="submit" and Major bugs are fixed

Use this

<?php
    mysql_connect("localhost","root","");
    mysql_select_db("test");

    $que= mysql_query("select * from test where id=1");
    $data = mysql_fetch_array($que);

    if(isset($_POST['update']))
    {
        $txt =  $_POST['txt'];
        $que= mysql_query("update test set test='$txt' where id='1'");

    }


?>
<form method="post">
    <input type="text" name="txt" value="<?php if(!empty($data)){ echo $data; } else { } ?>">
    <button name="update" type="submit">Update</button>
</form>

Warning This extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used.

and Read How can I prevent SQL-injection in PHP?

Community
  • 1
  • 1
Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85
0

change the type to submit and add the action.

if(isset($_POST['update']))
{
  $txt =  $_POST['txt'];
  $que= mysql_query("update test set test='$txt' where id='1'");
}
$que= mysql_query("select * from test where id=1");
$data=mysql_fetch_array($que); 
$test = $data[0];
?>
<form method="post" action="">
<input type="text" name="txt" value="<?php echo $test;?>">
<input type="submit" name="update" value="Update">
</form>
user3040610
  • 750
  • 4
  • 15