i've displayed a table from database using php.and now what i want is to edit records seperately as i've placed an edit and a delete link in front of each record which edits the record from sql.and it is now giving this warning.Please help me to find the error correctly.as iam new to php and sql as well..
this is my code when i click on the edit button in front of any row

<?php include("../includes/config.php"); ?>
<?php
 if ($_SESSION["isadmin"])
   {

   $con=mysql_connect($dbserver,$dbusername,$dbpassword);
if (!$con) { die('Could not connect: ' . mysql_error()); }

mysql_select_db($dbname, $con);

$result = mysql_query("SELECT * FROM accounts WHERE id=".$id."");


    while($row = mysql_fetch_array($result))
    {
       $id=$row['id'];
       $firstname = $row['firstname'];
       $lastname = $row['lastname'];
       $email=$row['email'];
       $type=$row['type'];
    }



mysql_close($con);
?>
<!DOCTYPE HTML>
<html>
<head>
<title>Edit Teacher</title>
<link rel="StyleSheet" href="../admin/css/style.css" type="text/css" media="screen">
</head>


<body>
<?php include("../admin/includes/header.php"); ?>
<?php include("../admin/includes/nav.php"); ?>
<?php include("../admin/includes/aside.php"); ?>
<div id="maincontent">

    <div id="breadcrumbs">
        <a href="">Home</a> >
         <a href="">Manage Users</a> >
         <a href="">List Users</a> >
         Edit Teacher
    </div>
    <h2>Edit Teacher</h2>


<?php
if (isset($_GET["status"]))
{
    if ($_GET["status"]==1)
    {
        echo("<strong>Teacher Has been Successfully Edited!</strong>");
    }
}
?>
    <form method="post" action="edit-teacher-action.php">
        <label>ID:</label><input type="text" value="<?php echo $id; ?>" name="id" />
        <label>First Name:</label> <input type="text" value="<?php echo $firstname; ?>" name="Fname" />
        <label>Last Name:</label> <input type="text" value="<?php echo $lastname; ?>" name="Lname" />
        <label>Email:</label> <input type="text" value="<?php echo $email; ?>" name="email" />
        <label>Type:</label>  <input type="text" value="<?php echo $type; ?>" name="type" />

        <input type="submit" value="Edit" />
    </form>



</div>

</body>
<?php include("../admin/includes/footer.php"); ?>
</html>
<?php
   }
    else
    {
        header("Location: ".$fullpath."login/unauthorized.php");

    }
?>

this is the warning it gives

Notice: Undefined variable: id in C:\xampp\htdocs\project\admin\edit-teacher.php on line 11

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\project\admin\edit-teacher.php on line 14

Dani AI

Generated

The two messages in the thread are the same root problem: an undefined $id makes the SELECT invalid, so mysql_query() returns FALSE and mysql_fetch_array() complains about getting a boolean instead of a resource. was right to point at retrieving the id from the request and checking the query result; correctly flagged query/syntax problems with the UPDATE. The safest approach is: validate the incoming id, use modern DB APIs (mysqli or PDO) and prepared statements, and always check for query errors before trying to fetch rows.

A compact, robust pattern (mysqli + prepared SELECT) — replace the old mysql_* flow with something like:

<?php
// $mysqli is an established mysqli connection
$id = filter_input(INPUT_GET, 'id', FILTER_VALIDATE_INT);
if (! $id) { /* handle missing/invalid id */ }

$stmt = $mysqli->prepare('SELECT id, firstname, lastname, email, type FROM accounts WHERE id = ?');
$stmt->bind_param('i', $id);
$stmt->execute();
$result = $stmt->get_result();
$row = $result ? $result->fetch_assoc() : null;
$stmt->close();

if ($row) {
    $firstname = $row['firstname'];
    // ...
} else {
    /* no record found or query error — inspect $mysqli->error */
}
?>

For the UPDATE, use a prepared statement too (bind parameters and check affectedrows), which avoids the quoting/concatenation mistakes that caused the parse error seen earlier. When debugging the original mysql* code, always print the SQL error (mysql_error or $mysqli->error) as soon as a query returns FALSE — that reveals syntax problems or missing/empty variables immediately.

Notes and cautions: never trust raw request data; validate or cast id to an integer; prefer prepared statements to stop SQL injection; the mysql_* extension is deprecated and removed in newer PHP, so migrating to mysqli or PDO avoids future runtime failures. Also confirm the edit link actually passes id (e.g. edit-teacher.php?id=123) so the initial SELECT has a valid value to work with.

