【发布时间】:2013-04-02 18:28:39
【问题描述】:
我正在尝试向 mysql 查询添加 if 语句,以便在查询运行之前检查用户是否存在于 user_id 列中名为 ptb_users 的表中。
用户可以在其他用户墙上写字,在用户可以在用户墙上写字之前/在查询插入 ptb_wallposts 之前,我希望它检查 from_user_id = ptb_users.user_id。
我正在尝试这样做,但我的页面上到处都是语法错误,有人可以告诉我我需要如何构建这个:
<?php
// check if the review form has been sent
if(isset($_POST['review_content']))
{
$content = $_POST['review_content'];
//We remove slashes depending on the configuration
if(get_magic_quotes_gpc())
{
$content = stripslashes($content);
}
//We check if all the fields are filled
if($_POST['review_content']!='')
{
如果存在(从 ptb_users 中选择 user_id,其中 user_id = @id) 开始
{
$sql = "INSERT INTO ptb_wallposts (id, from_user_id, to_user_id, content) VALUES (NULL, '".$_SESSION['user_id']."', '".$profile_id."', '".$content."');";
mysql_query($sql, $connection);
$_SESSION['message']="<div class=\"infobox-wallpost\"><strong>Wall Post Added</strong> - Your comment was posted to {$profile[2]}'s wall.</div><div class=\"infobox-close4\"></div>";
header("Location: {$_SERVER['HTTP_REFERER']}");
} }
}
结束 别的 开始 echo "错误用户不存在" 结束
?>
【问题讨论】:
-
请在你做任何事情之前阅读proper SQL escaping以避免严重损害职业生涯SQL injection bugs。不要在新代码中使用
mysql_query,它很危险,已被弃用,并将从未来的 PHP 版本中删除。 PDO 只需要半个小时 to learn,所以除非你背负着一些遗留项目,否则请改用它。 -
你有什么理由从头开始构建这个应用程序,而不是从 a popular framework 开始?您将犯无数错误,并且必须重新发明已经由专家编码、打包和记录的东西。