Updata并从表中删除

时间:2014-01-23 10:11:39

标签: php

当我点击删除页面得到刷新但数据没有被删除你能告诉我是什么问题。我也应该如何编辑数据使用edit.help我在这...我尽我所能。

这是我的删除页面

<?php
        $database = "example";
        $conn = mysql_connect("localhost","root","root");
        $db_found = mysql_select_db($database, $conn);
        $id=$_REQUEST['ID'];
            // sending query
            mysql_query("DELETE FROM my WHERE ID = '$id'")
            or die(mysql_error());      

            header("Location: main.php");
        ?>

这是我的主页

<!DOCTYPE html>
    <html>
    <head>
    <title>Employee</title>
    </head>
    <body>
    <center><b><font size=20>Employee Detail</font></b></center>
    <?php
    $database = "example";
    $conn = mysql_connect("localhost","root","root");
    $db_found = mysql_select_db($database, $conn);
    $SQL = "SELECT * FROM my";
    $result = mysql_query($SQL);
            print "<table border='1'>";
            print "<tr>";
            print "<th>ID</th>";
            print "<th>First name</th>";
            print "<th>Last name</th>";
            print "<th>Gender</th>";
            print "<th>Address</th>";
            print "<th>Contact_no</th>";
            print "<th>Picture</th>";
            print "<th>User_name</th>";
            print "<th>Password</th>";
            print "<th>Email_id</th>";
        while ( $db_field = mysql_fetch_assoc($result) ) 
        {

            print "<tr>";
            print "<td>".$db_field['ID']."</td>";
            print "<td>".$db_field['F_name']."</td>";
            print "<td>".$db_field['L_name']."</td>";
            print "<td>".$db_field['Gender']."</td>";
            print "<td>".$db_field['Address']."</td>";
            print "<td>".$db_field['Contact_no']."</td>";
            print "<td>".$db_field['Picture']."</td>";
            print "<td>".$db_field['U_name']."</td>";
            print "<td>".$db_field['Password']."</td>";
            print "<td>".$db_field['Email_id']."</td>";
            echo"<td> <a href ='edit.php?ID=$id'>Edit</a>";
            echo"<td> <a href ='delete.php?ID=$id'><center>Delete</center></a>";
            print "</tr>";

        }
            print "</table>";

        mysql_close($conn);
    ?>
    <form>
    <a href="test1.php">
    <input type="button" value="Add">
    </a>
    </form>
    </body>
    </html>

这是我的添加页面

<?php

?>

<html>
<head>
<title>Sign up Form</title>
<script type="text/javascript">
<!--
function validation()
{
    if (document.login.fname.value==null || document.login.fname.value=="")
    {
        alert("First name must be filled out");
        document.login.fname.focus();
        return false;
    }
    if((document.login.fname.value.length<3))
    {
        alert("First name is too short");
        document.login.psw.focus();
        return false;
    }
    if (document.login.lname.value==null || document.login.lname.value=="")
    {
        alert("Last name must be filled out");
        document.login.lname.focus();
        return false;
    }
    if((document.login.lname.value.length<3))
    {
        alert("Last name is too short");
        document.login.psw.focus();
        return false;
    }
    if( document.login.select.selectedIndex==0)
    {
            alert( "Gender must be filled out" );
        document.login.select.focus();
            return false;
    }
    if (document.login.address.value==null || document.login.address.value=="")
    {
        alert("Address must be filled out");
        document.login.address.focus();
        return false;
    }
    if((document.login.address.value.length < 20))
    {
        alert(" Your address must be 20 characters");       
        document.login.address.select();
        return false;
    }
    if (document.login.contact_no.value==null || document.login.contact_no.value=="")
    {
        alert("Contact number must be filled out");
        document.login.contact_no.focus();
        return false;
    }
    if(isNaN(document.login.contact_no.value))
    {
        alert("You use charecter in contact number");
        document.login.contact_no.focus();
        return false;
    }
    if((document.login.contact_no.value.length < 1) || (document.login.contact_no.value.length > 10))
    {
        alert("you enter more than 10 digit in contact");
        document.login.contact_no.focus();
        return false;
    }
    if (document.login.picture.value==null || document.login.picture.value=="")
    {
        alert("You must select an Image or Images");
        document.login.picture.focus();
        return false;
    }
    if (document.login.uname.value==null || document.login.uname.value=="")
    {
        alert("Login name must be filled out");
        document.login.uname.focus();
        return false;
    }

    if((document.login.psw.value.length<4))
    {
        alert("Password is too short");
        document.login.psw.focus();
        return false;
    }
    if (document.login.psw.value==null || document.login.uname.value=="")
    {
        alert("Password must be filled out");
        document.login.psw.focus();
        return false;
    }
    var emailfilter=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i
    var b=emailfilter.test(document.login.e_id.value);
    if(b==false)
    {
        alert("Please Enter a valid Mail ID");
        document.login.e_id.focus();
        return false;
    }

}
//-->
</script>
</head>
<body>
<form name="login" action="insert.php" onsubmit="return(validation())" method="post" enctype= multipart/form-data>
<table>
<tr>
<td>First Name:</td>
<td><input type="text" name="fname" /></td>
</tr>

<tr>
<td>Last Name:</td>
<td><input type="text" name="lname" /></td>
</tr>

