dpa55065 2010-12-31 04:15
浏览 20
已采纳

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

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;
           }

    }
?>
  • 写回答

1条回答 默认 最新

  • douken1726 2010-12-31 05:54
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)