954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Updating records in database

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>";
?>
demon8
Newbie Poster
4 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

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.

Karthik_pranas
Posting Pro
564 posts since Feb 2011
Reputation Points: 96
Solved Threads: 97
 

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

demon8
Newbie Poster
4 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

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

pritaeas
Posting Expert
Moderator
5,480 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 
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?

Karthik_pranas
Posting Pro
564 posts since Feb 2011
Reputation Points: 96
Solved Threads: 97
 

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!

demon8
Newbie Poster
4 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

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
Posting Expert
Moderator
5,480 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 

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.

demon8
Newbie Poster
4 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You