PHP MySQL INSERT 查询未插入到 table
PHP MySQL INSERT query not inserting to table
我不明白为什么我的 INSERT 查询失败了。我已经仔细检查了 phpMyAdmin 中的查询,它工作正常,但是当它被放入 PHP 时它失败了。
PHP:
if (isset($_POST['create'])){
//Insert booking to the database
$InsertQuery = DB::getInstance()->query("INSERT INTO booking (`StartDate`, `EndDate`, `UserId`, `ItemId`) VALUES (
'{$_POST['startdate']}', '{$_POST['enddate']}', {$_POST[currentuser]}, {$_POST[itemid]}");
print_r($InsertQuery);
}
HTML:
<form action="MakeABooking1.php" method="post">
<tr>
<td><input type="text" name="itemid" value=" <?php echo $ItemId ?> "> </td>
<td><input type="text" name="currentuser" value=" <?php echo $currentUser ?> "> </td>
<td><input type="text" name="startdate" value=" <?php echo $bookFrom ?> "> </td>
<td><input type="text" name="enddate" value=" <?php echo $bookTo ?> "> </td>
<td><input type="submit" name="create" value="Book"></td>
</tr>
</form>
执行 print_r($InsertQuery);
显示以下内容:
DB Object ( [_pdo:DB:private] => PDO Object ( ) [_query:DB:private]
=> PDOStatement Object ( [queryString] => INSERT INTO booking (StartDate
, EndDate
, UserId
, ItemId
) VALUES ( ' 2015-03-26 ',
' 2015-03-31 ', 48 , 8 ) [_error:DB:private] => 1
[_results:DB:private] => Array ( ) [_count:DB:private] => 0 )
从 print_r 复制查询时它是有效的并创建了一个预订。我哪里错了?
好的,事实证明我的 SQL 查询中有一个错误,我在 VALUES 列表之后缺少一个 )。不会说谎,感觉很愚蠢!感谢您的所有建议。
我不明白为什么我的 INSERT 查询失败了。我已经仔细检查了 phpMyAdmin 中的查询,它工作正常,但是当它被放入 PHP 时它失败了。
PHP:
if (isset($_POST['create'])){
//Insert booking to the database
$InsertQuery = DB::getInstance()->query("INSERT INTO booking (`StartDate`, `EndDate`, `UserId`, `ItemId`) VALUES (
'{$_POST['startdate']}', '{$_POST['enddate']}', {$_POST[currentuser]}, {$_POST[itemid]}");
print_r($InsertQuery);
}
HTML:
<form action="MakeABooking1.php" method="post">
<tr>
<td><input type="text" name="itemid" value=" <?php echo $ItemId ?> "> </td>
<td><input type="text" name="currentuser" value=" <?php echo $currentUser ?> "> </td>
<td><input type="text" name="startdate" value=" <?php echo $bookFrom ?> "> </td>
<td><input type="text" name="enddate" value=" <?php echo $bookTo ?> "> </td>
<td><input type="submit" name="create" value="Book"></td>
</tr>
</form>
执行 print_r($InsertQuery);
显示以下内容:
DB Object ( [_pdo:DB:private] => PDO Object ( ) [_query:DB:private] => PDOStatement Object ( [queryString] => INSERT INTO booking (
StartDate
,EndDate
,UserId
,ItemId
) VALUES ( ' 2015-03-26 ', ' 2015-03-31 ', 48 , 8 ) [_error:DB:private] => 1 [_results:DB:private] => Array ( ) [_count:DB:private] => 0 )
从 print_r 复制查询时它是有效的并创建了一个预订。我哪里错了?
好的,事实证明我的 SQL 查询中有一个错误,我在 VALUES 列表之后缺少一个 )。不会说谎,感觉很愚蠢!感谢您的所有建议。