当我尝试使用此代码时,它会显示错误:
您的SQL语法有错误;检查与您的MySQL服务器版本相对应的手册,以便在{< INSERT INTO
notifications
附近使用正确的语法(title,date,group_id,owner_id)VALUES(' Een nie' at line 1
$date = time();
$gid = 123;
$q = "INSERT INTO `notifications` (title,date,group_id,owner_id) VALUES ";
$zw = mysqli_query($database->connection,"SELECT `owner_id` FROM `joins` WHERE `group_id` = '$gid'");
$ch = mysqli_num_rows($zw);
while($gg = mysqli_fetch_array($zw))
{
$owner_idd = $gg['owner_id'];
$q .= "('xxxxxEen nieuwe aanbieding voor groep $gid','$date','$gid','$owner_idd'),";
}
$q .= rtrim($q,',');
mysqli_query($database->connection, $q) or die (mysqli_error($database->connection));
错误在哪里?
答案 0 :(得分:0)
试试(使用PDO你可以把它改成mysqli _):
<强>的index.php 强>
<?php
$pdo = new PDO('mysql:host=localhost;dbname=test', 'root', '', array(
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"
));
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$date = time();
$gid = 2;
$sth = $pdo->prepare("SELECT * FROM joins WHERE `group_id` = '$gid'");
$sth->execute();
$data = $sth->fetchAll(PDO::FETCH_ASSOC);
$values = array();
if (count($data)) {
foreach ($data as $dataIndex => $dataValue) {
$values[] = "('title','$date'," . implode(",", array_values($dataValue)) . ")";
}
$values = implode(",", $values);
$insert = "INSERT INTO notifications (title,date,group_id,owner_id) VALUES $values";
$sth = $pdo->prepare($insert);
$sth->execute();
}
通知表创建:
CREATE TABLE `notifications` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`title` VARCHAR(255) NOT NULL DEFAULT '0',
`date` INT(11) NOT NULL DEFAULT '0',
`group_id` INT(11) NOT NULL DEFAULT '0',
`owner_id` INT(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB;
加入表格创建:
CREATE TABLE `joins` (
`group_id` INT(11) NULL DEFAULT NULL,
`owner_id` INT(11) NULL DEFAULT NULL,
INDEX `group_id` (`group_id`),
INDEX `owner_id` (`owner_id`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB;
答案 1 :(得分:0)
根据您的mysql查询错误消息&#34;&#39; INSERT INTO通知(title,date,group_id,owner_id)VALUES(&#39; Een nie&#39;在第1行和第34行; 单引号创建问题。
在将数据推送到插入查询之前,尝试添加mysqli_real_escape_string($ con,&#34; dd&#39; sdfsdfsdf&#34;)。