【问题标题】:Get ID right after it's set, on same page在同一页面上设置后立即获取 ID
【发布时间】:2013-05-03 01:29:58
【问题描述】:

Thread_Body 中的 ThreadID 需要是 Thread_Titles id 的 id。 我该怎么做呢?

if (isset($_POST['Post_Thread'])) {
    if (empty($_POST['Post_Title'])) {
        echo "No Title No Thread.";
    } else {
        $Sec = mysql_real_escape_string($_GET['sec']);
        mysql_query("INSERT INTO Thread_Titles (Name, Section, Posted, Poster, Updated) VALUES('".cleanPost($_POST['Post_Title'])."', '".$Sec."', '".time()."', '".$ULN->Username."', '".time()."') ") or die(mysql_error()); 
    }
    if (empty($_POST['Post_Body'])) {
        echo "No Body No Thread.";
    } else {
        mysql_query("INSERT INTO Thread_Replies (Body, Posted, Poster, ThreadID) VALUES('".cleanPost($_POST['Post_Body'])."', '".time()."', '".$ULN->Username."', '".$ThreadID."') ") or die(mysql_error()); 
    }
}

这是所有期望表单的代码。

【问题讨论】:

    标签: php mysql last-insert-id


    【解决方案1】:

    使用mysql_insert_id()

    mysql_query("INSERT INTO Thread_Titles (Name, Section, Posted, Poster, Updated) VALUES('".cleanPost($_POST['Post_Title'])."', '".$Sec."', '".time()."', '".$ULN->Username."', '".time()."') ") or die(mysql_error());  
    $ThreadID = mysql_insert_id();
    

    仅供参考,you shouldn't use mysql_* functions in new code。它们不再维护and are officially deprecated。看到red box?改为了解 prepared statements,并使用 PDOMySQLi - this article 将帮助您决定哪个。如果你选择 PDO,here is a good tutorial

    【讨论】:

    • 有人告诉我这个,但我仍然不知道你们在说什么。我还在学习很多东西,我会通过视频和从其他人那里学到的东西……还有 mysql_insert_id() 会做什么?
    • 点击所有链接,您将获得所有答案。 :)
    • 天哪,这么简单?非常感谢你。也感谢您分享我现在将研究的所有其他内容。
    猜你喜欢
    • 2015-10-21
    • 2011-10-27
    • 2022-12-01
    • 2019-02-25
    • 2020-11-16
    • 1970-01-01
    • 2014-09-30
    • 1970-01-01
    • 2018-10-20
    相关资源
    最近更新 更多