简体   繁体   English

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

[英]Can INSERT from QUERY_STRING but cannot SELECT that same value from 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 我要做的就是查看检查在URL末尾输入的值是否匹配数据库中的值( 是的 ,它在数据库中。):谢谢


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

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. 请注意,使用mysql_real_escape_string()防止SQL注入攻击,并在SQL字符串中用单引号将变量引起来。 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. 同样,在代码的这一点上,$ keyword包含返回的脚本顶部的任何substr()调用,因此请确保substr调用实际上能够满足您的预期。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM