Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.