可以在 WHERE 子句中使用 $_POST

发布于 2024-12-11 20:22:38 字数 3818 浏览 0 评论 0原文

对此没有真正直接的答案,所以我想我应该尝试一下。

$myid = $_POST['id'];

       //Select the post from the database according to the id.
   $query = mysql_query("SELECT * FROM repairs WHERE id = " .$myid . " AND name = '' AND email = '' AND address1 = '' AND postcode = '';") or die(header('Location: 404.php'));

上面的代码假设将变量$myid设置为id的发布内容,然后在SQL WHERE子句中使用该变量根据提交的id从数据库中获取数据。忘记潜在的 SQL 注入(我稍后会修复它们),为什么这不起作用?

好的,这是我测试的完整代码:

<?php

//This includes the variables, adjusted within the 'config.php file' and the functions from the 'functions.php' - the config variables are adjusted prior to anything else.
require('configs/config.php');
require('configs/functions.php');

//Check to see if the form has been submited, if it has we continue with the script.
if(isset($_POST['confirmation']) and $_POST['confirmation']=='true')
{
    //Slashes are removed, depending on configuration.
    if(get_magic_quotes_gpc())
    {
        $_POST['model'] = stripslashes($_POST['model']);
        $_POST['problem'] = stripslashes($_POST['problem']);
        $_POST['info'] = stripslashes($_POST['info']);
    }
    //Create the future ID of the post - obviously this will create and give the id of the post, it is generated in numerical order.
    $maxid = mysql_fetch_array(mysql_query('select max(id) as id from repairs'));
    $id = intval($maxid['id'])+1;

    //Here the variables are protected using PHP and the input fields are also limited, where applicable.
    $model = mysql_escape_string(substr($_POST['model'],0,9));
    $problem = mysql_escape_string(substr($_POST['problem'],0,255));
    $info = mysql_escape_string(substr($_POST['info'],0,6000));

    //The post information is submitted into the database, the admin is then forwarded to the page for the new post. Else a warning is displayed and the admin is forwarded back to the new post page. 
    if(mysql_query("insert into repairs (id, model, problem, info) values ('$_POST[id]', '$_POST[model]', '$_POST[version]', '$_POST[info]')"))
    {

?>

<?php

$myid = $_POST['id'];

       //Select the post from the database according to the id.
   $query = mysql_query("SELECT * FROM repairs WHERE id=" .$myid . " AND name = '' AND email = '' AND address1 = '' AND postcode = '';") or die(header('Location: 404.php'));

       //This re-directs to an error page the user preventing them from viewing the page if there are no rows with data equal to the query.
   if( mysql_num_rows($query) < 1 )
{
 header('Location: 404.php');
 exit;
}

   //Assign variable names to each column in the database.
   while($row = mysql_fetch_array($query))
   {
       $model = $row['model'];
       $problem = $row['problem'];
   }

           //Select the post from the database according to the id.
   $query2 = mysql_query('SELECT * FROM devices WHERE version = "'.$model.'" AND issue = "'.$problem.'";') or die(header('Location: 404.php'));

       //This re-directs to an error page the user preventing them from viewing the page if there are no rows with data equal to the query.
   if( mysql_num_rows($query2) < 1 )
{
 header('Location: 404.php');
 exit;
}

   //Assign variable names to each column in the database.
   while($row2 = mysql_fetch_array($query2))
   {
       $price = $row2['price'];
       $device = $row2['device'];
       $image = $row2['image'];
   }

?>  

<?php echo $id; ?>
<?php echo $model; ?>
<?php echo $problem; ?>
<?php echo $price; ?>
<?php echo $device; ?>
<?php echo $image; ?>

    <?  
    }
    else
    {
        echo '<meta http-equiv="refresh" content="2; URL=iphone.php"><div id="confirms" style="text-align:center;">Oops! An error occurred while submitting the post! Try again…</div></br>';
    }
}
?>

There are not really and direct answers on this, so I thought i'd give it a go.

$myid = $_POST['id'];

       //Select the post from the database according to the id.
   $query = mysql_query("SELECT * FROM repairs WHERE id = " .$myid . " AND name = '' AND email = '' AND address1 = '' AND postcode = '';") or die(header('Location: 404.php'));

The above code is supposed to set the variable $myid as the posted content of id, the variable is then used in an SQL WHERE clause to fetch data from a database according to the submitted id. Forgetting the potential SQL injects (I will fix them later) why exactly does this not work?

Okay here is the full code from my test of it:

<?php

//This includes the variables, adjusted within the 'config.php file' and the functions from the 'functions.php' - the config variables are adjusted prior to anything else.
require('configs/config.php');
require('configs/functions.php');

