表单写入mysql数据库

时间:2015-04-26 10:14:46

标签: php html sql database mysqli

我正在努力将表单写入数据库,仍然是初学者。我尝试了一个表格,我将在下面显示,但它似乎给我错误。如果有人能帮助我找出我的PHP代码有什么问题那么它会很棒。提前致谢:

我的form.php

<form method="post" action="db.php" name="OverrideForm" id="OverrideForm" autocomplete="on">
    <fieldset>
        <legend>Contact Details</legend>
        <div>
            <label for="name" accesskey="N">First Name</label>
            <input name="name" type="text" id="name" required />
        </div>
        <div>
            <label for="mname" accesskey="M">Middle Name</label>
            <input name="mname" type="text" id="mname" required />
        </div>
        <div>
            <label for="fname" accesskey="F">Last Name</label>
            <input name="fname" type="text" id="fname" required />
        </div>
        <div>
            <label for="sid" accesskey="i">Student ID</label>
            <input name="sid" type="text" id="sid" size="10"  required />
        </div>
<div>
            <label for="email" accesskey="E">Email</label>
            <input name="email" type="email" id="email" pattern="^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$" required />
        </div>
        <div>
            <label for="phone" accesskey="p">Phone Number</label>
            <input name="phone" type="text" id="phone" size="8"  required />
        </div>
<div>
            <label for="sc" accesskey="s">Scolarship</label>
            <select name="sc" id="sc" required="required">
                <option value="0">Yes</option>
                <option value="1">No</option>
            </select>
        </div>            
   </fieldset>

    <fieldset>
        <legend>Subject Details</legend>
<div>
            <label for="class" accesskey="c">Class</label>
            <input name="class" type="text" id="class" size="50" required />
        </div>
        <div>
            <label for="section" accesskey="o">Section</label>
            <input name="section" type="text" id="section" size="1" required />
        </div>
        <div>
            <label for="semester" accesskey="S">Semester</label>
            <select name="semester" id="semester" required="required">
                <option value="F15">Fall 2015</option>
                <option value="S15">Summer 2015</option>
                <option value="SP16">Spring 2016</option>
            </select>
        </div>
    </fieldset>

    <input type="submit" class="submit" id="submit" value="Submit" />
</form>

我的db.php

    <?php
$mysql_host     = "localhost";
$mysql_username = "blahblah";
$mysql_password = "blahblah";
$mysql_database = "blahblah";

$mysqli  = new Mysqli($mysql_host, $mysql_username, $mysql_password, $mysql_database) or die(mysql_error());
$prepare = $mysqli->prepare("INSERT INTO `Overrides`(`name`,`mname`,`fname`,`sid`,`email`,`phone`,`sc`,`class`,`section`,`semester`) VALUES ('$name','$mname','$fname','$sid','$email','$phone','$sc','$class','$section','$semester')");
$prepare->bind_param("ssssssssss", $_POST['name'], $_POST['mname'], $_POST['fname'], $_POST['sid'], $_POST['email'], $_POST['phone'], $_POST['sc'], $_POST['class'], $_POST['section'], $_POST['semester']);
$prepare->execute();
print_r($_POST)
?>

我得到的错误是:

Warning: mysqli_stmt::bind_param(): Number of variables doesn't match number of parameters in prepared statement in /home/aukwizcq/public_html/db.php on line 9
Array ( [name] => aaa [mname] => aaa [fname] => aaaa [sid] => 123456 [email] => fgfg@hotmail.com [phone] => 45454 [sc] => 1 [class] => Cpeg 340 [section] => 1 [semester] => S15 )

我的数据库结构:

名称类型排序规则属性空默认额外操作

1   name    varchar(30) latin1_swedish_ci       No  None        Change 
2   mname   varchar(30) latin1_swedish_ci       No  None        Change 
3   fname   varchar(30) latin1_swedish_ci       No  None        Change 
4   sid varchar(11) latin1_swedish_ci       No  None        Change Change   
5   email   varchar(50) latin1_swedish_ci       No  None        Change 
6   phone   int(8)          No  None        Change Change   Drop Drop   
7   sc  bit(1)          No  None        Change Change   Drop Drop   
8   class   varchar(10) latin1_swedish_ci       No  None        Change 
9   section int(1)          No  None        Change Change   Drop Drop   
10  semester    varchar(11) latin1_swedish_ci       No  None        Change 

1 个答案:

答案 0 :(得分:1)

预准备语句的语法不正确,变量应该是问号。它应该是这样的:

$prepare = $mysqli->prepare("INSERT INTO `Overrides`(`name`,`mname`,`fname`,`sid`,`email`,`phone`,`sc`,`class`,`section`,`semester`) VALUES (?,?,?,?,?,?,?,?,?,?)");