繁体   English   中英

为什么在INSERT INTO后跟SELECT LAST_INSERT_ID()不输出任何内容?

[英]Why is INSERT INTO followed by SELECT LAST_INSERT_ID() not outputting anything?

我使用的PHP代码将前一页的HTML表单数据插入数据库,并在同一SQL语句中从插入的数据返回PostID。 PostID列为AUTO_INCREMENTING 我已经研究了这个问题一两个星期,但没有找到有效的解决方案。

<?php
include("dbconnect.php");
mysql_select_db("astral_database", $con);
session_start();

$username = $_SESSION['username'];
$forumtext = $_POST["forumtext"];
$forumsubject = $_POST["forumsubject"];

$postquery = 'INSERT INTO Forums (Creator, Subject, Content) VALUES ("$username", "$forumsubject", "$forumtext"); SELECT LAST_INSERT_ID()';
$result = mysql_query($postquery, $con);

if (!$con) {
    echo "<b>If you are seeing this, please send the information below to astraldevgroup@gmail.com</b><br>Error (331: dbconnect experienced fatal errors while attempting to connect)";
    die();
}

if ($username == null) {
    echo "<b>If you are seeing this, please send the information below to astraldevgroup@gmail.com</b><br>Error (332: Username was not specified while attempting to send request)";
    die();
}

if ($result != null) {
    echo "last id: " . $result;

    $fhandle = fopen("recentposts.txt", "r+");
    $contents = file_get_contents("recentposts.txt");
    fwrite($fhandle, json_encode(array("postid" => $result, "creator" => $username, "subject" => $forumsubject, "activity" => time())) . "\n" . $contents);
    fclose($fhandle);
    mysql_close($con);
    header("location: http://astraldevgroup.com/forums");
    die();
} else {
    die("<b>If you are seeing this, please send the information below to astraldevgroup@gmail.com</b><br>Error (330: Unhandled exception occured while posting forum to website.)<br>");
    echo mysql_error();
}

mysql_close($con);
?>

首先,mysql_query不会从SELECT语句返回任何内容。 我还没有找到可以在同一查询中同时运行SELECT语句和INSERT语句的任何内容。 如果我尝试在两个不同的语句中运行它们,它仍然不会返回任何内容。 我尝试在SQL控制台中运行以下语句,它运行得很好,没有错误。

INSERT INTO Forums (Creator, Subject, Content) VALUES ("Admin", "Test forum 15", "This is a forum that should give me the post id."); SELECT LAST_INSERT_ID();

mysql_query功能运行多个语句

参考: http : //php.net/manual/en/function.mysql-query.php

mysql_query()向服务器上当前活动的数据库发送一个唯一查询(不支持多个查询)。

这是您对mysql_query的调用未返回结果集的原因之一。

最明显的解决方法是不要尝试在同一查询中运行SELECT 您可以改用对mysql_insert_id的调用。

参考: PHP:mysql_insert_id http://php.net/manual/zh/function.mysql-insert-id.php


对您提出的一些问题的答案:

  • 是的,您的示例代码容易受到SQL Injection的攻击。
  • 是的, 不推荐使用 mysql_接口很长时间了。
  • 是的,您应该使用PDOmysqli接口,而不要使用不mysql_使用的mysql_函数。

跟进

重新访问我的答案,再次查看问题和示例代码。

我之前曾指出,该代码容易受到SQL注入的攻击,因为SQL文本中包含了潜在的不安全值。 这就是快速查看时的样子。

但是再来看一遍,这并不是严格意义上的,因为变量替换实际上并没有发生,因为字符串文字被括在单引号中 考虑一下以下内容的输出:

  $foo = "bar";
  echo '$foo';
  echo '"$foo"';

然后考虑以下代码行分配给$postquery的内容:

$postquery = 'INSERT ... VALUES ("$username", "$forumsubject", "$forumtext")';

修复是使$username被认为是一个变量的引用,而不是字面字符(获得分配到价值 $username纳入SQL文本变量),将介绍SQL注入漏洞。

带绑定占位符的预备语句实际上并不难

$result永远不会为null 它可以是结果句柄,也可以是布尔值false 由于您正在测试错误的值,因此您永远不会看到mysql_query()返回告诉您查询失败的错误信息。

正如其他人指出的那样,您不能在单个query()调用中发出多个查询-这是一种廉价的基本防御措施,可以防止PHP mysql驱动程序中一种形式的SQL注入攻击。 但是,您的其余代码易受其他形式的注入攻击,因此...最好开始阅读: http : //bobby-tables.com

另外,在逻辑方面,为什么要尝试将空用户名插入数据库后再测试空用户名? 运行查询之前,您应该测试/验证这些值。

暂无
暂无

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

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