简体   繁体   中英

PHP mySQL, inserting content into two tables

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!

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;

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM