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
Member Avatar for LastMitch

@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 LastMitch

@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 diafol

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 diafol
$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 diafol

OK, solved?

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

Member Avatar for diafol

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.