【发布时间】:2015-12-30 01:17:10
【问题描述】:
我刚刚将一个站点从一个域迁移到另一个域(和另一台主机)。我确保所有链接都被替换并导出/导入了我的数据库。迁移似乎成功了,但由于某种原因,我在新域上遇到了一个我在旧域上没有遇到的错误(据我所知,使用相同的代码)。
我的错误是:“警告:mysqli_free_result() 期望参数 1 为 mysqli_result,在第 84 行的 path 中给出了 null”。我查看了解决此错误的其他 StackOverflow 问题,但尚未找到解决方案。
这是我的代码:
<?php
session_start();
// 1. Create a database connection
$dbhost =
$dbuser =
$dbpass =
$dbname =
$connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
// Test if connection occurred.
if(mysqli_connect_errno()) {
die("Database connection failed: " .
mysqli_connect_error() .
" (" . mysqli_connect_errno() . ")"
);
}
// 2. Perform database query
if (empty($_SESSION['order'])) {
$query = "INSERT INTO `orders` (`order_id`) VALUES (NULL)";
$result = mysqli_query($connection, $query);
// Test if there was a query error
if (!$result) {
die("Database query failed.");
}
// 3. Use returned data (if any)
$order_id_recent = mysqli_insert_id($connection);
$_SESSION['order'] = $order_id_recent;
}
$size = $_POST["size"];
$paper = $_POST["paper"];
$type = $_POST["type"];
$quantity = $_POST["quantity"];
// 2. Perform database query
$query2 = "SELECT product_id FROM product WHERE product_type = '$type' AND size = '$size' AND paper = '$paper'";
$result2 = mysqli_query($connection, $query2);
// Test if there was a query error
if (!$result2) {
die("Database query failed.");
}
// 3. Use returned data (if any)
while($row = mysqli_fetch_assoc($result2)) {
$product_id = $row['product_id'];
}
$order_id = $_SESSION['order'];
// 2. Perform database query
$order_id = $_SESSION['order'];
$query3 = "SELECT * FROM order_item WHERE order_id = '$order_id' AND product_id = '$product_id'";
$result3 = mysqli_query($connection, $query3);
// Test if there was a query error
if (!$result3) {
die("Database query failed.");
}
while($row = mysqli_fetch_assoc($result3)) {
$itemexistrows = mysqli_num_rows($result3);
}
if ($itemexistrows > 0) {
$query4 = "UPDATE order_item SET quantity = quantity + '$quantity' WHERE product_id = '$product_id' AND order_id = '$order_id'";
$result4 = mysqli_query($connection, $query4);
if (!$result4) {
die("Database query failed.");
} else {
echo 'The item has been added to your cart. <a class="text-red" href="viewcart.php">View your cart</a></div>.';
}
} else {
$query5 = "INSERT INTO `order_item`(`order_item_id`, `product_id`, `quantity`,`order_id`) VALUES (NULL,'$product_id','$quantity','$order_id')";
$result5 = mysqli_query($connection, $query5);
if (!$result5) {
die("Database query failed.");
} else {
echo 'The item has been added to your cart. <a class="text-red" href="viewcart.php">View your cart</a></div>.';
}
}
// 4. Release returned data
mysqli_free_result($result);
// 5. Close database connection
mysqli_close($connection);
?>
奇怪的是我的网站似乎仍然可以工作。这些代码行是购物车模块的一部分,并且购物车似乎已更新。
【问题讨论】:
-
您可以“按原样”显示代码吗?
//4.在代码中的什么位置?感谢您的分离,但这让您更难为您提供帮助 -
嘿@Martin,感谢您的及时回复!我最初把其余的代码留了出来,因为它真的很丑而且很乱(很久以前写的),但我编辑了我的帖子。谢谢! (编辑:看起来我也没有尝试释放其他查询的结果。总体上可能还有很多方法可以改进此代码,但我目前只是希望解决这个问题。)
-
您的代码遭受 sql 注入,我建议您通过使用准备好的语句来敏感化您收到的输入。 websitebeaver.com/…
标签: php mysql mysqli sql-insert