Run MySQL INSERT Query multiple times (insert values into multiple tables)

后端 未结 5 1070
失恋的感觉
失恋的感觉 2021-01-24 05:07

basically, I have 3 tables; users and projects, then I have \'users_projects\' to allow the one-to-many formation. When a user adds a project, I need the project information st

5条回答
  •  不思量自难忘°
    2021-01-24 05:30

    You seem to be trying to execute mysql_query() only once. You have two queries, so it needs to be used twice, once on each query:

    $sql = "INSERT INTO projects (projectid, projectname, projectdeadline, projectdetails) VALUES
       ('{$projectid}','{$projectname}','{$projectdeadline}','{$projectdetails}')";
    $result = mysql_query($sql, $connection)
        or die("MySQL Error: ".mysql_error());
    
    $sql =  "INSERT INTO usersprojects (userid, projectid) VALUES
       ('{$userid}','{$projectid}')";
    $result = mysql_query($sql, $connection)
        or die("MySQL Error: ".mysql_error());
    

    Alternatively, you could use mysqli_multi_query(), but that might require a significant rewrite of your code to use the mysqli interface.

提交回复
热议问题