使用 MySQLi 更新和插入到 MySQL 时遇到问题
Trouble using MySQLi to update and insert to MySQL
我认为我的语法有错误,但我不确定在哪里。我看过 here for how to insert data, and here for how to update data...
这是 updates/inserts 数据 (PHP) 的片段...
if ($is_edit === true) {
$update_query = "UPDATE `$blog_table` SET `title`=$title_value, `content`=$content_value WHERE $identifier";
$connection->query($update_query);
header('Location: .');
} elseif ($is_edit === false) {
$current_date = date('Y-m-d');
$add_entry_query = "INSERT INTO $blog_table (date, title, content, comments) VALUES ($current_date, $title_value, $content_value, '')";
$connection->query($add_entry_query);
header('Location: .');
}
我知道实际逻辑运行正常,因为当逻辑正确时我得到页面被重定向。
据我所知,我已经完全复制了语法,但没有添加任何数据。
我知道 $blog_table
和各种值变量是正确的,因为我已经回应了它们以查看它们是否有效。我还使用完全相同的 $blog_table
通过相同的连接在同一页面上查看数据,该连接完美无缺。
我的语法有什么问题?
如果您没有像这样转义字符串:$title_value="'something'";
将您的查询更改为
if ($is_edit === true) {
$update_query = "UPDATE `$blog_table` SET `title`='$title_value', `content`='$content_value' WHERE $identifier";
$connection->query($update_query);
header('Location: .');
} elseif ($is_edit === false) {
$current_date = date('Y-m-d');
$add_entry_query = "INSERT INTO $blog_table (date, title, content, comments) VALUES ('$current_date', '$title_value', '$content_value', '')";
$connection->query($add_entry_query);
header('Location: .');
}
还要确保你的琴弦安全,
在 sql
中使用变量之前使用函数 addslashes
$str = addslashes($str);
我认为我的语法有错误,但我不确定在哪里。我看过 here for how to insert data, and here for how to update data...
这是 updates/inserts 数据 (PHP) 的片段...
if ($is_edit === true) {
$update_query = "UPDATE `$blog_table` SET `title`=$title_value, `content`=$content_value WHERE $identifier";
$connection->query($update_query);
header('Location: .');
} elseif ($is_edit === false) {
$current_date = date('Y-m-d');
$add_entry_query = "INSERT INTO $blog_table (date, title, content, comments) VALUES ($current_date, $title_value, $content_value, '')";
$connection->query($add_entry_query);
header('Location: .');
}
我知道实际逻辑运行正常,因为当逻辑正确时我得到页面被重定向。
据我所知,我已经完全复制了语法,但没有添加任何数据。
我知道 $blog_table
和各种值变量是正确的,因为我已经回应了它们以查看它们是否有效。我还使用完全相同的 $blog_table
通过相同的连接在同一页面上查看数据,该连接完美无缺。
我的语法有什么问题?
如果您没有像这样转义字符串:$title_value="'something'"; 将您的查询更改为
if ($is_edit === true) {
$update_query = "UPDATE `$blog_table` SET `title`='$title_value', `content`='$content_value' WHERE $identifier";
$connection->query($update_query);
header('Location: .');
} elseif ($is_edit === false) {
$current_date = date('Y-m-d');
$add_entry_query = "INSERT INTO $blog_table (date, title, content, comments) VALUES ('$current_date', '$title_value', '$content_value', '')";
$connection->query($add_entry_query);
header('Location: .');
}
还要确保你的琴弦安全, 在 sql
中使用变量之前使用函数 addslashes$str = addslashes($str);