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

Posted by Derek on Stack Overflow See other posts from Stack Overflow or by Derek
Published on 2010-03-15T17:30:47Z Indexed on 2010/03/15 17:49 UTC
Read the original article Hit count: 737

Filed under:
|
|
|

Hi, basically, I have 3 tables; users and projects (which is a many-to-many relationship), then I have 'usersprojects' to allow the one-to-many formation. When a user adds a project, I need the project information stored and then the 'userid' and 'projectid' stored in the usersprojects table. It sounds like its really straight forward but I'm having problems with the syntax I think!?

As it stands, I have this as my INSERT queries (values going into 2 different tables):

$project_id = $_POST['project_id'];
    $projectname = $_POST['projectname'];
    $projectdeadline = $_POST['projectdeadline'];
    $projectdetails = $_POST['projectdetails'];

    $user_id = $_POST['user_id'];

$sql = "INSERT INTO projects (projectid, projectname, projectdeadline, projectdetails) VALUES
       ('{$projectid}','{$projectname}','{$projectdeadline}','{$projectdetails}')";

    $sql =  "INSERT INTO usersprojects (userid, projectid) VALUES
       ('{$userid}','{$projectid}')";

None of the information is being stored in the projects table, but the user ID is being stored in the usersprojects table (but not project ID!?)... I did have it working where the project information is stored correctly with a project ID, before I added this bit:

 $sql = "INSERT INTO usersprojects (userid, projectid) VALUES
   ('{$userid}','{$projectid}')";

But before the code above was put in, obviously no info is being stored in usersprojects table.

The source code that links the script:

<form id="addform" name="addform" method="POST" action="addproject-run.php"> 
        <label>Project Name:</label> <input name="projectname" size="40" id="projectname" value="<?php if (isset($_POST['projectname'])); ?>"/><br />
        <input name="user_id" input type="hidden" size="40" id="user_id" value="<?php echo $_SESSION['SESS_USERID']; ?>"/> 

        <label>Project Deadline:</label> <input name="projectdeadline" size="40" id="projectdeadline" value="In the format of 'YYYY-MM-DD'<?php if (isset($_POST['projectdeadline'])); ?>"/><br />
        <label>Project Details:</label> <textarea rows="5" cols="20" name="projectdetails" id="projectdetails"><?php if (isset($_POST['projectdetails'])); ?></textarea>
        <br />
        <br />
        <input value="Create Project" class="addbtn" type="submit" /> 
        </form></div>

So I think I'm right in saying I have the syntax for the SQL statement to be run an insert query of values into 2 tables? Any help is much appreciated! Thanks.

© Stack Overflow or respective owner

Related posts about mysql

Related posts about sql