Home INSERT INTO 2 tables query
Reply: 1

INSERT INTO 2 tables query

Alex
1#
Alex Published in 2017-12-07 22:24:53Z

I'm trying to insert with one query into 2 tables, but is not working. I want to save all the _POST data into "tours" table and image gallery intro "poze_articol" table where "pid" from poze_articol is the id of tours id, some help is appreciated because i'm new with php, thank you!

Php code:

<?php
$dbhost = '';
$dbuser = '';
$dbpass = '';
$myDBname = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass, $myDBname);
$db_selected = mysql_select_db('antonytravel', $conn);
if (!$db_selected) {
    die ('Can\'t use antonytravel : ' . mysql_error());
}
if(isset($_POST['addtour']))
{
if (!isset($_FILES['poza_caseta']['tmp_name'])) {
    echo "";
    }else{
    $file=$_FILES['poza_caseta']['tmp_name'];
    $image= addslashes(file_get_contents($_FILES['poza_caseta']['tmp_name']));
    $image_name= addslashes($_FILES['poza_caseta']['name']);

            move_uploaded_file($_FILES["poza_caseta"]["tmp_name"],"../../../../img/traseu_thmb/" . $_FILES["poza_caseta"]["name"]);

            $poza_caseta="img/traseu_thmb/" . $_FILES["poza_caseta"]["name"];

    if (!isset($_FILES['poza_header']['tmp_name'])) {
    echo "";
    }else{
    $files=$_FILES['poza_header']['tmp_name'];
    $images= addslashes(file_get_contents($_FILES['poza_header']['tmp_name']));
    $images_name= addslashes($_FILES['poza_header']['name']);

            move_uploaded_file($_FILES["poza_header"]["tmp_name"],"../../../../img/poze_header/" . $_FILES["poza_header"]["name"]);

            $poza_header="img/poze_header/" . $_FILES["poza_header"]["name"];

            ########### Poze Upload #########
error_reporting(0);
define ("MAX_SIZE","9000"); 
function getExtension($str)
{
         $i = strrpos($str,".");
         if (!$i) { return ""; }
         $l = strlen($str) - $i;
         $ext = substr($str,$i+1,$l);
         return $ext;
}
$valid_formats = array("jpg", "png", "bmp","jpeg");
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST") 
{

    $uploaddir = "../../../../img/gallery/"; //image upload directory
    foreach ($_FILES['photos']['name'] as $name => $value)
    {

        $filename = stripslashes($_FILES['photos']['name'][$name]);
        $size=filesize($_FILES['photos']['tmp_name'][$name]);
        //get the extension of the file in a lower case format
          $ext = getExtension($filename);
          $ext = strtolower($ext);

         if(in_array($ext,$valid_formats))
         {
           if ($size < (MAX_SIZE*1024))
           {
           $image_name=time().$filename;

           $newname=$uploaddir.$image_name;

           if (move_uploaded_file($_FILES['photos']['tmp_name'][$name], $newname)) 
           {

$sqlz =  "INSERT INTO tours (titlu,durata,descriere,pret,ribbon,poza,titlu_slider,desc_slider,poza_slider,descriere_box,titlu_box1,desc_box1,titlu_box2,desc_box2,titlu_box3,desc_box3,titlu_box4,desc_box4,poze_articol) VALUES ('".$_POST["oras_caseta"]."','".$_POST["durata_caseta"]."','".$_POST["descriere_caseta"]."','".$_POST["pret_caseta"]."','".$_POST["icon_caseta"]."','$poza_caseta','".$_POST["titlu_slider"]."','".$_POST["descriere_slider"]."','$poza_header','".$_POST["descriere_overview"]."','".$_POST["titlu_box1"]."','".$_POST["desc_box1"]."','".$_POST["titlu_box2"]."','".$_POST["desc_box2"]."','".$_POST["titlu_box3"]."','".$_POST["desc_box3"]."','".$_POST["titlu_box4"]."','".$_POST["desc_box4"]."')";

$sqlz .=  "INSERT INTO poze_articol (poza_slider,pid) VALUES ('img/gallery/$image_name','1')";
$retval = mysql_query( $sqlz, $conn );

echo '<div class="alert alert-success" role="alert">
  <strong>Bravo!</strong> Ai adaugat cu succes un traseu pe site. 
</div>';
}
 }
  }
   }
    }
     }
      }
}
?>
Rod Elias
2#
Rod Elias Reply to 2017-12-07 22:41:43Z

You can't concatenate your SQL instructions like you did with your $sqlz variable.

You have to execute your SQL insert queries one at a time.

So, right after your first INSERT into tours you need to execute mysql_query( $sqlz, $conn ). After doing that, you need to reuse your $sqlz again with your INSERT INTO poze_articol and then execute your mysql_query( $sqlz, $conn ) one more time. Please try something like the following:

$sqlz =  "INSERT INTO tours (titlu,durata,descriere,pret,ribbon,poza,titlu_slider,desc_slider,poza_slider,descriere_box,titlu_box1,desc_box1,titlu_box2,desc_box2,titlu_box3,desc_box3,titlu_box4,desc_box4,poze_articol) VALUES ('".$_POST["oras_caseta"]."','".$_POST["durata_caseta"]."','".$_POST["descriere_caseta"]."','".$_POST["pret_caseta"]."','".$_POST["icon_caseta"]."','$poza_caseta','".$_POST["titlu_slider"]."','".$_POST["descriere_slider"]."','$poza_header','".$_POST["descriere_overview"]."','".$_POST["titlu_box1"]."','".$_POST["desc_box1"]."','".$_POST["titlu_box2"]."','".$_POST["desc_box2"]."','".$_POST["titlu_box3"]."','".$_POST["desc_box3"]."','".$_POST["titlu_box4"]."','".$_POST["desc_box4"]."')";

mysql_query( $sqlz, $conn );

$sqlz =  "INSERT INTO poze_articol (poza_slider,pid) VALUES ('img/gallery/$image_name','1')";

mysql_query( $sqlz, $conn );
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.322904 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO