php 和 mysql 请选择其他日期 - 消息未出现

标签 php mysql

我有两个问题,并且两个问题都是相关的。首先,如果两个客户选择相同的产品(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");
}

?>

最佳答案

有几个问题:

  1. 由于悬空 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 ) 的结果,但是...

  2. SELECT 语句无效; WHERE 子句用 AND 或 OR 分隔,而不是逗号。

  3. 当将行插入到数据库表中时,你会死在 mysql_error 上。当您使用mysqli时扩展名。

  4. 您很容易受到 SQL 注入(inject)攻击;如果pg_no恰好是'; DELETE FROM users; -- ,例如,您的用户表将被删除。您已经在使用mysqli扩展,因此使用参数绑定(bind)和准备好的语句。 Reference

关于php 和 mysql 请选择其他日期 - 消息未出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44973034/

相关文章:

php - 将 PHP 数组值与字符串的一部分进行比较

mysql - 更新表内连接

php - 从数据库中删除一行的某些部分(一组数据库行中的一个值)

MySQL触发器错误: update another cell on the same row

c# - MySql 查询 - 选择位置、别名和参数

PHP 产品数据库中的三列表

PHP 内容长度 header 延迟加载

php - 从数组中的 COUNT 中排除空值

php - 根据另一列值获取一列的值

mysql - 使用 % 和 IN 子句进行查询