PHP $ _POST不从表单发送数据

时间:2014-03-10 11:17:12

标签: php forms post

我正在尝试将数据从表单发送到我的数据库,但我似乎无法出于某种原因这样做。我已经尝试了很多东西,我知道所有的故障排除,但我想这是超出我的数据库知识范围的东西 - 只有一两个月。这些是我试图在两者之间发送数据的两个文件,但无济于事。有人可以解释一下发生了什么吗?!非常感谢!

maintenanceReps.php

<html>
<head>
<title>Maintenance : Reps</title>
<style type="text/css">
.style1 {
    margin-top: 114px;
}
.style2 {
    border-width: 0px;
}
.style3 {
    color: #000080;
    font-size: 26pt;
    font-family: Calibri;
}
.auto-style1 {
    text-align: right;
}
.auto-style2 {
    text-align: center;
}
.auto-style3 {
    border: 1px solid #000000;
    background-color: #C0C0C0;
}
</style>
</head>
<body>
<center><a href="http://acadweb1.salisbury.edu/~vg7308/premiereProductsHome.html">
<span class="style3">
<img alt="" src="img2.gif" width="100" height="100" style="float: left" class="style2">    </span></a><span class="style3">Rep Maintenance<hr></span></center></body>
<table align="center" class="auto-style3">
    <tr>
        <td class="auto-style1">Rep Num:</td>
        <td> <input type="text" maxlength="32" name="repnum" value="< =$_POST['REP_NUM']?>"></td>
    </tr>
    <tr>
        <td class="auto-style1">Last Name:</td>
        <td> <input type="text" maxlength="32" name="lastname" value="<?=$_POST['LAST_NAME']?>"></td>
    </tr>
    <tr>
        <td class="auto-style1">First Name:</td>
        <td> <input type="text" maxlength="32" name="firstname" value="<?=$_POST['FIRST_NAME']?>"></td>
    </tr>
    <tr>
        <td class="auto-style1">Street:</td>
        <td> <input type="text" maxlength="32" name="street" value="<?=$_POST['STREET']?>"></td>
    </tr>
    <tr>
        <td class="auto-style1">City</td>
        <td> <input type="text" maxlength="32" name="city"  value="<?=$_POST['CITY']?>"></td>
    </tr>
    <tr>
        <td class="auto-style1">State:</td>
        <td> <input type="text" maxlength="32" name="state"  value="<?=$_POST['STATE']?>"></td>
    </tr>
    <tr>
        <td class="auto-style1">Zip:</td>
        <td> <input type="text" maxlength="32" name="zip" value="<?=$_POST['ZIP']?>"></td>
    </tr>
    <tr>
        <td class="auto-style1">Commission:</td>
        <td> <input type="text" maxlength="32" name="commission" value="<?=$_POST['COMMISSION']?>"></td>
    </tr>
    <tr>
        <td class="auto-style1">Rate:</td>
        <td> <input type="text" maxlength="32" name="rate"  value="<?=$_POST['RATE']?>"></td>
    </tr>
    <tr>
        <td class="auto-style2" colspan="2">

            <?php var_dump($_POST); ?>

            <form action="" method="post" enctype="multipart/form-data">
                <input type = "submit" name ="Insert" value = "Insert" />
                <input type = "submit" name = "Search" value = "Search" />
                <input type = "submit" name = "Modify" value = "Modify" />
                <input type = "submit" name = "Delete" value = "Delete" onclick="return confirm('Are     you sure you want to delete this rep?')" />
            </form>
        </td>
    </tr>
</table>


<?php include "repsAdd.php"; ?>
</body>
</html>

repsAdd.php

<html>
<head>
<title>Maintenance : Reps</title>
</head>
<?php
session_start();


//address error handling

ini_set('display_errors',1);
error_reporting(E_ALL & ~E_NOTICE);
//define the Rep Number php variable name

//attempt to connect

