Inserting multiple values into relational database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
jballou
Forum Newbie
Posts: 15
Joined: Mon Mar 08, 2004 2:34 pm
Location: San Francisco, CA

Inserting multiple values into relational database

Post 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!
Last edited by Benjamin on Tue Jun 23, 2009 11:36 pm, edited 1 time in total.
Reason: Changed code type from text to php.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Inserting multiple values into relational database

Post 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();
 
(#10850)
User avatar
jballou
Forum Newbie
Posts: 15
Joined: Mon Mar 08, 2004 2:34 pm
Location: San Francisco, CA

Re: Inserting multiple values into relational database

Post 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?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Inserting multiple values into relational database

Post 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.
(#10850)
User avatar
jballou
Forum Newbie
Posts: 15
Joined: Mon Mar 08, 2004 2:34 pm
Location: San Francisco, CA

Re: Inserting multiple values into relational database

Post by jballou »

$conn->insertId() worked perfectly! Thanks so much. I must have been on mysqli
Post Reply