php+mysql不能在数据库中添加行,没有错误

vs3odd8k  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(345)

这个问题在这里已经有答案了

如何显示mysqli查询的错误[重复](2个答案)
引用-这个错误在php中是什么意思(36个答案)
两年前关门了。
我试图添加新行到数据库,但是,它没有添加,我没有收到一个错误。代码在本地工作,但在web上不工作。
这是代码;

$RoomNo = mysqli_real_escape_string($conn,$_POST["roomno"]);
$Lastname = mysqli_real_escape_string($conn,$_POST["lastname"]);
$Name = mysqli_real_escape_string($conn,$_POST["name"]);
$Arrival = mysqli_real_escape_string($conn,$_POST["arrival"]);
$Departure = mysqli_real_escape_string($conn,$_POST["departure"]);
$EventType = mysqli_real_escape_string($conn,$_POST["eventtype"]);
$AssignedUser = mysqli_real_escape_string($conn,$_POST["assigned_user"]);
$Comment = mysqli_real_escape_string($conn,$_POST["comment"]);
$Action = mysqli_real_escape_string($conn,$_POST["action"]);

$ChooseAssignedUser = explode("|", $AssignedUser);

$eventregister_sql = "INSERT INTO tbl_event (room,lastname,name,arrival,departure,event_type,assigned_user,department,comment,action,user,update_user,call_time,update_time,status,hotel_id) VALUES ('".$RoomNo."','".$Lastname."','".$Name."','".$Arrival."','".$Departure."','".$EventType."','".$ChooseAssignedUser[0]."','".$ChooseAssignedUser[1]."','".$Comment."','".$Action."',".$_SESSION['user_id'].",".$_SESSION['user_id'].",'$gmtdbro','$gmtdbro','N',".$_SESSION['hotel_id'].")";
$eventregister_result = $conn->query($eventregister_sql);
echo "<center>Event registered successfuly. <a href=\"index.php\">Go back to main page</a>.</center>
lnlaulya

lnlaulya1#

首先,我建议您使用pdo在数据库中插入数据。。。优点是
无需使用mysqli\u real\u escape\u字符串
可读代码

http://php.net/manual/en/pdostatement.bindvalue.php

$db = new PDO('mysql:host=localhost;dbname=DATABASENAME;charset=utf8',$dbname,$dbpass);
try{
$request=$db->prepare('INSERT INTO tbl_event (room,lastname,name,arrival,departure,event_type,assigned_user,department,comment,action,user,update_user,call_time,update_time,status,hotel_id) VALUES (:room,:lastname,:name......:hotel_id)');

$request->bindValue(':room',$_POST["roomno"],PDO::PARAM_STR); 
$request->bindValue(':lastname',$_POST["lastname"],PDO::PARAM_STR); 
.... 
$request->bindValue(':hotel_id',$_SESSION['hotel_id'],PDO::PARAM_INT); 

$result = $request->execute();
} catch(PDOException $e){
    var_dump($e);
}

然后在您的示例中,尝试打印sql字符串并在phpmyadmin中准确地使用该字符串来找出发生了什么

echo $eventregister_sql;

尝试使用var\u dump返回值来查看发生了什么

var_dump($eventregister_result);

或者

echo mysqli_error($db);

相关问题