if($connection = @mysql_connect ('localhost', 'username','password')){
//print '<p>Succesfully connected to MySQL.</p>';
}
else{
die('<p>Could not connect to MySQL because: <b>' .mysql_error() .'</b></p>');
}
if (@mysql_select_db("GIULIANA_PREMIERE",$connection)){
//print '<p>The GIULIANA_PREMIERE database has been selected.</p>';
}
else {
die('<p>Could not select the GIULIANA_PREMIERE database because: <b>'.mysql_error().'</b></p>');
}

//output the resulting query table
if(isset($_POST['Insert'])){
if(empty($_REQUEST['repnum']) || empty($_REQUEST['lastname']) || empty($_REQUEST['firstname']) || empty($_REQUEST['street']) || empty($_REQUEST['city']) || empty($_REQUEST['state']) || empty($_REQUEST['zip']) || empty($_REQUEST['commission']) || empty($_REQUEST['rate'])){
    print "There is information missing from your form.";
}
else{
    $repnum = $_POST['repnum'];
    $lastname = $_POST['lastname'];
    $firstname = $_POST['firstname'];
    $street = $_POST['street'];
    $city = $_POST['city'];
    $state = $_POST['state'];
    $zip = $_POST['zip'];
    $commission = $_POST['commission'];
    $rate = $_POST['rate'];

    $query = mysql_query("INSERT INTO REP VALUES ('$repnum', '$lastname', '$firstname', '$street', '$city', '$state', '$zip', '$commission', '$rate')");
    print "Succesfully inserted rep";

    $query2 = "SELECT * FROM REP WHERE REP_NUM = '$repnum'";
    if($r = mysql_query($query2)){
        while ($row = mysql_fetch_array($r)){
            print "{$row['LAST_NAME']}, {$row['FIRST_NAME']}<br>";
            print "{$row['STREET']} {$row['CITY']}, {$row['STATE']} {$row['ZIP']}<br>";
            print "<b>{$row['REP_NUM']}</b> | $ {$row['COMMISSION']} | {$row['RATE']}<br>";
        }
    }
}
}
if(isset($_POST['Modify'])){
    if(empty($_REQUEST['repnum']) || empty($_REQUEST['lastname']) || empty($_REQUEST['firstname']) || empty($_REQUEST['street']) || empty($_REQUEST['city']) || empty($_REQUEST['state']) || empty($_REQUEST['zip'])  || empty($_REQUEST['commission'])  || empty($_REQUEST['rate'])){
    print "There is information missing from your form.";
}
else{   
    $repnum = $_POST['repnum'];
    $lastname = $_POST['lastname'];
    $firstname = $_POST['firstname'];
    $street = $_POST['street'];
    $city = $_POST['city'];
    $state = $_POST['state'];
    $zip = $_POST['zip'];
    $commission = $_POST['commission'];
    $rate = $_POST['rate'];

    $query = mysql_query("UPDATE REP SET REP_NUM = '$repnum',LAST_NAME = '$lastname', FIRST_NAME = '$firstname',STREET = '$street', CITY = '$city', STATE = '$state',ZIP = '$zip', COMMISSION = '$commission',RATE = '$rate' WHERE REP_NUM = '$repnum'");
    print "Succesfully Modified Rep";

    $query2 = "SELECT * FROM REP WHERE REP_NUM = '$repnum'";
    if($r = mysql_query($query2)){
        while ($row = mysql_fetch_array($r)){
            print "{$row['LAST_NAME']}, {$row['FIRST_NAME']}<br>";
            print "{$row['STREET']} {$row['CITY']}, {$row['STATE']} {$row['ZIP']}<br>";
            print "<b>{$row['REP_NUM']}</b> | $ {$row['COMMISSION']} | {$row['RATE']}<br>";
        }
    }
}
}
if(isset($_POST['Delete'])){
if(empty($_REQUEST['repnum'])){
    print "There is information missing in your form.";
}
else{
    $repnum= $_POST['repnum'];
    $query = mysql_query("DELETE FROM REP WHERE REP_NUM = '$repnum'");
    print"Succesfully deleted Rep";
}
}
?>

