MySQLi准备语句未插入数据库表

时间:2011-09-13 06:12:11

标签: php mysqli prepared-statement

我正在尝试使用MySQLi prepare语句将值插入到我的OpenJobs表中。我有4个复选框,相当于4个不同的工作。如果标记了一个或多个复选框,我希望将相应的jobid插入到表中。这是我到目前为止编写的代码:

<form id="frmSelStore" method="post" action ="<?php 
$restaurantid = $_POST['ddlStore'];
$jobtype = $_POST['jobs'];
if($stmt = mysqli_prepare($mysqli,'Insert into OpenJobs values $restaurantid, $jobtype'))
{
    mysqli_stmt_bind_param($restaurantid,$jobtype);
    mysqli_stmt_execute($stmt);
    echo '$jobtype was posted for Store $restaurantid';
    mysqli_stmt_close($stmt);
}

?>">

其余形式:

 <fieldset><?php 
    $query=('SELECT restaurantid,location from restaurant');
    $result = mysqli_query($mysqli,$query);
    echo '<select name="ddlStore">';
    while($row=mysqli_fetch_array($result))
    {
        echo '<option value="' . htmlspecialchars($row['restaurantid']) . '">' .
         htmlspecialchars($row['location']) . 
        '</option>';
    }
    echo '</select>';
    ?>
    <h2>Step 2:  Please Select the Jobs to be Posted</h2>
    <input type="checkbox" name='[jobs]' id="cbJobs1">Host/Hostess
    </input><br/>
    <input type="checkbox" name='[jobs]' id="cbJob2">Bartender</input><br/>
    <input type="checkbox" name='[jobs]' id="cbJob3">Server</input><br/>
    <input type="checkbox" name='[jobs]' id="cbJobs4">Cook</input><br/>

    <input type="submit" id="submit"/>
    </fieldset>

当我选中一个复选框并单击“提交”时,我希望得到的信息表明我的工作已经发布。但是,没有任何操作,也没有记录写入表。我该如何解决这个问题?

更新:我的代码如下:

<form id="frmSelStore" method="post" action ="<?php 
$restaurantid = $_POST['ddlStore'];
$jobtype = $_POST['jobs'];
$stmt = mysqli_statement_init($mysqli);
if(mysqli_stmt_prepare($stmt,'Insert into `OpenJobs` (`restaurantid`,`jobtype`) 
values ($restaurantid, $jobtype'))
{
mysqli_stmt_bind_param($stmt,$restaurantid,$jobtype);
mysqli_stmt_execute($stmt);
printf("%d Row Inserted.\n", mysqli_stmt_affected_rows($stmt));
mysqli_stmt_close($stmt);
}

?>">

当我查看我的页面时,我看到以下错误:

Warning</b>: mysqli_stmt_bind_param() expects 
parameter 1 to be mysqli_stmt, null given in 
<b>/path/to/page</b> on line 
<b>23</b><br /><br /><b>Warning</b>: mysqli_stmt_execute() expects parameter 1 to be   
mysqli_stmt, boolean given in <b>/path/to/page</b> 
on line <b>24</b><br />$jobtype was posted for Store   
$restaurantid<br /><b>Warning</b>:   
mysqli_stmt_close() expects parameter 1 to be mysqli_stmt, boolean given in 
<b>/path/to/page</b> on line <b>26</b><br /> was not 
found on this server.

Additionally, a 404 Not Found error was encountered while trying to use an 
ErrorDocument to handle the request.

我正在遵循程序方法的手动示例,但需要一些如何纠正上述错误的指导。

1 个答案:

答案 0 :(得分:1)

好像你的插入查询语句格式不正确。

尝试更改:

Insert into OpenJobs values $restaurantid, $jobtype

最低限度:

Insert into OpenJobs values ($restaurantid, $jobtype)

我的建议:

insert into `OpenJobs` (`restaurantid`, `jobtype`) values ($restaurantid, $jobtype)

请注意:区分大小写也会导致问题,具体取决于您配置的方式(例如OpenJobs与openJobs vs. openjobs)。