0

I know this is common to ask but I'm newbie in PHP, The problem I've encountered is that the multiple Insert queries do not work in my current code, it works if I've removed at least one query Insert but what if I have multiple queries and CRUD it to another table, I just want to combine query when inserting data to different table, Any help can do

function signUp($table, $id_no, $middlename, $lastname)
{
    $id_no = $this->prepareData($id_no);
    $middlename = $this->prepareData($middlename);
    $lastname = $this->prepareData($lastname);
    $table2 = "users";
    $userrole = 0;
    $username = "test_username";
    $password = "test_password";

    $this->sql =
        "INSERT INTO " . $table . " (id_no, middle_name, last_name) VALUES ('" . $id_no . "','" . $middlename . "','" . $lastname . "')" ; //IF I removed this first query it will work to second table
        "INSERT INTO " . $table2 . " (user_role, username, password) VALUES ('" . $userrole . "','" . $username . "','" . $password . "')";
    if (mysqli_query($this->connect, $this->sql)) {
        return true;
    } else return false;
}

Updated

    function userDetailsTable($table, $firstname,$middlename, $lastname, $sex, $id_no,  $email, $contactno, $address)
{
    $firstname = $this->prepareData($firstname);
    $middlename = $this->prepareData($middlename);
    $lastname = $this->prepareData($lastname);
    $sex = $this->prepareData($sex);
    $id_no = $this->prepareData($id_no);
    $email = $this->prepareData($email);
    $contactno = $this->prepareData($contactno);
    $address = $this->prepareData($address);

    $result =sql2("SELECT MAX(id) FROM users");  // returns error
    
    $this->sql =
        "INSERT INTO " . $table . " (user_id,first_name, middle_name, last_name, sex, id_no, email, contact_no, address) VALUES ('" . $result . "','" . $firstname . "','" . $middlename . "','" . $lastname . "','" . $sex . "','" . $id_no . "','" . $email . "','" . $contactno . "','" . $address . "')" ;
    if (mysqli_query($this->connect, $this->sql)) {
        return true;
    } else return false;
}
14
  • Yes, multiple query doesn't work. So you have to run two queries. As simple as that Commented May 18, 2022 at 16:08
  • 1) Just call mysqli_query twice - once for each insert. Wrap them in a transaction if you want to be sure they both succeed or both fail. 2) BUT... you should not be building queries in this insecure way anyway. Use prepared statements and parameters, always (and just execute two statements, of course :-) ) Commented May 18, 2022 at 16:08
  • 1
    Warning: Your code is vulnerable to SQL Injection attacks. You should use parameterised queries and prepared statements to help prevent attackers from compromising your database by using malicious input values. bobby-tables.com gives an explanation of the risks, as well as some examples of how to write your queries safely using PHP / mysqli. Never insert unsanitised data directly into your SQL. The way your code is written now, someone could easily steal, incorrectly change, or even delete your data. Commented May 18, 2022 at 16:10
  • phpdelusions.net/mysqli also contains good examples of writing safe SQL using mysqli. See also the mysqli documentation and this: How can I prevent SQL injection in PHP? . Parameterising your queries will also greatly reduce the risk of accidental syntax errors as a result of un-escaped or incorrectly quoted input values. If you learnt your current technique from a tutorial or book, please don't use it again. Commented May 18, 2022 at 16:10
  • 2
    Also, please don't store passwords in plain text - that is another security risk. Learn about password hashing instead. See also How to use PHP's password_hash to hash and verify passwords Commented May 18, 2022 at 16:10

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.