I'm working on a news system for a website. I used the news system from this tutorial: http://pixelcode.co.uk/tutorials/php/mysql-news-system/
我正在为一个网站开发一个新闻系统。我使用了本教程中的新闻系统:http://pixelcode.co.uk/tutorials/php/mysql-news-system/
So to display the news I use this line of code:
为了显示新闻,我用了这一行代码:
if(!$_GET['id'])
{
$query = mysql_query("SELECT * FROM news ORDER BY id DESC");
while($output = mysql_fetch_assoc($query))
{
echo '<div id="nieuws">';
echo '<a href="?id='.$output['id'].'" id="link">';
echo '<h1>'.$output['title'].'</h1>';
echo '<span id="date">'.date('d-m-y', $output['date']).'</span><br / >';
echo $output['shortnews'].'<br / >';
echo '</a>';
echo '</div>';
}
}
else
{
$id = $_GET['id'];
$query = mysql_query("SELECT * FROM news WHERE id='$id'");
$output = mysql_fetch_assoc($query);
?>
<form method="post" action="?id=<? echo $output['id']; ?>">
<h1><? echo $output['title']; ?></h1>
<? echo '<span id="date">'.date('d-m-y', $output['date']).'</span><br / >' ?>
<? echo $output['news']; ?>
</form>
<?php } ?>
I can do: if(!$_GET['id'] = 6)
but I can't do this: if(!$_GET['id'] > 6)
. What's the problem? Or is there another code for bigger then an id?
我能做的:如果(!$_GET['id'] = 6)但我不能这么做:if(!$_GET['id'] > 6).有什么问题吗?或者还有比id更大的代码吗?
Thanks for replying, MARCH
谢谢回复,3月
2 个解决方案
#1
4
Operator precedence: !
binds tighter than ==
, so you're effectively doing
运算符优先级:!绑紧比==,所以你实际上在做。
(not($_GET) == 6)
That's why there's !=
for inequality tests.
这就是为什么不平等测试有!=。
If you'd used proper bracketing, e.g
如果你用了合适的括号,例如
(!($_GET['id'] == 6))
then you'd be doing
然后你会做什么
not(id == 6)
and get your expected results, because that's logically/functionally equivalent to id != 6
并得到预期结果,因为这在逻辑上/功能上等同于id != 6
And note that you're vulnerable to sql injection attacks and are using an obsolete/deprecated DB interface.
注意,您很容易受到sql注入攻击,并且正在使用过时的/不赞成使用的DB接口。
#2
0
Use isset()
:
使用收取():
if (isset($_GET['id'])) ...
Or, what I like to do:
或者,我想做的是:
// Get all possible arguments:
$id = @$_GET['id']; // The @ prevents an error message
$foo = @$_GET['foo']; // foo means ... (explain the args)
...
// Later, as needed:
if (isset($id)) ...
#1
4
Operator precedence: !
binds tighter than ==
, so you're effectively doing
运算符优先级:!绑紧比==,所以你实际上在做。
(not($_GET) == 6)
That's why there's !=
for inequality tests.
这就是为什么不平等测试有!=。
If you'd used proper bracketing, e.g
如果你用了合适的括号,例如
(!($_GET['id'] == 6))
then you'd be doing
然后你会做什么
not(id == 6)
and get your expected results, because that's logically/functionally equivalent to id != 6
并得到预期结果,因为这在逻辑上/功能上等同于id != 6
And note that you're vulnerable to sql injection attacks and are using an obsolete/deprecated DB interface.
注意,您很容易受到sql注入攻击,并且正在使用过时的/不赞成使用的DB接口。
#2
0
Use isset()
:
使用收取():
if (isset($_GET['id'])) ...
Or, what I like to do:
或者,我想做的是:
// Get all possible arguments:
$id = @$_GET['id']; // The @ prevents an error message
$foo = @$_GET['foo']; // foo means ... (explain the args)
...
// Later, as needed:
if (isset($id)) ...