Jump to content

Edit a record


wooowooo

Recommended Posts

Hi

 

I am trying to create a page which will take session information and allow the user to update there record in the mysql database my friend gave me the following code but it wont work, anyone got any ideas whats wrong with it?

 

Many thanks

 

// Connect to server and select databse.
mysql_connect("$host", "$user", "$pass")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// ***** This part will process when you Click on "Submit" button ***** 
// Check, if you clicked "Submit" button 
if($_POST['Submit']){

// Get parameters from form. 
$username=$_POST['username'];
$forname=$_POST['forname'];
$password=$_POST['password'];


// Do update statement. 
mysql_query("update week9 set username='$username', forname='$forname', password='$password'");

// Close database connection. 
mysql_close();
}
?>

<!-- END OF PHP CODES AND START HTML TAGS -->

<!-- set this form to POST method and target this form to itself ($PHP_SELF;)--> 
<form id="form1" name="form1" method="post" action="<? echo $PHP_SELF; ?>">
<p>Username : 
<!-- name of this text field is "name" -->
<input name="name" type="text" id="username" value="<? echo $row['username']; ?>"/>
<br />
Forname : 
<!-- name of this text field is "email" -->
<input name="email" type="text" id="forname" value="<? echo $row['forname']; ?>"/>
<br />
Password: 
<!-- name of this text field is "tel" -->
<input name="tel" type="text" id="password" value="<? echo $row['password']; ?>"/>
</p>
<p>
<input type="submit" name="Submit" value="Submit" />
</p>
</form> 
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/83000-edit-a-record/
Share on other sites

The first thing that stuck out to me was you're not specifying what record you want to update.

 

You have:

 

// Do update statement. 
mysql_query("update week9 set username='$username', forname='$forname', password='$password'");

 

Should be something like:

 

// Do update statement. 
mysql_query("UPDATE `week9` SET `username`= '$username', `forname` = '$forname', `password` = '$password' WHERE `id` = '$id' ");

 

Fix that first then post any errors.

Link to comment
https://forums.phpfreaks.com/topic/83000-edit-a-record/#findComment-422183
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.