Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Mysqli和real转义字符串错误_Php - Fatal编程技术网

Php Mysqli和real转义字符串错误

Php Mysqli和real转义字符串错误,php,Php,我正在将旧文件转换为mysqli,在我点击mysql\u real\u escape\u字符串之前,我一直很顺利。 我收到了关于不传递2个参数的错误消息,我知道我只给了它一个参数,但不知道在哪里添加第二个参数。我相信它正在寻找Db连接,但在这一点上,我尝试了太多我不确定的事情。 我认为我可以将Db连接放在$u POST命令前面,但这不起作用,并给了我2个参数错误,因此如果有人能给我一个正确方向的提示,我将不胜感激 我 在您的示例代码中没有使用真正的secape字符串方法,但您正在调用$conn对

我正在将旧文件转换为mysqli,在我点击mysql\u real\u escape\u字符串之前,我一直很顺利。 我收到了关于不传递2个参数的错误消息,我知道我只给了它一个参数,但不知道在哪里添加第二个参数。我相信它正在寻找Db连接,但在这一点上,我尝试了太多我不确定的事情。 我认为我可以将Db连接放在$u POST命令前面,但这不起作用,并给了我2个参数错误,因此如果有人能给我一个正确方向的提示,我将不胜感激


在您的示例代码中没有使用真正的secape字符串方法,但您正在调用$conn对象上的query方法,我认为这是mysqli连接。所以你可以用

$string = $conn->real_escape_string($string);
而不是mysql\u real\u escape\u字符串。事实上,您可以使用对象方法和属性而不是mysqli函数。例如,您可以使用

$result->num_rows
而不是mysqli_num_行


希望这能有所帮助。

有很多问题

首先,

需要数据库连接,并作为第一个参数,后跟POST数组或变量,您在代码中仅将其用于其中一个

$product_name = mysqli_real_escape_string($conn, $_POST['product_name']);
                                          ^^^^^
你需要为它下面的所有其他部分做这件事

您也没有执行此查询:

 $sql = "DELETE FROM products ....
也不是这个:

$sql = ("INSERT INTO products ....
$sql = "SELECT id FROM products ...
也不是这个:

$sql = ("INSERT INTO products ....
$sql = "SELECT id FROM products ...
我还注意到,您可能将密码存储为纯文本,在实时环境中使用这是不安全的

你应该使用一个事先准备好的语句,这也是一件好事

此外,还需要db连接:

mysqli_error($conn)

并确保所有POST数组都包含值。

我喜欢将完成的代码发布给任何未来的初学者,以帮助他们与我同舟共济

       include "../connections/connect_mysqli.php";
    $conn = dbConnect('read'); 
    $sql = "SELECT id FROM admin WHERE username='$manager' AND password='$password' LIMIT 1"; // query the person
    $result = $conn->query($sql) or die($conn->error);
    // ------- MAKE SURE PERSON EXISTS IN DATABASE ---------
    $existCount = $result->num_rows; // count the row nums
if ($existCount == 0) { // evaluate the count
     echo "Your login session data is not on record in the database.";
     exit();
}
?>
<?php 
// Script Error Reporting
error_reporting(E_ALL);
ini_set('display_errors', '1');
?>
<?php 
// Delete Item Question to Admin, and Delete Product if they choose
if (isset($_GET['deleteid'])) {
    echo 'Do you really want to delete product with ID of ' . $_GET['deleteid'] . '? <a href="inventory_list.php?yesdelete=' . $_GET['deleteid'] . '">Yes</a> | <a href="inventory_list.php">No</a>';
    exit();
}
if (isset($_GET['yesdelete'])) {
    // remove item from system and delete its picture
    // delete from database
    $id_to_delete = $_GET['yesdelete'];
    $sql = "DELETE FROM products WHERE id='$id_to_delete' LIMIT 1";
    $result = $conn->query($sql) or die($conn->error);
    // unlink the image from server
    // Remove The Pic -------------------------------------------
    $pictodelete = ("../images/$id_to_delete.jpg");
    if (file_exists($pictodelete)) {
                unlink($pictodelete);
    }
    header("location: inventory_list.php"); 
    exit();
}
?>
<?php 
// Parse the form data and add inventory item to the system
if (isset($_POST['product_name'])) {

    $product_name = $conn->real_escape_string($_POST['product_name']);
    $price = $conn->real_escape_string($_POST['price']);
    $details = $conn->real_escape_string($_POST['details']);
    $details2 = $conn->real_escape_string($_POST['details2']);
    $details3 = $conn->real_escape_string($_POST['details3']);
    // See if that product name is an identical match to another product in the system
    $sql = "SELECT id FROM products WHERE product_name='$product_name' LIMIT 1";
    $result = $conn->query($sql) or die($conn->error);
    $productMatch = $result->num_rows; // count the output amount
    if ($productMatch > 0) {
        echo 'Sorry you tried to place a duplicate "Product Name" into the system, <a href="inventory_list.php">click here</a>';
        exit();
    }
    // Add this product into the database now
    $sql = ("INSERT INTO products (product_name, price, details, details2, details3, date_added) 
        VALUES('$product_name','$price','$details','$details2','$details3',now())") or die ($conn->error);
        $result = $conn->query($sql) or die($conn->error);
     $pid = $conn->insert_id;
    // Place image in the folder 
    $newname = "$pid.jpg";
    move_uploaded_file( $_FILES['fileField']['tmp_name'], "../images/$newname");
    header("location: inventory_list.php"); 
    exit();
}
?>

在我点击mysql\u real\u escape\u字符串之前,没有任何代码建议它,如果您使用了I版本并将连接传递给它,那么连接实际上是什么。所以,这是任何人的游戏;不过我不会玩猜谜游戏,因为你到底在用什么。对不起,弗雷德,我以为我把它都抄下来了。谢谢萨朱什科,我会试试看。谢谢弗雷德,我已经敲了好一阵子了,这帮我修好了。我有$conn在逃逸字符串中,但我把它们拿出来重新开始。我认为页面顶部的Db连接可以在不添加的情况下继续,这是错误的。在多次擦除和重新开始之后,我也错过了查询执行。我在我的常规站点上使用密码散列,但这是一个教程,所以我保留了原样。我真的很感谢你的帮助。Cheers@ribbonman不客气。如果您希望将问题标记为已解决,请这样做。这样,它将通知每个人不需要其他答案,并结束问题,cheers@ribbonman顺便说一句,这就是问题被标记为已解决的方式。