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>
1
  • 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 <?php echo $row[0]; ?> Commented Dec 23, 2014 at 15:55

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Now getting a blank field
How can i chck wether $row[0] exists?
As you can see in my code, I am calling the isset() function. That syntax, what I've used, a shorter version of if (isset($row[0])) { echo $row[0]; } else { echo ''; }
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.

2 Comments

How can i chck wether $row[0] exists?
@user645 <?php echo (isset($row[0]) ? $row[0] : ''); ?>
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

1 Comment

I've copied your code and change two sections. Would you please share your code using pastebin or something

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.