PHP表单将空数据发送到mySQL

时间:2015-08-12 04:21:49

标签: php mysql forms

我正在为学校的活动创建一个注册表,但对于PHP和数据库来说,这是一个非常新的事情,这对我来说很难。

问题是表单会向表格提交空白数据。我可以看到每次提交时都会创建一个新行,但数据是空白的。

以下是代码:

index.html

中表单的

部分

<form action="input.php" method="post">
<p>
   <input name="prefix" type="radio" id="male"/>
   <label for="male">นาย/เด็กชาย</label>
</p>
<p>
   <input name="prefix" type="radio" id="female"/>
   <label for="female">นางสาว/เด็กหญิง</label>
</p>
<div class="row">
    <div class="col s12">
        <div class="input-field col s12 m6">
            <input id="firstname" type="text" class="validate"/>
            <label for="firstname">ชื่อ</label>
        </div>
        <div class="input-field col s12 m6">
            <input id="lastname" type="text" class="validate"/>
            <label for="lastname">นามสกุล</label>
        </div>
           .
           .
           .
           .
<button class="btn btn-large waves-effect waves-light red accent-4" type="submit" name="submit">ส่ง</button>
</form>

input.php

<?php
mysql_connect("localhost", "acsp", "passwordhidden");
mysql_select_db("logicgames");
$order = "INSERT INTO data_logicgames (prefix, firstname, lastname, phone, school, teammate1, teammate2, games, division) VALUES ('$prefix', '$firstname', '$lastname', '$phone', '$school', '$teammate1', '$teammate2', '$games', '$division')";
$result = mysql_query($order);
if ($result) {
    echo "<p>Success</p>";
} else {
    echo "<p>Failed</p>";
}
?>

4 个答案:

答案 0 :(得分:1)

首先在数据库字段中为每个输入字段指定一些名称。

您必须在变量中收到POST值。如果你不想每次都这样做。你可以使用foreach循环。

<?php
mysql_connect("localhost", "acsp", "passwordhidden");
mysql_select_db("logicgames");
foreach($_POST as $key => $val)
{
    ${$key} = $val;
}
$order = "INSERT INTO data_logicgames (prefix, firstname, lastname, phone, school, teammate1, teammate2, games, division) VALUES ('$prefix', '$firstname', '$lastname', '$phone', '$school', '$teammate1', '$teammate2', '$games', '$division')";
$result = mysql_query($order);
if ($result) {
    echo "<p>Success</p>";
} else {
    echo "<p>Failed</p>";
}
?>

答案 1 :(得分:1)

更好的方法是......

<?php

if( isset($_POST['submit']) )
{
    $prefix     =   $_POST['prefix'];
    $firstname  =   $_POST['firstname'];
    $lastname   =   $_POST['lastname'];
    $phone      =   $_POST['phone'];
    $school     =   $_POST['school'];
    $teammate1  =   $_POST['teammate1'];
    $teammate2  =   $_POST['teammate2'];
    $games      =   $_POST['games'];
    $division   =   $_POST['division'];

    mysql_connect("localhost", "acsp", "passwordhidden");
    mysql_select_db("logicgames");
    $order = "INSERT INTO data_logicgames (prefix, firstname, lastname, phone, school, teammate1, teammate2, games, division) VALUES ('$prefix', '$firstname', '$lastname', '$phone', '$school', '$teammate1', '$teammate2', '$games', '$division')";
    $result = mysql_query($order);
    if ($result) {
        echo "<p>Success</p>";
    } else {
        echo "<p>Failed</p>";
    }
}
else
{
    echo "<p>Failed</p>";
}
?>

答案 2 :(得分:0)

在你的input.php中

你没有定义

'$prefix', '$firstname', '$lastname', '$phone', '$school', '$teammate1', '$teammate2', '$games', '$division'
像这样 $ prefix = $ _POST [&#39;前缀&#39;]; 等

答案 3 :(得分:0)

您的表单有2个没有名称标签的输入!您需要为发送到服务器的每个字段指定一个名称标签,并且能够使用$ _POST [&#34; lastname&#34;]检索它。在PHP中。 你在哪里宣布自己的价值观? ($ prefix,$ lastaname等..) id仅在使用JavaScript时使用。