我正在努力转换为MYSQLi。我还没有完全自信的东西。我试图分解我脚本的这一部分时遇到错误。
<?php
// Parse the form data and add inventory item to the system
if (isset($_POST['username'])) {
$username = $_POST['username'];
$password = $_POST['password'];
$level = ($_POST['level']);
// See if that product name is an identical match to another product in the system
include "includes/db_conx.php";
$sql = "SELECT id FROM users WHERE username='$username' LIMIT 1";
$user_query = mysqli_query($db_conx, $sql);
$productMatch = mysqli_num_rows($sql); // count the output amount
if ($productMatch > 0) {
header("location: message.php?msg=usererror");
exit();
}
// Add this product into the database now
$username = preg_replace('#[^a-z0-9]#i', '', $_POST['username']);
$ip = preg_replace('#[^0-9.]#', '', getenv('REMOTE_ADDR'));
$email = mysqli_real_escape_string($_POST['email']);
$p_hash = md5($password);
$sql = mysqli_query("INSERT INTO users (username, password, ip, email, level, date_added)
VALUES('$username','$p_hash','$ip','$email','$level',now())") or die (mysql_error());
header("location: order_complete.php");
exit();
}
?>
我相信我已经完成了大部分工作,但是下半部分让我很适合。我正试图建立过去
//立即将此产品添加到数据库中
一个mysqli转换。我似乎无法避免破坏脚本并抛出各种错误。我相信我已经走了一半,但引入选择让我失望。有人可以帮我解决这个问题。
答案 0 :(得分:0)
您将$ sql作为参数传递给mysqli_num_rows(),它应该是mysqli_query()的结果。
所以改为这个。
$sql = "SELECT id FROM users WHERE username='$username' LIMIT 1";
$user_query = mysqli_query($db_conx, $sql);
$productMatch = mysqli_num_rows($user_query); // count the output amount
该变量的更好名称可能是$ user_result或带有结果的字样。
答案 1 :(得分:-1)
检查下面提到的代码。
if (isset($_POST['username'])) {
$username = $_POST['username'];
$password = $_POST['password'];
$level = ($_POST['level']);
// See if that product name is an identical match to another product in the system
include "includes/db_conx.php"; // I guess it has $db_conx = mysqli_connect("localhost", "my_user", "my_password", "world");
$sql = "SELECT id FROM users WHERE username='$username' LIMIT 1";
$user_query = mysqli_query($db_conx, $sql);
$productMatch = mysqli_num_rows($user_query); // count the output amount
if ($productMatch > 0) {
header("location: message.php?msg=usererror");
exit();
}
// Add this product into the database now
$username = preg_replace('#[^a-z0-9]#i', '', $_POST['username']);
$ip = preg_replace('#[^0-9.]#', '', getenv('REMOTE_ADDR'));
$email = mysqli_real_escape_string($db_conx, $_POST['email']);
$p_hash = md5($password);
$sql = mysqli_query($db_conx, "INSERT INTO users (username, password, ip, email, level, date_added) VALUES('$username','$p_hash','$ip','$email','$level',now())") or die (mysqli_error($db_conx));
header("location: order_complete.php");
exit();
}