Running multiple queries in php

后端 未结 2 1022
甜味超标
甜味超标 2021-01-22 16:29

I\'m really new in PHP and HTML. After pressing the submit button I\'m trying to populate the fields with the data that is already in the Users MySQL table (this works). I also

2条回答
  •  粉色の甜心
    2021-01-22 16:50

    First of all don't use the variables directly inside the query. For security purpose, prepared statement is highly recommended now a days.

    So, change your query like this and as you are executing two queries one after another at the same time, it is necessary to name the variables to different name else the later will overwrite the previous one:

    $query1 = "SELECT * FROM Users WHERE user_id = ? LIMIT 1";
    $query2 = "INSERT INTO scan (user_id, osha, firstname, lastname, company, trade, email, picture) SELECT user_id, osha, firstname, lastname, company, trade, email, picture FROM Users WHERE user_id = ? LIMIT 1";
    

    Then create prepared statement like below:

    $stmt = mysqli_stmt_init($connect);
    $stmt2 = mysqli_stmt_init($connect);
    
    mysqli_stmt_prepare($stmt, $query1);
    mysqli_stmt_prepare($stmt2, $query2);
    
    mysqli_stmt_bind_param($stmt, "s", $user_id);
    mysqli_stmt_bind_param($stmt2, "s", $user_id);
    

    Then execute the queries:

    mysqli_stmt_execute($stmt);
    mysqli_stmt_execute($stmt2);
    

    Finally, you get the results of $query1 by this:

    $result = mysqli_stmt_get_result($stmt);
    

提交回复
热议问题