Hey all.

Ok this is so basic but confused. I'm updating the student name in this instance. I'm able to extract the name out of the database, but I cannot update. Any hints as to where i'm going wrong?

<?php
$dbhost = 'localhost';
$dbuser = 'nbsstud1_test1';
$dbpass = 'demon8';
$dbname = 'nbsstud1_testdb';

mysql_connect($dbhost,$dbuser,$dbpass);
mysql_select_db($dbname);


$msg = "&nbsp;";
if(!empty($_POST["update"]))
{
 mysql_query("UPDATE students SET firstname='".$_POST["firstname"]."' WHERE student_id ='".$_POST["student_id"]."'");
 $msg = "Record # ".$_POST["student_id"]." has been successfully updated.";
 }
$result = mysql_query("SELECT * FROM students ORDER BY 'student_id', 'firstname'");
echo "<table width='100%' border='0' cellspacing='1' cellpadding='1'>
       <tr>
        <th>Student Id</th>
        <th>Firstname</th>
       </tr>";
while($Update = mysql_fetch_assoc($result))
{
 echo "<tr>
        <form method='post' action='edit.php'>
        <td align='center'><input type='text' name='student_id' value='{$Update['student_id']}' readonly style='border:none;background:none;text-align:center;' /></td>
        <td align='center'><input type='text' name='firstname' value='{$Update['firstname']}' /></td>
        <td colspan='4' align='center'><input type='submit' name='update' value='Update' /></td>
        </form>
       </tr>";
 }
echo "</table><center><div style='width:450px;text-align:center;font-size:20px;font-weight:bold;color:#00f;margin-top:20px;border:1px solid #000;'>$msg</div></center>";
?>

Echo out your query as follows

if(!empty($_POST["update"]))
{
echo "UPDATE students SET firstname='".$_POST["firstname"]."' WHERE student_id ='".$_POST["student_id"]."'";
mysql_query("UPDATE students SET firstname='".$_POST["firstname"]."' WHERE student_id ='".$_POST["student_id"]."'");
$msg = "Record # ".$_POST["student_id"]." has been successfully updated.";
}

Copy the printed query and paste it in the sql sectin of phpmyadmin and see the result.

Hmm I see the logic in that, but its not editing anything in phpmyadmin. How do I solve that?

Read this thread on how to include error checking in your code, so you can track if something fails.

Hmm I see the logic in that, but its not editing anything in phpmyadmin. How do I solve that?

did the query return any error in phpmyadmin?

Thanks pritaeas

Karthik - basically its still only pulling out and displaying data that is already in the database, its not updating the new information im entering through the form!

Of course not, for that you need an INSERT INTO query... (or is that not what you mean?)

Disregard previous comment... Add the error checking first, it will provide you with more information about why it fails.

Pritaeas - i've already inserted into the database, so I at this moment I only need to update that entry and then see the changes.

So if for example i'm changing a surname, its not editing it in phpmyadmin.

But i'll try ur suggestion and opt for error checking.

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.