表单提交时未获得预期值

时间:2015-05-07 21:44:38

标签: php html forms

我有一个表单,我要求用户输入一些值。然后,使用PHP,我正在尝试处理用户输入的值。

我面临的问题是,在用户提交表单后,我的所有值都设置为1,而不是用户输入的值。

以下是表格:

<form method="post" action="checkOutResult.php">
    <p> Transaction #:
        <input type="text" name="transactionNum" placeholder="Input #">
    </p>
    <table>
        <tr>
            <td>Student ID</td>
            <td>Employee ID</td>
            <td>Professor ID</td>
        </tr>
        <tr>
            <td>
                <input type="text" name="studentID" placeholder="7 Digit Student ID">
            </td>
            <td>
                <input type="text" name="empID" placeholder="7 Digit Employee ID">
            </td>
            <td>
                <input type="text" name="profID" placeholder="7 Digit Professor ID">
            </td>

        </tr>
    </table>
    <table>
        <tr>
            <td>Product ID</td>
        </tr>
        <tr>
            <td>
                <input type="text" name="productID" placeholder="Scan Barcode">
            </td>
        </tr>
    </table>

    <center>
        <table>
            <tr>
                <iframe src="http://free.timeanddate.com/clock/i4nrjjak/n179/fn10/fs24/fcb40431/ftb/bas2/bat1/bacfff/pa8/tt0/tw1/td2/th2/tb4" frameborder="0" width="334" height="74"></iframe>

                <td>CheckOut Date</td>
                <td>CheckOut Time</td>
            </tr>
            <tr>
                <td>
                    <input type="text" name="checkoutDate" placeholder="YYYY-MM-DD">
                </td>
                <td>
                    <input type="text" name="checkoutTime" placeholder="HH:MM:SS">
                </td>
            </tr>
        </table>
    </center>

    <center>
        <table>
            <tr>
                <td>Due Date</td>
                <td>Due Time</td>
            </tr>
            <tr>
                <td>
                    <input type="text" name="dueDate" placeholder="YYYY-MM-DD">
                </td>
                <td>
                    <input type="text" name="dueTime" placeholder="HH:MM:SS">
                </td>
            </tr>
        </table>
    </center>

    <input type="submit" name="Submit" value="submit">
</form>        

这是在表单提交时执行的PHP代码。

<?php

include ("thesis.php");

$transactionNum = isset($_POST['transactionNum']);
$studentID = isset($_POST['studentID']);
$empID = isset($_POST['empID']);
$profID = isset($_POST['profID']);
$productID = isset($_POST['productID']);
$checkoutDate = isset($_POST['checkoutDate']);
$checkoutTime = isset($_POST['checkoutTime']);
$dueDate = isset($_POST['dueDate']);
$dueTime = isset($_POST['dueTime']);

$query = "INSERT INTO Transactions(transactionNum, studentID, empID, profID, productID, checkoutDate, checkoutTime, dueDate, dueTime)
    VALUES ('$transactionNum', '$studentID', '$empID', '$profID', '$productID', '$checkoutDate', '$checkoutTime', '$dueDate', '$dueTime')";

if($link->query($query) === TRUE){
    echo "New Record Created Successfully. ";
    echo"<br>";
} 

else{
    echo" Error: " .$query ."<br>" .$link->error;
}

echo"<br>";
echo "Transacton #: ".$transactionNum;
echo"<br>";
echo "Student ID: ".$profID;
echo"<br>";
echo "Employee ID: ".$empID;
echo"<br>";
echo "Professor ID: ".$profID;
echo"<br>";
echo "Product ID: ".$productID;
echo"<br>";
echo "Out Date: ".$checkoutDate;
echo"<br>";
echo "Out Time: ".$checkoutTime;
echo"<br>";
echo "Due Date: ".$dueDate;
echo"<br>";
echo "Due Time: ".$dueTime;
echo"<br>";
?>

为什么我不断获取1而不是我使用表单提交的值?

2 个答案:

答案 0 :(得分:0)

如果您的true数据中设置了1字段,则以下命令会将empID$_POST作为字符串)返回。

$empID =isset($_POST['empID']);

应该是

$empID = isset($_POST['empID']) ? $_POST['empID'] : "";

等等。

这意味着:如果有empID,则捕获$_POST,否则捕获空字符串。它是以下的简短版本:

if(isset($_POST['empID'])) {
    $empID = $_POST['empID'];
} else {
    $empID = "";
}

答案 1 :(得分:-1)

你在这里获得了isset的布尔值 $empID = isset($_POST['empID']); 这就是为什么它给你的值为1,因为在这种情况下它是true的值。

解析为if isset($_POST['empID']) {$empID = $_POST['empID'];}