php.mysqli的新手,并坚持选择查询

时间:2013-03-01 15:14:28

标签: php ajax mysqli

我正在尝试对以下代码进行Ajax调用,但是我得到的却是500错误。关于我做错的任何指示都会受到欢迎。感谢

if isset(($_POST['lodgeChoice']))
{
    require_once $_SERVER['DOCUMENT_ROOT'] . '/Includes/datalogin.php';
    $lodgeChoice = $_POST['lodgeChoice'];
    $startDate = $_POST['dateChosen'];
    $duration = $_POST['duration'];

    $endDate = strtotime($startDate. ' + ' . $duration . ' days');

    $mysql_query = 'SELECT id FROM bookings WHERE (the_date BETWEEN "' . $startDate . '" AND "' . $endDate . '") AND id_item = ' . $lodgeChoice;

    if($stmt = $mysqli->prepare('SELECT COUNT(*) FROM bookings WHERE (the_date BETWEEN "?" AND "?") AND (id_item = ?)');
        $stmt->bind_param("sss",$startDate, $endDate,$lodgeChoice);
        $stmt->execute();
        $stmt->bind_result($count);
        $stmt->fetch();
        $stmt->close();

        return ($count > 0 ? TRUE : FALSE);

}

1 个答案:

答案 0 :(得分:2)

我强烈建议您使用支持语法突出显示的代码编辑器,因为您会非常清楚地看到错误。

我将您的代码粘贴到Netbeans中,并立即在代码中的if语句中标记错误。

第一个:if条件周围没有括号。 (你在post变量周围也有太多括号,但这不会导致任何错误)

if isset(($_POST['lodgeChoice']))
  ^                              ^
bracket reqd here              and here

第二个,你在结尾有一个分号,这是错误的,并且你想要在if()中的代码周围没有任何大括号。

//removed ; from end of the if() line:
if($stmt = $mysqli->prepare('SELECT COUNT(*) FROM bookings WHERE (the_date BETWEEN "?" AND "?") AND (id_item = ?)')
{    //added this line
    .....
    .....
}   //added this line as well.

希望有所帮助。

正如我所说,一个不错的代码编辑器将使您的生活更轻松。因为你可以免费获得一些非常优秀的编辑器,所以没有理由不使用它。