可以从 QUERY_STRING 插入,但不能从 MySQL 中选择相同的值!

发布于 2024-10-09 23:58:50 字数 1420 浏览 2 评论 0原文

正如您所看到的,它可能没有理由不能工作。我不知道我还能做什么,有什么想法吗?任何帮助表示赞赏!

我想做的就是查看检查在网址末尾输入的值是否与数据库中的值匹配(,它在数据库中。:) 谢谢


The code:

<?php

$keyword = substr($_SERVER['REQUEST_URI'],11); 
    if($_REQUEST['action'] == "link")
    {
        $keyword = $_POST['keyword'];
        $link    = $_POST['link'];

        $connection =
               mysql_connect("my01..com","h","h") or die(mysql_error());

        if($connection)
        {
         mysql_select_db("mysql_17902_h", $connection);

         mysql_query(
                 "INSERT INTO mysql_17902_h.links (
                   link,
                   keyword) VALUES (
                    '".$link."',
                      '".$keyword."')") or die(mysql_error());

            $state = true;
        }
    }
    else
    {
        if(!empty($_POST))
        {
            print_r($keyword);
            $connection =
                   mysql_connect("my01.h.com","h","h") or die(mysql_error());


            if($connection)
            {


                mysql_select_db("mysql_17902_h") or die(mysql_error());
           $result = mysql_query("SELECT link FROM links WHERE keyword = $keyword")
           or die(mysql_error());

           $row = mysql_fetch_array($result);
               $outsy = $row['link'];

           }
           $state = true;
           }

    }
?>

As you can see, there's probably no reason why it shouldn't be working. I don't know what else I can do, any ideas? Any help is appreciated!

All I am trying to do, is view check if the value entered at the end of the url, matches one that is in the database (and yes, it IS in the database. :)
Thank you


The code:

<?php

$keyword = substr($_SERVER['REQUEST_URI'],11); 
    if($_REQUEST['action'] == "link")
    {
        $keyword = $_POST['keyword'];
        $link    = $_POST['link'];

        $connection =
               mysql_connect("my01..com","h","h") or die(mysql_error());

        if($connection)
        {
         mysql_select_db("mysql_17902_h", $connection);

         mysql_query(
                 "INSERT INTO mysql_17902_h.links (
                   link,
                   keyword) VALUES (
                    '".$link."',
                      '".$keyword."')") or die(mysql_error());

            $state = true;
        }
    }
    else
    {
        if(!empty($_POST))
        {
            print_r($keyword);
            $connection =
                   mysql_connect("my01.h.com","h","h") or die(mysql_error());


            if($connection)
            {


                mysql_select_db("mysql_17902_h") or die(mysql_error());
           $result = mysql_query("SELECT link FROM links WHERE keyword = $keyword")
           or die(mysql_error());

           $row = mysql_fetch_array($result);
               $outsy = $row['link'];

           }
           $state = true;
           }

    }
?>

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

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

发布评论

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

评论(1

无边思念无边月 2024-10-16 23:58:50

尝试重写代码,使其更易读:

$link = mysql_real_escape_string($_POST['link']);
$keyword = mysql_real_escape_string($_POST['keyword']);

$sql = <<<EOL;
INSERT INTO mysql_17902_h.links (link, keyword)
VALUES ('$link', '$keyword')
EOL;

mysql_query($sql) or die(mysql_error());

请注意使用 mysql_real_escape_string() 来防止 SQL 注入攻击,并在 SQL 字符串中用单引号将变量括起来。您在这里忽略了这样做:

$result = mysql_query("SELECT link FROM links WHERE keyword = $keyword") or ...
                                                              ^^^^^^^^ 

文本类型字段周围没有引号是语法错误。同样,在代码中的这一点上, $keyword 包含脚本顶部返回的 substr() 调用的任何内容,因此请确保 substr 调用实际上执行了您想要的操作。

Try rewriting your code so it's more legible:

$link = mysql_real_escape_string($_POST['link']);
$keyword = mysql_real_escape_string($_POST['keyword']);

$sql = <<<EOL;
INSERT INTO mysql_17902_h.links (link, keyword)
VALUES ('$link', '$keyword')
EOL;

mysql_query($sql) or die(mysql_error());

Note the use of mysql_real_escape_string() to prevent SQL injection attacks, and surrounding the variables with single quotes within the SQL string. You've neglected to do so here:

$result = mysql_query("SELECT link FROM links WHERE keyword = $keyword") or ...
                                                              ^^^^^^^^ 

No quotes around a text-type field is a syntax error. As well, at that point in the code, $keyword contains whatever the substr() call at the top of the script returned, so make sure that substr call actually does what you're intending.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文