php中的mysql insert无错误反馈

whitzsjs  于 2021-06-24  发布在  Mysql
关注(0)|答案(1)|浏览(251)

我正在尝试使用php将数据输入mysql。不知道怎么了。连接成功,没有错误,但最后没有数据写入数据库。

$dbhost = "localhost";
$dbname = "listings";
$un = $_POST["un"];
$pass = $_POST["pass"];
$name = $_POST["name"];
$des = $_POST["des"];
$quan = $_POST["quantity"];
$specs = $_POST["specs"];
$price = $_POST["price"];
$url1 = ".";
$url2 = ".";
$url3 = ".";
$url4 = ".";

$connection = mysqli_connect($dbhost,$un,$pass,$dbname);
if (!$connection) {
    die("Error".mysqli_error);
} else {
    echo "Database connection successfull ".$des;
}

$query = "INSERT INTO items 
    (name,description,quantity,specs,price,url1,url2,url3,url4) VALUES 
    '$name','$des','$quan','$specs','$price','$url1','$url2','$url3','$url4')
";
echo "Hellos";
$exeute_query = mysqli_query($query,$connection);
if(!execute_query){
    die("error ".mysqli_error());
    echo "query error";
} else {
    echo "Query successfull";
}
mysqli_close($connection);

有什么帮助吗?

3pvhb19x

3pvhb19x1#

代码中有几个小错误:

$query = "INSERT INTO items (name,description,quantity,specs,price,url1,url2,url3,url4) VALUES ('$name','$des','$quan','$specs','$price','$url1','$url2','$url3','$url4')";
echo "Hellos";

**$exeute_query**= mysqli_query($query,$connection); // $execute_query instead of $exeute_query

if(!**execute_query**){ //$execute_query instead of execute_query
 die("error ".mysqli_error());
 echo "query error";
}
else{echo "Query successfull";}
mysqli_close($connection);
?>

您的代码在if语句处中断,因为找不到具有该名称的函数(如果您不使用dollarsign来显示它是一个变量,php将把它解释为一个函数)。此外,在初始化变量时,您忘记了一个“c”,因此请确保检查您是否有正确的变量名,否则php将找不到您的变量。现在,如果数据格式错误或连接不好,您的查询将起作用或给出错误消息。以后使用下面列出的代码调试php。

error_reporting(E_ALL); 
ini_set('display_errors', 'On');

相关问题