我正在尝试将一个查询插入2个表中,但是不起作用。
我想将所有_POST数据保存到“ tours”表和图片库简介“ poze_articol”表中,其中来自poze_articol
的“ pid”是tours
id的ID,感谢您的帮助,因为我是php新手,谢谢!
邮递区号:
<?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>';
}
}
}
}
}
}
}
}
?>
最佳答案
您无法像使用$sqlz
变量那样串联SQL指令。
您必须一次执行一次SQL插入查询。
因此,在第一个INSERT into tours
之后,您需要执行mysql_query( $sqlz, $conn )
。之后,您需要再次使用INSERT INTO poze_articol
重用$ sqlz,然后再执行一次mysql_query( $sqlz, $conn )
。请尝试以下操作:
$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 );