-5

Hello I have an edit form but I get an error that I cant solve, can you help me to fix it? The error that I ge is from the first code and it is this " Notice: Undefined index: username in C:\xampp\htdocs\industrial\CVTool\cv\edit_personal_information.php on line 5" The code for the form is the below..

<?php
mysql_connect('localhost', 'root', 'password') or die(mysql_error());
mysql_select_db("cvtool") or die(mysql_error());

$username = $_GET['username'];
$query = mysql_query("SELECT * FROM personal_information WHERE username = '$username'") or die(mysql_error());

if(mysql_num_rows($query)>=1){
    while($row = mysql_fetch_array($query)) {
        $name = $row['name'];
        $email = $row['email'];
        $city = $row['city'];
        $phone = $row['phone'];
    }
?>
<form action="update.php" method="post">
<input type="hidden" name="username" value="<?=$username;?>">
Name: <input type="text" name="name" value="<?=$name?>"><br>
Email: <input type="text" name="email" value="<?=$email?>"><br>
City: <input type="text" name="city" value="<?=$city?>"><br>
Phone: <input type="text" name="city" value="<?=$phone?>"><br>
<input type="Submit" name="Update!">
</form>
<?php
}else{
    echo 'No entry found. <a href="javascript:history.back()">Go back</a>';
}
?>

The code for the update is the below...

 <?php
     mysql_connect('localhost', 'root', 'smogi') or die(mysql_error());
     mysql_select_db("cvtool") or die(mysql_error());

     $username = $_POST["username"];

     $name = mysql_real_escape_string($_POST["name"]);
     $email = mysql_real_escape_string($_POST["email"]);
     $city = mysql_real_escape_string($_POST["city"]);
     $phone = mysql_real_escape_string($_POST["phone"]);


     $query="UPDATE personal_information
             SET name = '$name', email = '$email', phone = '$phone' 
             WHERE username='$username'";


 mysql_query($query)or die(mysql_error());
 if(mysql_affected_rows()>=1){
     echo "<p>($username) Record Updated<p>"; }else{
     echo "<p>($username) Not Updated<p>"; } ?>
user692942
  • 16,398
  • 7
  • 76
  • 175
Nick Bourlai
  • 31
  • 1
  • 7
  • Please, [don't use `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php), They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://us1.php.net/pdo) or [MySQLi](http://us1.php.net/mysqli). – Jay Blanchard Dec 15 '14 at 14:16
  • @JayBlanchard your comment does not help me to solve my problem, thanks tho – Nick Bourlai Dec 15 '14 at 14:27
  • possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – andrewsi Dec 16 '14 at 03:46

1 Answers1

1

This is because when you first load the page, there are no $_GET["username"] variable. You should test it with a condition:

if (!empty($_GET["username"])) {
    //Do code here
}

It will have value only when you submiting the form.

vaso123
  • 12,347
  • 4
  • 34
  • 64
  • I did what u said but now it gives "no entry found" but in my database I have stored data with this username that I logged in. – Nick Bourlai Dec 15 '14 at 14:12
  • maybe the given username is not in the db? – vaso123 Dec 15 '14 at 14:15
  • that's the strange part of my problem that I'm logged in I can see that my username is in the specific column of the table but it still gives me this notification!!!! – Nick Bourlai Dec 15 '14 at 14:19