//Check to see if the form has been submited, if it has we continue with the script.
if(isset($_POST['confirmation']) and $_POST['confirmation']=='true')
{
    //Slashes are removed, depending on configuration.
    if(get_magic_quotes_gpc())
    {
        $_POST['model'] = stripslashes($_POST['model']);
        $_POST['problem'] = stripslashes($_POST['problem']);
        $_POST['info'] = stripslashes($_POST['info']);
    }
    //Create the future ID of the post - obviously this will create and give the id of the post, it is generated in numerical order.
    $maxid = mysql_fetch_array(mysql_query('select max(id) as id from repairs'));
    $id = intval($maxid['id'])+1;

    //Here the variables are protected using PHP and the input fields are also limited, where applicable.
    $model = mysql_escape_string(substr($_POST['model'],0,9));
    $problem = mysql_escape_string(substr($_POST['problem'],0,255));
    $info = mysql_escape_string(substr($_POST['info'],0,6000));

    //The post information is submitted into the database, the admin is then forwarded to the page for the new post. Else a warning is displayed and the admin is forwarded back to the new post page. 
    if(mysql_query("insert into repairs (id, model, problem, info) values ('$_POST[id]', '$_POST[model]', '$_POST[version]', '$_POST[info]')"))
    {

?>

<?php

$myid = $_POST['id'];

       //Select the post from the database according to the id.
   $query = mysql_query("SELECT * FROM repairs WHERE id=" .$myid . " AND name = '' AND email = '' AND address1 = '' AND postcode = '';") or die(header('Location: 404.php'));

       //This re-directs to an error page the user preventing them from viewing the page if there are no rows with data equal to the query.
   if( mysql_num_rows($query) < 1 )
{
 header('Location: 404.php');
 exit;
}

   //Assign variable names to each column in the database.
   while($row = mysql_fetch_array($query))
   {
       $model = $row['model'];
       $problem = $row['problem'];
   }

           //Select the post from the database according to the id.
   $query2 = mysql_query('SELECT * FROM devices WHERE version = "'.$model.'" AND issue = "'.$problem.'";') or die(header('Location: 404.php'));

       //This re-directs to an error page the user preventing them from viewing the page if there are no rows with data equal to the query.
   if( mysql_num_rows($query2) < 1 )
{
 header('Location: 404.php');
 exit;
}

   //Assign variable names to each column in the database.
   while($row2 = mysql_fetch_array($query2))
   {
       $price = $row2['price'];
       $device = $row2['device'];
       $image = $row2['image'];
   }

?>  

<?php echo $id; ?>
<?php echo $model; ?>
<?php echo $problem; ?>
<?php echo $price; ?>
<?php echo $device; ?>
<?php echo $image; ?>

    <?  
    }
    else
    {
        echo '<meta http-equiv="refresh" content="2; URL=iphone.php"><div id="confirms" style="text-align:center;">Oops! An error occurred while submitting the post! Try again…</div></br>';
    }
}
?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

流年里的时光 2024-12-18 20:22:38

你的表中的 id 是什么数据类型?您可能需要用单引号将其引起来。

$query = msql_query("SELECT * FROM repairs WHERE id = '$myid' AND...")

编辑:您也不需要使用带双引号字符串的串联。

What data type is id in your table? You maybe need to surround it in single quotes.

$query = msql_query("SELECT * FROM repairs WHERE id = '$myid' AND...")

Edit: Also you do not need to use concatenation with a double-quoted string.

千鲤 2024-12-18 20:22:38
  1. 检查 $myid 的值和整个动态创建的 SQL 字符串,确保它包含您认为包含的内容。

  2. 您的问题很可能是由于对可能包含 NULL 值的列使用空字符串比较而引起的。对所有空字符串尝试 name IS NULL 等。

  1. Check the value of $myid and the entire dynamically created SQL string to make sure it contains what you think it contains.

  2. It's likely that your problem arises from the use of empty-string comparisons for columns that probably contain NULL values. Try name IS NULL and so on for all the empty strings.

澉约 2024-12-18 20:22:38

$myid 为空的唯一原因是浏览器没有发送它。确保您的表单操作设置为 POST。您可以使用以下命令验证 $_POST 中是否存在值:

print_r($_POST);

并且,回显您的查询以确保它符合您的预期。尝试通过 PHPMyAdmin 或 MySQL Workbench 手动运行它。

The only reason $myid would be empty, is if it's not being sent by the browser. Make sure your form action is set to POST. You can verify there are values in $_POST with the following:

print_r($_POST);

And, echo out your query to make sure it's what you expect it to be. Try running it manually via PHPMyAdmin or MySQL Workbench.

浪漫之都 2024-12-18 20:22:38

使用 $something = mysql_real_escape_string($POST['something']);
不仅可以防止 SQL 注入,还可以防止由于人们输入以下数据而导致的语法错误:

name = O'Reilly    <<-- query will bomb with an error
memo = Chairman said: "welcome"   
etc.

因此,为了拥有一个有效且可运行的应用程序,它确实是必不可少的。
“我稍后修复它” 的论点有一些逻辑缺陷:

  • 稍后修复东西会更慢,你会花费更多时间,因为你需要重新访问旧代码。
  • 由于上述功能错误,您将在测试中得到不需要的错误报告。
  • 我稍后再做,事情往往永远不会发生。
  • 安全不是可有可无的,而是必不可少的。
  • 如果您完成了该项目并且其他人必须接管,会发生什么,他(她)将不知道您的未决问题。
  • 如果你做了某件事,就完成它,不要留下各种悬而未决的问题。
  • 如果我是你的老板,对该代码进行了代码审查,你就会当场被解雇。

Using $something = mysql_real_escape_string($POST['something']);
Does not only prevent SQL-injection, it also prevents syntax errors due to people entering data like:

name = O'Reilly    <<-- query will bomb with an error
memo = Chairman said: "welcome"   
etc.

So in order to have a valid and working application it really is indispensible.
The argument of "I'll fix it later" has a few logical flaws:

  • It is slower to fix stuff later, you will spend more time overall because you need to revisit old code.
  • You will get unneeded bug reports in testing due to the functional errors mentioned above.
  • I'll do it later thingies tend to never happen.
  • Security is not optional, it is essential.
  • What happens if you get fulled off the project and someone else has to take over, (s)he will not know about your outstanding issues.
  • If you do something, finish it, don't leave al sorts of issues outstanding.
  • If I were your boss and did a code review on that code, you would be fired on the spot.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文