【问题标题】:Not finding the right syntax to use near '1' at line 1 [mysqli] [closed]在第 1 行的“1”附近找不到正确的语法 [mysqli] [关闭]
【发布时间】:2020-02-18 10:59:28
【问题描述】:

我明白了

"您的 SQL 语法有错误;请查看手册 对应于您的 MySQL 服务器版本,以便使用正确的语法 在第 1 行的 '1' 附近"

运行时的消息

private function saveBookingData(){
        global $mysqli;
        $sql = $mysqli->query("INSERT INTO bsi_bookings 
                    (booking_id, booking_time, start_date, end_date, client_id, 
                     total_cost, payment_amount, payment_type, special_requests) 
                values(".$this->bookingId.", NOW(), 
                        '".$this->mysqlCheckInDate."', 
                        '".$this->mysqlCheckOutDate."', 
                        ".$this->clientId.", 
                        ".$this->grandTotalAmount.", 
                        ".$this->totalPaymentAmount.", 
                        '".$this->paymentGatewayCode."', 
                        '".$this->clientdata['message']."')");

        foreach($this->reservationdata as $revdata){
            foreach($revdata['availablerooms'] as $rooms){              
                $sql = $mysqli->query("INSERT INTO bsi_reservation 
                              (bookings_id, room_id, room_type_id) 
                          values(".$this->bookingId.",  ".$rooms['roomid'].", 
                              ".$revdata['roomtypeid'].")");
            }   
        }
        if (mysqli_connect_errno()) {
              printf("Connect failed: %s\n", mysqli_connect_error());
              exit();
        }

        $result = $mysqli->query($sql);
        if (!$result) {
             printf("%s\n", $mysqli->error);
             exit();
        }
    }   

其他信息
表一

CREATE TABLE `bsi_bookings` (
        `booking_id` int(10) UNSIGNED NOT NULL,
        `booking_time` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
        `start_date` date NOT NULL DEFAULT '0000-00-00',
        `end_date` date NOT NULL DEFAULT '0000-00-00',
        `client_id` int(10) UNSIGNED DEFAULT NULL,
        `child_count` int(2) NOT NULL DEFAULT '0',
        `extra_guest_count` int(2) NOT NULL DEFAULT '0',
        `discount_coupon` varchar(50) DEFAULT NULL,
        `total_cost` decimal(10,2) UNSIGNED NOT NULL DEFAULT '0.00',
        `payment_amount` decimal(10,2) NOT NULL DEFAULT '0.00',
        `payment_type` varchar(255) NOT NULL,
        `payment_success` tinyint(1) NOT NULL DEFAULT '0',
        `payment_txnid` varchar(100) DEFAULT NULL,
        `paypal_email` varchar(500) DEFAULT NULL,
        `special_id` int(10) UNSIGNED NOT NULL DEFAULT '0',
        `special_requests` text,
        `is_block` tinyint(4) NOT NULL DEFAULT '0',
        `is_deleted` tinyint(4) NOT NULL DEFAULT '0',
        `block_name` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

表 2

CREATE TABLE `bsi_reservation` (
        `id` int(11) NOT NULL,
        `bookings_id` int(11) NOT NULL,
        `room_id` int(11) NOT NULL,
        `room_type_id` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

【问题讨论】:

  • 你能把data typesdatabase attributes 发上来吗?
  • @Robin Gillitzer 好的
  • 你看我的回答了吗?我认为这是解决方案。
  • @rick 您的代码对 SQL 注入开放(或者假设实际上 checkInDate 或 checkOutDate 值可能来自用户输入。无论哪种方式,您都应该使用参数作为标准做法,以避免任何疑问)。
  • @rick 在bobby-tables.com/php 有一个关于 PDO(和其他库)的简单示例,bobby-tables.com 有一个卡通片,它简单而有趣地解释了 SQL 注入是如何发生的,以及风险是。对查询进行参数化是防止它的唯一可靠方法。

标签: php mysql mysqli


【解决方案1】:

您可以使用 PHP MySQLi Prepared Statements 来防止 SQL 注入

解释:

$stmt = $mysqli->prepare("INSERT INTO bsi_bookings (booking_id, booking_time, start_date, end_date, client_id, total_cost, payment_amount, payment_type, special_requests) VALUES (?, ?, ?, ...)");
$stmt->bind_param("si", $this->bookingId, $var2, ....);
$stmt->execute();
$stmt->close();

Read more

【讨论】:

  • 我应该在我的表中为 datetime 数据类型添加什么?还是十进制?
  • @rick 对两者都使用“s”。实际上,您可以将“s”用于任何类型
  • 绝对不是 OP 的问题。
  • @你的常识我明白了,谢谢
  • @RobinGillitzer 绝对 唯一 OP 可接受的解决方案
猜你喜欢
  • 2018-07-01
  • 2019-08-17
  • 1970-01-01
  • 2019-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-30
  • 1970-01-01
相关资源
最近更新 更多