PHP Form将空白数据发送到mySQL [英] PHP Form sends blank data to mySQL

查看:92
本文介绍了PHP Form将空白数据发送到mySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为学校的活动创建注册表,但是PHP和数据库的新手使我很难进行这项工作.

I am creating a registration form for my school's event, but being very new to PHP and database makes it a hard work for me.

问题在于表单将空白数据提交到表中.我可以看到每次提交时都会创建一个新行,但是数据为空.

The problem is that the form submits blank data to the table. I can see that there is a new row created upon every submission, but the data is blank.

这是代码:

部分

Part of form in 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>";
}
?>

推荐答案

更好的方法是..

<?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>";
}
?>

这篇关于PHP Form将空白数据发送到mySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