Jump to content

how do i now edit table?


xcoderx

Recommended Posts

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>

Link to comment
https://forums.phpfreaks.com/topic/202813-how-do-i-now-edit-table/
Share on other sites

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')";
}

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.