<tr>
<td>Gender:</td>
<td><select name="select">
   <option value="-1" selected>[Select option]</option>
   <option value="male">Male</option>
   <option value="female">Female</option>
</td>
</tr>

<tr>
<td>Address:</td>
<td><textarea name="address" col="60" row="10"></textarea></td>
</tr>

<tr>
<td>Contact no:</td>
<td><input type="number" name="contact_no"></td>
</tr>

<tr>
<td>Picture:</td>
<td><input type="file" name="picture"> </td>
</tr>

<tr>
<td>User name:</td>
<td><input type="text" name="uname"></td>
</tr>

<tr>
<td>Password:</td>
<td><input type="password" name="psw"></td>
</tr>

<tr>
<td>Email id:</td>
<td><input type="email" name="e_id"></td>
</tr>

<tr>
<td><input type="reset" value="Reset"></td>
<td><input type="submit" name="submit"></td>
</tr>
</table>
</form>
</body>
</html>

这是我的编辑页面

<?php
$database = "example";
$conn = mysql_connect("localhost","root","root");
$db_found = mysql_select_db($database, $conn);
$id =$_REQUEST['ID'];

$result = mysql_query("SELECT * FROM my WHERE ID = '$id'");
$db_field = mysql_fetch_array($result);
if (!$result) 
        {
        die("Error: Data not found..");
        }
            $F_name=$db_field['F_name'];
            $L_name=$db_field['L_name'];
            $Gender=$db_field['Gender'];
            $Address=$db_field['Address'];
            $Contact_no=$db_field['Contact_no'];
            $Picture=$db_field['Picture'];
            $U_name=$db_field['U_name'];
            $Password=$db_field['Password'];
            $Email_id=$db_field['Email_id'];

if(isset($_POST['save']))
{   
    $fname_save = $_POST['fname'];
    $lname_save = $_POST['lname'];
    $gender_save = $_POST['select'];
    $address_save = $_POST['address'];
    $contactno_save = $_POST['contact_no'];
    $picture_save = $_POST['picture'];
    $uname_save = $_POST['u_name'];
    $password_save = $_POST['psw'];
    $emailid_save = $_POST['e_id'];

    mysql_query("UPDATE my SET F_name='$fname_save', L_name='$lname_save', Gender='$gender_save', Address='$address_save',               Contact_no='$contactno_save', Picture='$picture_save', U_name='$uname_save', Password='$password_save',                 Email_id='$emailid_save' WHERE ID = '$id'")
                or die(mysql_error()); 
    echo "Saved!";

    header("Location: main.php");           
}
mysql_close($conn);
?>
</head>
<body>
<form method="post">
<table>
    <tr>
        <td>First Name:</td>
        <td><input type="text" name="fname" value="<?php echo $F_name ?>" /></td>
    </tr>

    <tr>
        <td>Last Name:</td>
        <td><input type="text" name="lname" value="<?php echo $L_name ?>" /></td>
    </tr>

    <tr>
        <td>Gender:</td>
        <td><select name="select" value="<?php echo $Gender ?>">
        <option value="-1" selected>[Select option]</option>
        <option value="male">Male</option>
        <option value="female">Female</option>
        </td>
    </tr>

    <tr>
        <td>Address:</td>
        <td><textarea name="address" col="60" row="10" value="<?php echo $Address ?>"></textarea></td>
    </tr>

    <tr>
        <td>Contact no:</td>
        <td><input type="number" name="contact_no" value="<?php echo $Contact_no ?>"></td>
    </tr>

    <tr>
        <td>Picture:</td>
        <td><input type="file" name="picture" value="<?php echo $Picture ?>"> </td>
    </tr>

    <tr>
        <td>User name:</td>
        <td><input type="text" name="uname" value="<?php echo $U_name ?>"></td>
    </tr>

    <tr>
        <td>Password:</td>
        <td><input type="password" name="psw" value="<?php echo $Password ?>"></td>
    </tr>

    <tr>
        <td>Email id:</td>
        <td><input type="email" name="e_id" value="<?php echo $Email_id ?>"></td>
    </tr>

    <tr>
        <td><input type="submit" name="save" value="save"></td>
    </tr>
</table>
</form>
</body>
</html>

这是我的数据库

<?php
// Create connection
$conn = mysql_connect('localhost', 'root', 'root');
     if (!$conn)
        {
     die('Could not connect: ' . mysql_error());
    }
    echo 'Connected Successfully';
    $sql = "CREATE TABLE my(
        ID INT NOT NULL AUTO_INCREMENT,
        F_name VARCHAR(20) NOT NULL,
        L_name VARCHAR(20) NOT NULL,
        Gender VARCHAR(10) NOT NULL,
    Address VARCHAR(80) NOT NULL,
    Contact_no INT NOT NULL,
    Picture BLOB NOT NULL,
    U_name VARCHAR(20) NOT NULL,
    Password VARCHAR(25) NOT NULL,
    Email_id VARCHAR(30) NOT NULL,
        primary key ( ID ))";

mysql_select_db('example');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not create table: ' . mysql_error());
}
echo "Table my created successfully\n";
    mysql_close($conn);
?> 
echo ("<td><a href=\"delete.php?id=$row[employees_number]\">Delete</a></td></tr>")

1 个答案:

答案 0 :(得分:0)

添加:

$id = $db_field['ID'];

到主页面上while循环的开头。您在echo语句的网址中使用此变量,但从未设置过它。