user645
user645

Reputation: 37

MySQL Edit records using PHP

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>

Upvotes: 0

Views: 76

Answers (3)

geekido
geekido

Reputation: 171

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

Upvotes: 0

vaso123
vaso123

Reputation: 12391

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.

Upvotes: 3

Kevin
Kevin

Reputation: 862

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.

Upvotes: 1

Related Questions