It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Board index Programming PHP and MySQL Forum

deleting records in php

Moderator: Malcolm

deleting records in php

Postby ravi9510 on Thu Aug 18, 2011 2:07 pm

hi all,
i have created one form for inserting and deletion.
my database name is "test" table name is "emp" which contains three fields
namely empno,empname,desig.
i have inserted some 4 to 5 values for the above table.
i have not made emp no as primary key.
i have entered random number for empno.and some values for empname and desig.
below is for insert is "new1.php"
Code: Select all
<?php
function checkform($empno,$empname,$desig,$error)
{
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>New Record</title>
</head>
<body>
<?php
// if there are any errors, display them
if ($error!= '')
{
echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
}
?>
<form action="" method="post">
<div>
<strong>EmpNo: </strong> <input type="text" name="empno" value="<?php echo $empno; ?>" /><br/>
<strong>EmpName: </strong> <input type="text" name="empname" value="<?php echo $empname; ?>" /><br/>
<strong>Desig: </strong> <input type="text" name="desig" value="<?php echo $desig; ?>" /><br/>
<input type="submit" name="submit" value="Submit">
</div>
</form>
</body>
</html>
<?php
}
  include('connect_db1.php');
  if (isset($_POST['submit']))
{
// get form data, making sure it is valid
$empno =mysql_real_escape_string($_POST['empno']);
$empname = mysql_real_escape_string($_POST['empname']);
$desig = mysql_real_escape_string($_POST['desig']);
 
if ($empno == '' || $empname == '' || $desig == '')
{
// generate error message
$error = 'ERROR: Please fill in all required fields!';

// if either field is blank, display the form again
checkform($empno,$empname,$desig,$error);
}
else
{
  mysql_query("INSERT emp SET empno='$empno', empname='$empname',desig='$desig'")
or die(mysql_error());
  header("Location: view1.php");
}
}
else
  {
checkform('','','','');
}
?>

below is "connect_db1.php"
Code: Select all
<?php
$connection = mysql_connect("localhost","root","")
or die ("Could not connect to server ... \n" . mysql_error());
mysql_select_db("test")
or die ("Could not connect to database ... \n" . mysql_error());
?>

i am unable to delete the records .
kindly tell me how to delete the records in my table.this is with out autoincrement.
below is my "delete1.php"
Code: Select all
<?php
// connect to the database
include('connect_db1.php');
if (isset($_POST['empno'])) /*&& is_numeric($_POST['empno'])) */
{
$empno = $_POST['empno'];
$result = mysql_query("DELETE FROM emp WHERE empno=$empno")
or die(mysql_error());
header("Location:view1.php");
}
else
{
header("Location:view1.php");
}
?>

i have also have "view1.php"
Code: Select all
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>View Records</title>
</head>
<body>

<?php
include('connect_db1.php');
$result = mysql_query("SELECT * FROM emp") or die(mysql_error()); 
echo "<table border='1' cellpadding='10'>";
echo "<tr> <th>EmpNo</th> <th>EmpName</th> <th>Desig</th> <th></th> </tr>";
// loop through results of database query, displaying them in the table
while($row = mysql_fetch_array($result))
{       
// echo out the contents of each row into a table
    echo "<tr>";
    echo '<td>' . $row['empno'] . '</td>';
    echo '<td>' . $row['empname'] . '</td>';
    echo '<td>' . $row['desig'] . '</td>';
    echo '<td><a href="delete1.php?empno=' . $row['empno'] . '">Delete</a></td>';
    echo "</tr>";
}
    // close table>
    echo "</table>";
?>
<p><a href="new1.php">Add a new record</a></p>
</body>
</html>

please tell me how to delete the records........
ravi9510
 
Posts: 11
Joined: Tue Aug 09, 2011 12:42 pm

Who is online

Users browsing this forum: No registered users and 0 guests