xcoderx Posted May 25, 2010 Share Posted May 25, 2010 ok the below code i use to insert data into db. now how do i do a editing with the data i have stored already? <html> <head> <title>Insert Data</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> <?php if(isset($_POST['insert'])) { if(! $conn=mysql_connect("localhost", "root", "demo")) { die("Could't connect to database server.."); } mysql_select_db("online_application",$conn) or die(mysql_error()); $no_error=1; $user_name = trim($_POST['user_name']); if($user_name =="") { $no_error=0; } $password = md5(trim($_POST['password'])); $f_name = $_POST['f_name']; $l_name = $_POST['l_name']; $email = $_POST['email']; $date = $_POST['dob']; $query = "INSERT INTO users (user_name, password, f_name, l_name, email, date) VALUES ('$user_name','$password', '$f_name', '$l_name', '$email', '$date')"; $result= mysql_query($query) or die('Error, insert query failed'); echo "Data Inserted"; if($result){ header("Location:index.php"); } } else { ?> <form method="POST"> <table width="400" border="0" cellspacing="1" cellpadding="2"> <tr> <td colspan="2"><?php if(isset($no_error) && $no_error==0) print "Error in inserted data";?></td> </tr> <tr> <td width="100">Username</td> <td><input name="user_name" type="text" id="user_name"></td> </tr> <tr> <td width="100">Password</td> <td><input name="password" type="password" id="password"></td> </tr> <tr> <td width="100">First Name</td> <td><input name="f_name" type="text" id="f_name"></td> </tr> <tr> <td width="100">Last Name</td> <td><input name="l_name" type="text" id="l_name"></td> </tr> <tr> <td width="100">Email</td> <td><input name="email" type="text" id="email"></td> </tr> <tr> <td width="100">DOB</td> <td><input name="dob" type="text" id="dob"></td> </tr> <td width="100"> </td> <td> </td> </tr> <tr> <td width="100"> </td> <td><input name="insert" type="submit" id="insert" value="Insert Data"></td> </tr> </table> </form> <?php } ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/202813-how-do-i-now-edit-table/ Share on other sites More sharing options...
MadTechie Posted May 25, 2010 Share Posted May 25, 2010 Using the UPDATE! just echo the record values in to the form along with the record prime id (put in a hidden field later), then when you submit the form if the ID is set then use update instead of insert, ie if(empty($_POST['ID'])){ $ID = (int)$_POST['ID']; $query = "UPDATE users SET user_name = '$user_name', password='$password', etc etc etc WHERE ID=$ID"; }else{ $query = "INSERT INTO users (user_name, password, f_name, l_name, email, date) VALUES ('$user_name','$password', '$f_name', '$l_name', '$email', '$date')"; } Quote Link to comment https://forums.phpfreaks.com/topic/202813-how-do-i-now-edit-table/#findComment-1062916 Share on other sites More sharing options...
xcoderx Posted May 25, 2010 Author Share Posted May 25, 2010 thanks bro got it :-) Quote Link to comment https://forums.phpfreaks.com/topic/202813-how-do-i-now-edit-table/#findComment-1062934 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.