0

Trying to insert into two database tables with one try!

$query = "INSERT INTO category 
        (base_img) VALUES ('$imgsrc')";

$insert_row = $db->insert($query);

//Getting last ID
$catID = mysql_insert_id();

$query = "INSERT INTO cat_lng
        (locate, foreign_id, name, body) VALUES
        ('lv', '$catID', '$name_lv', '$text_lv'),
        ('ru', '$catID', '$name_ru', '$text_ru'),
        ('en', '$catID', '$name_en', '$text_en')";

$insert_row = $db->insert($query);  

My insert function:

public function insert($query){
        $insert_row = $this->link->query($query) or die($this->link->error.__LINE__);

        //Validate Insert
        if($insert_row){
            header("Location: index.php?msg=".urlencode('Record Added'));
            exit();
        } else {
            die('Error : ('. $this->link->errno .') '. $this->link->error);
        }
   }

Code inserts into category, but stops after that! Cant figure out why..? They work seperatly but cant get them working together!

3
  • Does it throw an error? Commented Nov 28, 2014 at 14:14
  • No. It jumps to the index page as everything would be fine Commented Nov 28, 2014 at 14:15
  • Ok! I found an error. "Validate insert" takes me to the index.php after first instert. deleted that and now it works. But mysql_insert_id(); is not working? Commented Nov 28, 2014 at 14:20

2 Answers 2

2

Hello: The exit() functions terminates the execution of the script.

http://php.net/manual/en/function.exit.php

You might change:

exit()

to

return true;
Sign up to request clarification or add additional context in comments.

Comments

0

Change the name of $query as $query1 and $insert_row as $insert_row1 which will insert $query1 or any other name you like to use than use the if as follow:

if($insertrow && $insertrow1)
 {
        //bunch of code here
 }

And also you can use mysqli_muti_query function for one name.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.