我正在尝试向数据库中添加新行,但是,它没有添加并且没有收到错误。代码可以在本地运行,但不能在网络上运行。
这是代码;
$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>
答案 0 :(得分:-1)
首先,我建议您使用PDO在数据库中插入数据...优点是
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_dump返回值以查看发生了什么
var_dump($eventregister_result);
或
echo mysqli_error($db);