Recommended Answers

All 11 Replies

Member Avatar for Member #949455

@Riu 2009

line 11 you need to used the $_GET[] function

line 14 you check errors:

if (!$result) { 
    die('Invalid query: ' . mysql_error());
}

how would i use $_GET[]function...i've used the POST function in action-edit which is as follows..

<?php include("../includes/config.php");?>
<?php
$id=$_POST["id"];
$Fname=$_POST["Fname"];
$Lname=$_POST["Lname"];
$email=$_POST["email"];
$type=$_POST["type"];

$con=mysql_connect($dbserver,$dbusername,$dbpassword);
if (!$con) { die('Could not connect: ' . mysql_error()); }


 mysql_select_db($dbname, $con);
query=("UPDATE accounts SET firstname='$Fname' , lastname='$Lname', email='$email', type='$type' WHERE id=."$id"");
mysql_query($query);
header("Location: edit-teacher.php?status=1");
mysql_close($con);
?>

and is my query on line 14 wrong???

Member Avatar for Member #949455

@Riu 2009

Is this part of the code you first post above?

This is the code you had for the first post above:

$result = mysql_query("SELECT * FROM accounts WHERE id=".$id."");
while($row = mysql_fetch_array($result))
{
$id=$row['id'];
$firstname = $row['firstname'];
$lastname = $row['lastname'];
$email=$row['email'];
$type=$row['type'];
}

I didn't see this:

$id=$_POST["id"];
$Fname=$_POST["Fname"];
$Lname=$_POST["Lname"];
$email=$_POST["email"];
$type=$_POST["type"];

nor this:

query=("UPDATE accounts SET firstname='$Fname' , lastname='$Lname', email='$email', type='$type' WHERE id=."$id"");

I mean right now bit confuse what are you asking?

Member Avatar for Member #120589

You've got unescaped double quote:

query=("UPDATE accounts SET firstname='$Fname' , lastname='$Lname', email='$email', type='$type' WHERE id=."$id"");
mysql_query($query);

Try this:

$query= "UPDATE accounts SET firstname='$Fname' , lastname='$Lname', email='$email', type='$type' WHERE id=$id";
$r = mysql_query($query);
if(mysql_affected_rows()>0){
    //success
}else{
    //fail
}

@diafol i've changed the query how u told now its again giving error

Parse error: syntax error, unexpected '=' in C:\xampp\htdocs\project\admin\edit-teacher-action.php on line 14

and the error i above mentioned is from the code i posted first...i am sorry i know its a bit confusing but i need to get the work done very soon but i couldnot find any solution to these errors.

@lastMitch the above code which i posted first works when i press the link in row <td>EDIT</td> to edit the specific record...the other one that i posted afterwards works when i change that specific record and click on the Edit button to save that record in database...

if i use

$result = mysql_query("SELECT * FROM accounts WHERE  (id=".$_SESSION['id'].")"); 

then it dont gives any error but shows only record from the first row in database..

but when i use this

$result = mysql_query("SELECT * FROM accounts WHERE id=$id; 

it gives following error

mysql_fetch_array() expects parameter 1 to be resource, boolean given.

now why i changed it to WHERE id=$id is because i want to edit only the row whose id is equal to the id shown in the html table..i think u will get my point now... i hope

Member Avatar for Member #120589
$result = mysql_query("SELECT * FROM accounts WHERE id=".$id."");
while($row = mysql_fetch_array($result))
{
   $id=$row['id'];
   $firstname = $row['firstname'];
   $lastname = $row['lastname'];
   $email=$row['email'];
   $type=$row['type'];
}

Try:

$result = mysql_query("SELECT * FROM accounts WHERE id= $id");
while($row = mysql_fetch_array($result))
{
   $id=$row['id'];
   $firstname = $row['firstname'];
   $lastname = $row['lastname'];
   $email=$row['email'];
   $type=$row['type'];
}

NOTE the closing bracket before the semi colon for the first line.
If that gives you an error (resource error on mysql_fetch_array), perhaps $id is not set to what you expect. Is accounts the correct name of the table?

got the work done

Member Avatar for Member #120589

OK, solved?

yup solved but used other ways.. anyways thankyou very much for the support :)

Member Avatar for Member #120589

OK, mark thread as solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.