防止此php代码中的sql注入

防止此php代码中的sql注入,php,mysql,sql,sql-injection,Php,Mysql,Sql,Sql Injection,我想在下面的代码中询问如何修复sql注入。如果我是正确的,我认为代码很容易受到sql注入的攻击 $result = mysql_query("SELECT * FROM newsevent order by date DESC"); 这是完整的代码。这个文件来自newsview.php?id=200。任何人都可以从下面的代码中给出建议,并解释为什么下面的代码会导致sql注入 <?php $page="video"; require('include/header.php');

我想在下面的代码中询问如何修复sql注入。如果我是正确的,我认为代码很容易受到sql注入的攻击

$result = mysql_query("SELECT * FROM newsevent order by date DESC"); 
这是完整的代码。这个文件来自newsview.php?id=200。任何人都可以从下面的代码中给出建议,并解释为什么下面的代码会导致sql注入

<?php 
$page="video";  
require('include/header.php');  
require('include/config.php'); 
session_start(); 

$result = mysql_query("SELECT * FROM newsevent order by date DESC"); 
?> 

<section id="wrappermain"> 
    <div class="wrapper"> 
        <div class="gray-top">&nbsp;</div> 
          <div class="content"> 

        <h2>News and Events</h2> 

            <div id="video"> 
                <?php 
                    while($row = mysql_fetch_assoc($result)) 
                    { 
                        $no+= 1; 
                        $clk = $row['date']; 

                ?> 

                        <a class="h-cat" href = "newsview.php?id=<?php echo $row['id']; ?>"><?php echo $row['title']; ?></a> ( <a> <?php $date1 = $clk;    $date2 = time(); require('include/timestamp.php'); ?> </a>) 


                    <div class="vline">             
                    <div class="v-img"> 
                        <a href = "newsview.php?id=<?php echo $row['id']; ?>"> <img src = "images/newsandevent/<?php echo $row['image']; ?>" style = "height: 150px; width: 250px;" /> </a>    
                    </div> 
                    <div class="v-des">     
                        <?php echo substr($row['de'],0,225); ?><br/><span style="float:right;padding: 10px 0px;"> <a href = "newsview.php?id=<?php echo $row['id']; ?>"> Read more ... </a> </span>       
                     </div> 
                    </div> 
            <?php } ?> 

            </div> 

            <div class="right"> 
                <div class="white-lt"> 
                <div class="white-rt"></div> 
                    <div class="white-m"> 

                         <strong></strong> 

                        <div style="width: 150px;"> 
                        <?php $result = mysql_query("SELECT * FROM newslink order by id DESC"); 
                        while($row = mysql_fetch_assoc($result)) 
                        { 
                        ?> 

                        <?php echo $row['de']; ?> 


                        <?php } ?>  
                      </div>                                   
                   </div> 
                <div class="white-lb"> 
                <div class="white-rb"></div>  
             </div>    
            </div> 
            </div> 


            </div> 
         <div class="gray-bot">&nbsp;</div> 
    </div> 
</section> 
<?php  
require('include/footer.php');  
?> 

新闻和事件
(     


由于您没有在查询中进行任何输入,因此sql注入没有问题。但是如果您在查询中使用任何数据(变量),则必须注意sql注入

<?php 
$page="video";  
require('include/header.php');  
require('include/config.php'); 
session_start(); 

$result = mysql_query("SELECT * FROM newsevent order by date DESC"); 
?> 

<section id="wrappermain"> 
    <div class="wrapper"> 
        <div class="gray-top">&nbsp;</div> 
          <div class="content"> 

        <h2>News and Events</h2> 

            <div id="video"> 
                <?php 
                    while($row = mysql_fetch_assoc($result)) 
                    { 
                        $no+= 1; 
                        $clk = $row['date']; 

                ?> 

                        <a class="h-cat" href = "newsview.php?id=<?php echo $row['id']; ?>"><?php echo $row['title']; ?></a> ( <a> <?php $date1 = $clk;    $date2 = time(); require('include/timestamp.php'); ?> </a>) 


                    <div class="vline">             
                    <div class="v-img"> 
                        <a href = "newsview.php?id=<?php echo $row['id']; ?>"> <img src = "images/newsandevent/<?php echo $row['image']; ?>" style = "height: 150px; width: 250px;" /> </a>    
                    </div> 
                    <div class="v-des">     
                        <?php echo substr($row['de'],0,225); ?><br/><span style="float:right;padding: 10px 0px;"> <a href = "newsview.php?id=<?php echo $row['id']; ?>"> Read more ... </a> </span>       
                     </div> 
                    </div> 
            <?php } ?> 

            </div> 

            <div class="right"> 
                <div class="white-lt"> 
                <div class="white-rt"></div> 
                    <div class="white-m"> 

                         <strong></strong> 

                        <div style="width: 150px;"> 
                        <?php $result = mysql_query("SELECT * FROM newslink order by id DESC"); 
                        while($row = mysql_fetch_assoc($result)) 
                        { 
                        ?> 

                        <?php echo $row['de']; ?> 


                        <?php } ?>  
                      </div>                                   
                   </div> 
                <div class="white-lb"> 
                <div class="white-rb"></div>  
             </div>    
            </div> 
            </div> 


            </div> 
         <div class="gray-bot">&nbsp;</div> 
    </div> 
</section> 
<?php  
require('include/footer.php');  
?> 
您可以使用防止sql注入

这里使用的查询类似于
SELECT*FROM newsfevent order by date DESC
。在这种情况下,没有变量传递给查询。因此,查询中永远不会有任何修改。但是如果使用这样的查询

从newsevent order by date DESC中选择*,其中某些列=“$variable”

这里说,
$variable
是一个可以替换值的数据。因此,您必须注意这里的sql注入。此外,您还可以检查以下答案,这是sql注入情况下的最佳答案


  • SQL注入在这种情况下不适用,因为您没有在SQL语句中插入用户数据


    但是,如果您的表数据是用户输入的,则您的代码易受PHP攻击。

    您的代码不易受攻击,因为您没有向查询传递任何用户数据。但是,您使用的是一个不推荐的数据库API:在这个网站上,您应该先搜索类似的问题,然后再提问。注意:停止使用不推荐的
    mysql.*
    funActions.使用或。如果您在数据库中观察到SQL注入,请查看header.php、config.php和footer.php。这些包含的文件可能与名称不符。@zer0fl4g您在问题中发布的查询不适用于SQL注入,因为没有用户数据。正如下面的答案中所指出的,任何查询哪一个传递用户数据对注入是脆弱的