0

This is the code for attempting to do a update on mysql data errors stating undefined variable

mysql_connect ("localhost", "root", "");
mysql_select_db("supplierdetails");
 $con = mysql_connect("localhost", "root", "");
   if (!$con)
    {
    die('Could not connect: ' . mysql_error());
    }
//Run a query 
    $result = mysql_query ("SELECT * FROM users WHERE id= '$id'");
    while ($row = mysql_fetch_array($result))
   {
$username=$row['username'];
$password=$row['password'];
  }
$query = "UPDATE users SET username = '$username', password = '$password' WHERE id           = '$id'";
$result = @mysql_query($query);
//Check whether the query was successful or not
if($result) {  
    header("message= Users Updated");
}else {
    die("Query failed");
}
?>
Natalie Webb
  • 33
  • 2
  • 6

3 Answers3

0

you used 2 connect no need to do while and you forgot $id

    $con = mysql_connect("localhost", "root", "");
    mysql_select_db("supplierdetails");

       if (!$con)
        {
        die('Could not connect: ' . mysql_error());
        }

    $id = $_POST['id'];        
    $username=$_POST['username'];
    $password=$_POST['password'];

    $query = "UPDATE users SET username = '".$username."', password = '".$password."' WHERE id = '".$id."'";
    $result = mysql_query($query);
    //Check whether the query was successful or not
    if($result) {  
        echo "message= Users Updated";
    }else {
        die("Query failed");
    }
    ?>
mgraph
  • 15,238
  • 4
  • 41
  • 75
0

You miss the $id value?

And can use echo to debug or check script result, not header

http://php.net/manual/en/function.header.php

0

Please be more specific with regards to which variable is undefined.

In the code you've posted $username and $password are only set if $result returns a result, if it doesn't then your while loop will not run and therefore $username and $password will never be set.

Also $id doesn't look as if that has been set either, unless this has been set outside of the code which you have included in your question.

Hope this helps :)

billyonecan
  • 20,090
  • 8
  • 42
  • 64
  • Notice: Undefined variable: id in E:\xampp\htdocs\Edit_User.php on line 10 Notice: Undefined variable: username in E:\xampp\htdocs\Edit_User.php on line 16 Notice: Undefined variable: password in E:\xampp\htdocs\Edit_User.php on line 16 Notice: Undefined variable: id in E:\xampp\htdocs\Edit_User.php on line 16 – Natalie Webb Apr 10 '12 at 09:28
  • ***I have a user_edit_form with the following code** – Natalie Webb Apr 10 '12 at 09:29
  • @NatalieWebb are you getting value of $_GET['id']? Please echo out $_GET. – heyanshukla Apr 10 '12 at 09:38
  • What is it you're actually trying to achieve here? From what I can see you're retrieving a users details, then just updating that users details with the same values :| – billyonecan Apr 10 '12 at 09:50
  • I know :\ this is not what i want to do i have a user form that all i want to do is change the details for?! but struggling – Natalie Webb Apr 10 '12 at 09:52