0

I am getting the data from db and updating it here and trying to sending back to db. The error is undefined variable row.I am accessing with index values instead of their names.

<!DOCTYPE HTML>
<?php
    include_once('DBConnection.php');

    if( isset($_GET['edit']) )
    {
        $id = $_GET['edit'];
        $res= mysql_query("SELECT * FROM details WHERE id='$id'");
        $row= mysql_fetch_array($res);
    }

    if( !empty($_POST['newName'])&& isset($_POST['newName'])&& !empty($_POST['email'])&& isset($_POST['email'])&&!empty($_POST['phonenumber'])&& isset($_POST['phonenumber']))
    {
        $newName = $_POST['newName'];
        $EMail = $_POST['email'];
        $PhoneNumber = $_POST['phonenumber'];
        $id      = $_POST['id'];
        $sql     = "UPDATE details SET name='$newName' email='$EMail' phonenumber='$PhoneNumber' WHERE id='$id'";
        $res     = mysql_query($sql) 
                                    or die("Could not update".mysql_error());
        echo "<meta http-equiv='refresh' content='0;url=random.php'>";
    }

?>
<form action="edit.php" method="POST">
PhoneNumber: <input type="text" name="phonenumber" value="<?php echo $row[3];?>"/><br />
EMail: <input type="text" name="email" value="<?php echo $row[2];?>"/><br />
Name: <input type="text" name="newName" value="<?php echo $row[1];?>"/><br />
SlNo<input type="hidden" name="SlNo" value="<?php echo $row[0];?>"/>
<input type="submit" value=" Update "/>
</form>
RacingKid
  • 15
  • 5

3 Answers3

0

You haven't done a SELECT query and executed it in your second if. There is no $row variable defined, yet you talk about it in your form.

delboy1978uk
  • 12,118
  • 2
  • 21
  • 39
0

In this instance, you need to be sure $row is defined before using it. Try this:

<!DOCTYPE HTML>
<?php
    include_once('DBConnection.php');

    if( isset($_GET['edit']) )
    {
        $id = $_GET['edit'];
        $res= mysql_query("SELECT * FROM details WHERE id='$id'");
        $row= mysql_fetch_array($res);
    }

    if( !empty($_POST['newName'])&& isset($_POST['newName'])&& !empty($_POST['email'])&& isset($_POST['email'])&&!empty($_POST['phonenumber'])&& isset($_POST['phonenumber']))
    {
        $newName = $_POST['newName'];
        $EMail = $_POST['email'];
        $PhoneNumber = $_POST['phonenumber'];
        $id      = $_POST['id'];
        $sql     = "UPDATE details SET name='$newName' email='$EMail' phonenumber='$PhoneNumber' WHERE id='$id'";
        $res     = mysql_query($sql) 
                                    or die("Could not update".mysql_error());
        echo "<meta http-equiv='refresh' content='0;url=random.php'>";
    }

?>
<form action="edit.php" method="POST">
PhoneNumber: <input type="text" name="phonenumber" value="<?php echo (isset($row)? $row[3]: "");?>"/><br />
EMail: <input type="text" name="email" value="<?php echo (isset($row)? $row[2]: "");?>"/><br />
Name: <input type="text" name="newName" value="<?php echo (isset($row)? $row[1]: "");?>"/><br />
SlNo<input type="hidden" name="SlNo" value="<?php echo (isset($row)? $row[0]: "");?>"/>
<input type="submit" value=" Update "/>
</form>
Elisha Senoo
  • 3,489
  • 2
  • 22
  • 29
0

The query() method shall return FALSE on fail, which is a boolean, which has no rows...

Before trying to access the result rows, check if it is an array :

$res = mysql_query($sql);
if (!is_array($res)) {
    return ('error');
}
$row = mysql_fetch_array($res);

and also make sure $row exists, as Elisha said.

Also to concatenate strings and variables : PHP - concatenate or directly insert variables in string (Note that you method perhaps works, I don't know)

Jean-Marc Zimmer
  • 537
  • 6
  • 20