user3346354
user3346354

Reputation: 1

Database table UPDATE form doesn´t work

I have read like 10 tutorials on how to update database info, I have tried at least 3 different ways, but nothing works. This code below always echo "Success", though it doesn´t update if I check the database.

if(isset($_POST['update']))
{
$dbhost = 'host';
$dbuser = 'user';
$dbpass = 'password';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}

$name= $_POST['name'];

$sql = "UPDATE table SET name='$name' WHERE id='$id'";

mysql_select_db('my_db');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not update data: ' . mysql_error());
}
echo "Success">";
mysql_close($conn);
}
else
{
  include"db.inc.php";
   $id = $_GET["id"];
   $order = "SELECT * FROM table where id='$id'";
   $result = mysql_query($order);
   $row = mysql_fetch_array($result);

}
    ?>
<form id="update" name="update" method="post" role="form" action="">
<input type="text" id="name" name="name" value="<? echo "$row[name]"?>" />
<input name="update" type="submit" id="update" value="Save">
</form>
<?php
}
?>

I don´t know what I am doing wrong...any help would be appreciated!

EDIT: I considered your comments and edited my code

if(isset($_POST['update']))
{
$dbhost = 'host';
$dbuser = 'user';
$dbpass = 'password';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}

$name= $_POST['name'];

mysql_select_db('my_db'); 
$sql = "UPDATE table SET name='$name' WHERE id='$id'";


$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not update data: ' . mysql_error());
}
else{
echo "Success";}

mysql_close($conn);
}
else
{
  include"db.inc.php";
   $id = $_GET["id"];
   $order = "SELECT * FROM table where id='$id'";
   $result = mysql_query($order);
   $row = mysql_fetch_array($result);

}
    ?>
<form id="update" name="update" method="post" role="form" action="">
<input type="text" id="name" name="name" value="<? echo "$row[name]"?>" />
<input name="update" type="submit" id="update" value="Save">
</form>
<?php
}
?>

I get $id variable through url like edit-form.php?id=123 This code above does exactly the same that the first one, echoes Success and do nothing with database info.

EDIT: Ok, I think it might me solved. I used the very first version of my code (the first one here) and I put

$id = $_GET["id"];

above

$name= $_POST['name']; 

It seems to work.

Upvotes: 0

Views: 1097

Answers (3)

user3346354
user3346354

Reputation: 1

Ok, I think it might me solved. I used the very first version of my code (the first one here) and I put

$id = $_GET["id"];

above

$name= $_POST['name']; 

It seems to work. Thanks everyone for their posts!

Upvotes: 0

mysqlrockstar
mysqlrockstar

Reputation: 2612

I hope these 5 points will help you

  1. Your echo "Success">"; is not in the conditional braces. No matter if the data gets update or not it will show success always

  2. mysql_select_db and mysql_query are all depreciated. Use mysqli or PDO instead try using mysqli_select_db ? It should be before the query. This is reason why the SQL is not updating . Try in this sequence

    mysqli_select_db($conn,'my_db'); $sql = "UPDATE table SET name='$name' WHERE id='$id'";

    For reference http://php.net/manual/en/mysqli.select-db.php

    OR

  3. You may like to do it in one line

    $conn = mysqli_connect("host","user","password","my_db") or die("Error " . mysqli_error($conn ));

  4. Are you getting the correct value of $id in WHERE id='$id'. As I see $id = $_GET["id"]; is after the SQL. For debugging print the query in browser, see if you are getting the right value

Upvotes: 2

Laura Morris
Laura Morris

Reputation: 222

You need to put the id in as well. If you dont want to make it visible to the user you could put it in as a hidden field so your form would be:

<form id="update" name="update" method="post" role="form" action="">
<input type="hidden" id="id" name="id" value="<? echo "$row[id]"?>" />
<input type="text" id="name" name="name" value="<? echo "$row[name]"?>" />
<input name="update" type="submit" id="update" value="Save">

Upvotes: 0

Related Questions