Page 1 of 1

Inserting multiple values into relational database

Posted: Tue Jun 23, 2009 7:53 pm
by jballou
Hey!

So- I'm trying to take data from a form and insert it into a few different tables. I'm also trying to use the auto incremented IDs that those inserts are generating to then insert into another table. Here's the code I'm using so far that isn't working.

Code: Select all

<?php
 
if(isset($_POST['addContent'])) {
 
    require("sources/connection.php");
    
    $title = $_POST['title'];
    $content = $_POST['content'];
    $thumbnail = $_FILES['thumbnail']['name'];
    $date_posted = $_POST['timestamp'];
    $keyword = $_POST['keyword'];
 
    $sql = "INSERT INTO project VALUES ('', '$title', '$thumbnail', '$content', '$date_posted')";
    $result = $conn->query($sql) or die(mysqli_error());
    $projectid = mysql_insert_id();
    
    
    $sql2 = "INSERT INTO keywords VALUES ('', '$keyword')";
    $result2 = $conn->query($sql2) or die(mysqli_error());
    $keywordid = mysql_insert_id();
    
    $sql3 = "INSERT INTO keywordlink VALUES ('$projectid', '$keywordid')";
    $result3 = $conn->query($sql2) or die(mysqli_error());
    
 
    if($result && $result2){
        header("location: addNewProject.php?message=1");
    }
}
 
?>
I tried echoing out the variables $projectid and $keywordid and I get nothing. The data for the first 2 inserts is going in the database just fine, so I'm wondering where my error is...

can anybody help me out?
Thanks so much!

Re: Inserting multiple values into relational database

Posted: Tue Jun 23, 2009 8:49 pm
by Christopher
I see $conn, mysqli and mysql being used together! I would recommend using $conn for everything if possible (add methods if necessary ;)).

Code: Select all

    $result = $conn->query($sql) or die(mysqli_error());
     $projectid = mysql_insert_id();
 

Re: Inserting multiple values into relational database

Posted: Wed Jun 24, 2009 12:02 am
by jballou
Hey there, forgive me- I'm really a beginner. I'm not sure how the different things you mentioned impact my code.... how would I use $conn for everything?

Re: Inserting multiple values into relational database

Posted: Wed Jun 24, 2009 1:26 am
by Christopher
jballou wrote:Hey there, forgive me- I'm really a beginner. I'm not sure how the different things you mentioned impact my code.... how would I use $conn for everything?
Well I don't know what extension that $conn uses internally, but the other two calls are to separate extensions mysqli_error() is the mysqli extension, where mysql_insert_id() is the mysql extension. You can't mix them. They are totally different libraries.

By using $conn I mean, either using $conn->error() and $conn->insertId() if they are available -- or writing them for your use. Then you are sure that all calls are to the same extension.

Re: Inserting multiple values into relational database

Posted: Wed Jul 01, 2009 5:43 pm
by jballou
$conn->insertId() worked perfectly! Thanks so much. I must have been on mysqli