0

I have a weird problem in which if I delete the line Type doctor name <input type="text" name="new_Doctor_name" value="<?php echo $row1[3]; ?>" ><br />, I cannot update my records and get the notice Undefined variable: row1. However, if I keep this line, which I copy from another table, I can update just fine.

Please explain this. Any help will be highly appreciated.

<?php

include_once('Connect.php');
            if( isset($_GET['edit1']) )
            {
                $id = $_GET['edit1'];
                $res1= mysql_query("SELECT * FROM department WHERE Dept_name='$id'");
                $row1= mysql_fetch_array($res1);
            }

            if( isset($_POST['new_Doctor_name']) )
            {
                $id                 = $_POST['id'];
                $new_Dept_name      = $_POST['new_Dept_name'];
                $new_Ward           = $_POST['new_Ward'];
                $sql1                = "UPDATE department SET Dept_name='$new_Dept_name', Ward='$new_Ward' WHERE Dept_id='$id'";
                $res2                = mysql_query($sql1) or die("Could not Update".mysql_error());
                echo "<meta http-equiv='refresh' content='0;url=Department_viewtable.php'>";
            }   
            var_dump($row1);
?>

<FORM ACTION="Department_dmod.php" METHOD="post"> 
<input type="hidden" name="id" value="<?php echo $id; ?>" />
***Type doctor name <input type="text" name="new_Doctor_name" value="<?php echo $row1[3]; ?>" ><br />***
Type Department Name <input type="text" name="new_Dept_name" value="<?php echo $row1[1]; ?>" ><br />
Type Department Ward <input type="text" name="new_Ward" value="<?php echo $row1[2]; ?>" >
<INPUT TYPE="SUBMIT" NAME="UPDATE" VALUE="UPDATE"> 
<p><a href=Department_viewtable.php>Back to the Department table</a></p>
<p><a href=Main_Menu.php>Back to Main menu</a></p>
</FORM>
2
  • is your $_GET['edit1'] set? seems like php isn't entering this if: if( isset($_GET['edit1']) ) try to echo something inside it see if it shows. Another possible issue, is you using $row[1] $row[2] and $row[3] at the place of $row[0]` $row[1] and $row[2]. Keep in mind arrays start with index 0 Commented Apr 29, 2014 at 10:50
  • if( isset($_POST['new_Doctor_name']) ) requires input field with name new_Doctor_name to be present. If you delete this text field, this IF will not be true Commented Apr 29, 2014 at 10:51

2 Answers 2

1

The if() statement :

if( isset($_POST['new_Doctor_name']) )

Will only ever be executed if an input element exists in the POST data with a name of new_Doctor_name. If you remove it from the DOM, it will not be passed with the request, and thus the queries won't execute.

It may be better to check for the presence of the UPDATE variable inside the POST request:

if(isset($_POST['UPDATE']))
{
    $id                 = $_POST['id'];
    $new_Dept_name      = $_POST['new_Dept_name'];
    $new_Ward           = $_POST['new_Ward'];
    $sql1                = "UPDATE department SET Dept_name='$new_Dept_name', Ward='$new_Ward' WHERE Dept_id='$id'";
    $res2                = mysql_query($sql1) or die("Could not Update".mysql_error());
    echo "<meta http-equiv='refresh' content='0;url=Department_viewtable.php'>";
}

It's also worth noting that the mysql_* family of functions is now deprecated. Instead, you should look at MySQLi or PDO. Finally, your code is open to SQL injection, so I'd recommend looking at Prepared Statements, too.

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

1 Comment

Glad to help. You might like to accept the answer if it helped.
0

The variable row1 is set in this part of the code. If the variable is returning an error that it has not been defined this means that the code below has not been executed. This code is only ran if the $_GET['edit1'] variable is set.

if( isset($_GET['edit1']) )
        {
            $id = $_GET['edit1'];
            $res1= mysql_query("SELECT * FROM department WHERE Dept_name='$id'");
            $row1= mysql_fetch_array($res1);
        }

5 Comments

This isn't the issue here. The undefined index notice doesn't explain why the update doesn't work. It's likely that edit1 is passed to the page with the form on, and then used to output the form's hidden fields.
@BenM the OP says, it is undefined variable $row not undefined index
@BenM It explains the cause of the Undefined variable: row1 however.
@CodeBird typo on my part there, sorry. Anyway, the fact that if he adds the field back in, the code works, indicates it's not an issue with his GET statement check.
@BenM yeah right, just saying that the answer is still a bit logical :) I suspect the OP is using indices 1,2,3 for $row at the place of 0,1,2

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.