相同的代码在另一个页面中正常工作,但现在它得到了以下错误。
mysqli_error()只需要1个参数
当我回声它工作正常,但当我尝试插入它失败。不知道代码有什么问题。
<?php
session_start();
$id = $_SESSION["id"];
$cart_id = $_SESSION["cart_id"];
$cname = $_SESSION["cname"];
echo $cname;
echo $id;
echo $cart_id;
if (isset($_SESSION['cart_id'])) {
$con=mysqli_connect("localhost","root","","medical");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
error_reporting(E_ALL ^ E_NOTICE);
$insert="INSERT INTO order(cart,cid,name)
VALUES($cart_id,$id,$cname)";
if (!mysqli_query($con,$insert))
{
die('Error: Failed Inserting' . mysqli_error());
}
echo "Success Fully Added ";
}else{
echo "somethimg wrong loser";
}
?>
答案 0 :(得分:0)
我甚至不想回答这个问题,因为我在上面的评论中为您提供了所有有用的链接,
这些是非常有用的链接
When to use single quotes, double quotes, and backticks in MySQL
How to get mysqli error in different environments?
Syntax error due to using a reserved word as a table or column name in MySQL
您的查询存在问题:
$insert="INSERT INTO order(cart,cid,name)
VALUES($cart_id,$id,$cname)";
您将这些变量视为整数,但$ cname是一个字符串,因此它应该是带引号的包装。
目前上面的代码将会出现
'字段列表'
中的未知列'WhateverValue of cname'
这应该是这样的
$con = mysqli_connect("localhost", "root", "", "medical");
if (!$con) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
error_reporting(E_ALL ^ E_NOTICE);
$insert = "INSERT INTO `order`(cart,cid,name) VALUES($cart_id,$id,'$cname')";
$query = mysqli_query($con, $insert);
if ($query) {
echo "Success Fully Added ";
}else{
die('Error: Failed Inserting' . mysqli_error($con));
}
编辑更好的方法是使用预准备语句
以这种方式,你不会像现在这样有简单的问题$con = new mysqli("localhost", "root", "", "schedules");
if ($con->connect_error) {
die("Connection failed: " . $con->connect_error);
}
$insert = "INSERT INTO `order`(cart,cid,name) VALUES(?,?,?)";
$query = $con->prepare($insert);
$query->bind_param("iis",$cart_id,$id,$cname);
if($query->execute()){
echo "Success Fully Added ";
}else{
die('Error: Failed Inserting' . $con->error);
}