我有两个问题,并且两个问题都是相关的。首先,如果两个客户选择相同的产品(pg_no)和日期(Date),则不会出现“已预订 - 请选择其他日期”。这些字段是唯一约束。
其次,当插入或提交数据时,localhost显示完整的地址
http://localhost/lstcomp/index.php?note=submitted 。但是当它失败时,本地主机仅显示:http://localhost/lstcomp/
<?php
//connecting string
include("dbconnect.php");
//assigning
$name = $_REQUEST['Name'];
$tele = $_REQUEST['Tele'];
$city = $_REQUEST['City'];
// UNIQUE CONSTRAINT
$pg_no = $_REQUEST['pg_no']; //product
$date = $_REQUEST['Date']; //date
//checking if pg_no and Date are same
$check = mysqli_query($db_connect, "SELECT * FROM lstclient WHERE pg_no='{$pg_no}', Date='{$date}'");
{
echo "Already booked please select another date<br/>";
}
//if not same then insert data
else
{
$query = mysqli_query($db_connect, "INSERT INTO lstclient(pg_no,Name,Tele,City,Date) VALUES('$pg_no','$name','$tele','$city','$date')") or die(mysql_error());
}
mysqli_close($db_connect);
// messaging
if ($query) {
header("location:index.php?note=failed");
} else {
header("location:index.php?note=success");
}
?>
最佳答案
有几个问题:
由于悬空
else
,文件无法解析;它不与 if 语句配对。{ echo "Already booked please select another date<br/>"; } //if not same then insert data else { $query = mysqli_query($db_connect, "INSERT INTO lstclient(pg_no,Name,Tele,City,Date) VALUES('$pg_no','$name','$tele','$city','$date')") or die(mysql_error()); }
看起来您打算使用 SELECT 语句 (
$check
) 的结果,但是...SELECT 语句无效; WHERE 子句用 AND 或 OR 分隔,而不是逗号。
当将行插入到数据库表中时,你会死在
mysql_error
上。当您使用mysqli
时扩展名。您很容易受到 SQL 注入(inject)攻击;如果
pg_no
恰好是'; DELETE FROM users; --
,例如,您的用户表将被删除。您已经在使用mysqli
扩展,因此使用参数绑定(bind)和准备好的语句。 Reference
关于php 和 mysql 请选择其他日期 - 消息未出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44973034/