</body>
</html>

5 个答案:

答案 0 :(得分:0)

所有输入元素都应该在你的表单标签中,使用这个html

<html>
<head>
<title>Maintenance : Reps</title>
<style type="text/css">
.style1 {
    margin-top: 114px;
}
.style2 {
    border-width: 0px;
}
.style3 {
    color: #000080;
    font-size: 26pt;
    font-family: Calibri;
}
.auto-style1 {
    text-align: right;
}
.auto-style2 {
    text-align: center;
}
.auto-style3 {
    border: 1px solid #000000;
    background-color: #C0C0C0;
}
</style>
</head>
<body>
<center>
    <a href="http://acadweb1.salisbury.edu/~vg7308/premiereProductsHome.html"> <span class="style3"> <img alt="" src="img2.gif" width="100" height="100" style="float: left" class="style2"> </span></a><span class="style3">Rep Maintenance
    <hr>
    </span>
</center>
</body>
<?php var_dump($_POST); ?>
<form action="" method="post" enctype="multipart/form-data">
    <table align="center" class="auto-style3">
        <tr>
            <td class="auto-style1">Rep Num:</td>
            <td><input type="text" maxlength="32" name="repnum" value="< =$_POST['REP_NUM']?>"></td>
        </tr>
        <tr>
            <td class="auto-style1">Last Name:</td>
            <td><input type="text" maxlength="32" name="lastname" value="<?=$_POST['LAST_NAME']?>"></td>
        </tr>
        <tr>
            <td class="auto-style1">First Name:</td>
            <td><input type="text" maxlength="32" name="firstname" value="<?=$_POST['FIRST_NAME']?>"></td>
        </tr>
        <tr>
            <td class="auto-style1">Street:</td>
            <td><input type="text" maxlength="32" name="street" value="<?=$_POST['STREET']?>"></td>
        </tr>
        <tr>
            <td class="auto-style1">City</td>
            <td><input type="text" maxlength="32" name="city"  value="<?=$_POST['CITY']?>"></td>
        </tr>
        <tr>
            <td class="auto-style1">State:</td>
            <td><input type="text" maxlength="32" name="state"  value="<?=$_POST['STATE']?>"></td>
        </tr>
        <tr>
            <td class="auto-style1">Zip:</td>
            <td><input type="text" maxlength="32" name="zip" value="<?=$_POST['ZIP']?>"></td>
        </tr>
        <tr>
            <td class="auto-style1">Commission:</td>
            <td><input type="text" maxlength="32" name="commission" value="<?=$_POST['COMMISSION']?>"></td>
        </tr>
        <tr>
            <td class="auto-style1">Rate:</td>
            <td><input type="text" maxlength="32" name="rate"  value="<?=$_POST['RATE']?>"></td>
        </tr>
        <tr>
            <td class="auto-style2" colspan="2"><input type = "submit" name ="Insert" value = "Insert" />
                <input type = "submit" name = "Search" value = "Search" />
                <input type = "submit" name = "Modify" value = "Modify" />
                <input type = "submit" name = "Delete" value = "Delete" onclick="return confirm('Are     you sure you want to delete this rep?')" /></td>
        </tr>
    </table>
</form>
<?php
    include "repsAdd.php";
?>
</body>
</html>

答案 1 :(得分:0)

问题是,您的input个字段位于form开始和结束标记之外。您的表单操作也需要指向repsAdd.php

<form action="repsAdd.php" method="post" enctype="multipart/form-data">
    <!-- All form elements in here -->
</form>

答案 2 :(得分:0)

您的表单输入不在标签内。

基本上你执行一个只有按钮而没有输入的表单

你也可能希望逃避获取php的数据。

答案 3 :(得分:0)

HTML(XML)是一种层次结构,因此您必须将所有相关的<input>元素放在<form>标记中。

问候

答案 4 :(得分:0)

您需要在所有查询中进行以下更改

`REP_NUM` = '$repnum' etc