0

I am not able to edit the field and update it. The name field displays the text":

Notice: Undefined variable: row in D:\XAMPP\htdocs\test\test5.php on line 31

instead of the value. Might have done some silly code error, I'm a naivete.

<!DOCTYPE html>
<html>
<body>
<?php
define('DB_NAME', 'test');
define('DB_USER', '');
define("DB_PASSWORD", '');
define('DB_HOST', 'localhost');

$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
$db_selected = mysql_select_db(DB_NAME, $link);

if(isset($_GET['del']))
{
    $value1=$_GET['del']; 
    $rs = mysql_query("SELECT * from `add` where empid ='".$value1."'"); 
    $row = mysql_fetch_array($rs);
}
?>
    <form action="test7.php" method="post">
        <ul>
            <li>
                Name:</br>
                <input type="text" name="name" value="<?php echo $row[0]; ?>">
            </li>
            <li>
                <input type="submit" value="UPDATE">
            </li>
        </ul>
    </form>
</body>
</html>
AeroX
  • 3,387
  • 2
  • 25
  • 39
user645
  • 27
  • 1
  • 3
  • 1
    Your `$row` variable is not set if you don't have a `del` GET parameter. That is why you get an error when you try to read that variable on **line 31** with `` – AeroX Dec 23 '14 at 15:55

3 Answers3

3

You can check, is the $row[0]is exists. If there are no del parameter in the $_GET then $row will not exists, becuase you are creating $row in the condition.

<input type="text" name="name" value="<?php echo (isset($row[0]) ? $row[0] : ''); ?>">

NOTE

  • Do not use mysql functions. They are deprecated. Use mysqli or PDO instead.

  • Avoid sql injection by escaping your variables from outside, or use prepared statements.

vaso123
  • 12,347
  • 4
  • 34
  • 64
1

You are echo'ing $row[0] in your form without declaring it. You need to initialize and declare $row outside the if statement and before you echo it.

Kevin
  • 874
  • 3
  • 16
  • 34
0

updated code.

<?php

$row = "";
 if(isset($_GET['del']))
    {
     $value1=$_GET['del']; 
     $rs = mysql_query("SELECT * from `add` where empid ='".$value1."'"); 
     $row = mysql_fetch_array($rs);
    }
?>

<form action="test7.php" method="get">
    <ul>
        <li>
            Name:</br>
            <input type="text" name="name" value="<?php echo $row[0]; ?>">
        </li>

        <li>
            <input type="submit" value="UPDATE">
        </li>
    </ul>
</form>

you must declare $row first. if you declare it in if, you can't use it outside. Also change your form method to GET

geekido
  • 171
  • 5
  • I've copied your code and change two sections. Would you please share your code using pastebin or something – geekido Dec 23 '14 at 18:58