【问题标题】:unable to retrieve sql data to use in another query [duplicate]无法检索要在另一个查询中使用的 sql 数据 [重复]
【发布时间】:2020-07-21 00:16:25
【问题描述】:

我正在努力解决一个问题,也许有人可以帮助我:)

在第一个查询中,我想将数据发送到为我生成 id 的第一个表,在第二个 quary 我想检索第一个 quary 生成的 id,在第三个 quary 我想使用该 id在第二个表中插入与第一个表相同 id 的其他数据。

我的问题是:除了 $id 之外,所有东西都可以正常插入,我错过了什么吗?

<?php
    session_start();
    //declaratie sessie variabelen
    $username = $_SESSION['username'];
    $voornaam = $_SESSION['voornaam'];
    $achternaam = $_SESSION['achternaam'];
    $firma = $_SESSION['firma'];

    //database configuratie file
    require('dbconfig.php');

    //Declaratie post variabelen
    $ticnaam = $_POST['ticket_naam'];
    $ticonderwerp = $_POST['ticket_onderwerp'];
    $ticassign = $_POST['ticket_voor'];
    $bericht = $_POST['bericht'];

    //proccesing


    //procces first quary
    $sql = "INSERT INTO `tickets` 
                        (`naam`, `onderwerp`, `maker`) 
                VALUES ('$ticnaam', '$ticonderwerp', '$username')";

    //retrieves the generated new id from the quary above
    $sql2 = "SELECT id FROM tickets where onderwerp='$ticonderwerp' AND naam = '$ticnaam';";
    $result2 = mysqli_query($mysqli,$sql2);
    $id = mysqli_fetch_array($result2,MYSQLI_ASSOC);


    //insertes the id into another quary
    $sql3 = "INSERT INTO `berichten` 
                        (`id`, `text`, `voornaam`,`achternaam`,`firma`) 
                VALUES ('$id', '$bericht', '$voornaam',
                        '$achternaam','$firma')";

    //sql3 ok? user can continu
    if($mysqli->query($sql3) == TRUE) {
        require('email_na_ticketaanmaak.php')
?>
<script>alert('nieuw ticket is gemaakt');</script>
<?php
        require('../procces_files/email_na_ticketaanmaak.php');
        header('Location: ../home/index.php');
    }else{
        echo "Error: " . $sql . "<br>" . $mysqli->error;
    }
    $mysqli->close();
?>

【问题讨论】:

  • 你需要mysqli::$insert_id See manual page here
  • 一方面,您从未对$sql 执行过INSERT 查询。
  • 使用准备好的语句来防止 SQL 注入。
  • @FunkFortyNiner 哦,是的,错过了,我需要停下来喝下午茶
  • @RiggsFolly Heh,在这些艰难时期全神贯注是正常的。

标签: php html sql session mysqli


【解决方案1】:

如果id 列是AutoIncrement 列,则mysqli 对象有一个名为$insert_id 的属性将返回新插入行的id

<?php
    session_start();

    //database configuratie file
    require('dbconfig.php');

    // process first query
    $sql = "INSERT INTO `tickets` 
                    (`naam`, `onderwerp`, `maker`) VALUES (?,?,?)";

    $stmt = $mysqli->prepare($sql);
    $stmt->bind_param('sss', $_POST['ticket_naam'],
                            $_POST['ticket_onderwerp'],
                            $_SESSION['username']);
    $stmt->execute();

    // retrieves the generated new id from the query above
    $new_id = $mysqli->insert_id;

    // inserts the id into another query
    $sql = "INSERT INTO `berichten` 
                        (`id`, `text`, `voornaam`,`achternaam`,`firma`) 
                VALUES (?,?,?,?,?)";
    $stmt = $mysqli->prepare($sql);
    $stmt->bind_param('isss', $new_id,
                                $_POST['bericht'],
                                $_SESSION['voornaam'],
                                $_SESSION['achternaam'],
                                $_SESSION['firma']);
    $res = $stmt->execute();
    if ($res) {
        require('email_na_ticketaanmaak.php')
        require('../procces_files/email_na_ticketaanmaak.php');
        header('Location: ../home/index.php');
    }else{
        echo "Error: " . $sql . "<br>" . $mysqli->error;
    }
?>

如果您要使用

重定向到另一个页面
header('Location: ../home/index.php');

没有必要将它发送回您之前所在的页面,因为您永远不会看到它。

<script>alert('nieuw ticket is gemaakt');</script>

您的脚本也对SQL Injection Attack 开放。 甚至if you are escaping inputs, its not safe! 您应该考虑在 MYSQLI_PDO API 中使用 prepared parameterized statements 而不是串联值

所以我稍微更改了代码以使用参数化、准备和绑定的查询。

【讨论】:

    【解决方案2】:

    正如 RiggsFolly 在他的评论中提到的那样:

    $lastIdInserted = $mysqli->insert_id();
    if($lastIdInserted == 0){
    echo("something went wrong);
    }
    

    【讨论】:

      猜你喜欢
      • 2017-11-23
      • 1970-01-01
      • 2014-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-17
      • 1970-01-01
      相关资源
      最近更新 更多