为什么会这样? mysqli_query():第52行的/ home / evansbwg / public_html / Old Things / Website / insert.php中的空查询

时间:2015-06-05 11:13:09

标签: php mysql mysqli

<?php
//$game_id = ['gameid'];
$first_name = $_POST['firstname'];
$last_name = $_POST['lastname'];
$address = $_POST['address'];
$postal_code = $_POST['postalcode'];
$start_date = $_POST['startdate'];
$rental_days = $_POST['rentaldays'];
$borrow_game = $_POST['borrowgame'];

变量在这里设置^!

$end_date = date('Y-m-d', strtotime($start_date.' + '.$rental_days.'days'));

end_date函数如何工作^!

$conn = mysqli_connect("localhost", "evansbwg_user", "1password1", "evansbwg_database");

if ($conn->connect_error)
{
    die("Connection failed: " . $conn->connect_error);
}

连接^!

$result = mysqli_query($conn, "SELECT * FROM tbl_games WHERE game_name = '$borrow_game'");

//$result_present = mysqli_query($conn, $result);

$num_rows = mysqli_num_rows($result);

if ($num_rows == 1)

{
    $row2 = mysqli_fetch_array($result);
    $id = $row2['game_id'];

让game_id等于borrow_game ^!

    $sql_search = "SELECT * FROM tbl_rental WHERE game_id = '$id' 
    AND (start_date <= '$start_date' AND end_date >= '$end_date')
    OR (start_date >= '$start_date' AND start_date <= '$end_date')
    OR (end_date >= '$start_date' AND end_date <= '$end_date')
    OR (start_date >= '$start_date' AND end_date <= '$end_date')";

    $result1 = mysqli_query($conn, $sql_search);

    $num = mysqli_num_rows($result1);

    if ($num == 0) 
    {
        $sql = "INSERT INTO tbl_rental (game_id, first_name, last_name, address, postal_code, start_date, rental_days, borrow_game, end_date)
       VALUE ('$id', '$first_name', '$last_name', '$address', '$postal_code', '$start_date', '$rental_days', '$borrow_game', '$end_date')";

将数据插入表格^!

    if (mysqli_query($conn, $sql))
    {
        echo "New record created successfully";
    }
    else
    {
        echo "Error: " . $sql . "<br>" . mysqli_error($conn);
    }
    }

}
while ($row = mysqli_fetch_array($result)) 
{
    $id = $row['game_id'];
}
mysqli_close($conn);
?>

我已解决了我的主要问题,但如果需要进行任何更改,请提出建议。

2 个答案:

答案 0 :(得分:2)

您提前关闭了连接,因此后面的查询与查询无关。

mysqli_close($conn);
^^^^^^^^^^^^^^^^^^^^
}
while ($row = mysqli_fetch_array($result)) 
{
$id = $row['game_id'];
}

将其放在脚本的末尾,或者将其删除。

这一行也没有意义$end_date = ['enddate'];

你可能打算这样做:

$end_date = $_POST['enddate'];

因为上面的其他人正在使用POST数组。

然而,看到

$end_date = date('Y-m-d', strtotime($start_date.' + '.$rental_days.'days'));

您需要删除$end_date = ['enddate'];,这是无效的语法。

旁注:

您目前的代码向SQL injection开放。使用prepared statementsPDO with prepared statements他们更安全

error reporting添加到文件的顶部,这有助于查找错误。

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

旁注:错误报告应仅在暂存时完成,而不是生产。

另外,将or die(mysqli_error($conn))添加到mysqli_query()

答案 1 :(得分:0)

如果

会发生什么
$num = mysqli_num_rows($result1);

返回than more than one zero row

if ($num == 0) 

您的代码不会进入if condition和您的$sql variable is empty

if (mysqli_query($conn, $sql))// here $sql is empty if $num is greater then zero

如此贴近你的if条件

if ($num == 0) 
{
$sql = "INSERT INTO tbl_rental (game_id, first_name, last_name, address, postal_code, start_date, rental_days, borrow_game, end_date)
VALUE ('$id', '$first_name', '$last_name', '$address', '$postal_code', '$start_date', '$rental_days', '$borrow_game', '$end_date')";

Inserting the data into the table^!

if (mysqli_query($conn, $sql))
{
echo "New record created successfully";
}
else
{
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
}// if condition close here

代码末尾的连接也关闭

while ($row = mysqli_fetch_array($result)) 
{
$id = $row['game_id'];
}

mysqli_close($conn);// at last