2013-02-19 2 views
-2

Я пытаюсь вставить несколько изображений с тем же last_id Но у меня проблема с кодом, который не выполняет именно то, что я ищу У меня две таблицы: это таблица пользователя и таблица изображения, поэтому я хочу, чтобы каждый пользователь, когда он или она загружает изображения, они должны захватывать последние идентификаторы и использовать их в таблице изображения, но также каждое изображение будет иметь уникальный идентификатор изображения. в пользовательской таблице там user_id как первичный ключ, а в таблице изображений есть user_id как fk и image_d pk. Ниже приведен мой код.Как загрузить несколько изображений, но с тем же last_id?

<?php 
$target = 'image_uploads/'; 
    if(isset($_FILES['image_name'])===true){ 
    $files = $_FILES['image_name']; 
    for($x = 0 ; $x < count($files['name']); $x++){ 
    $name = $files['name'][$x] ; 
    $temp_name = $files['tmp_name'][$x]; 
    #extention filter it takes only the extension want 
$allowed ='gif,png,jpg,pdf'; 
$extension_allowed= explode(',',$allowed); 
$file_extention = pathinfo($name, PATHINFO_EXTENSION); 
if(array_search($file_extention,$extension_allowed)){ 
}else { 
echo 'We only allow gif, png ,jpg'; 
    exit(); 
} #extention filter ends here 
    #check the size of the image 
    $file_size = $files['size'][$x]; 
    if($file_size > 2097152){ 
    echo 'The file should be lesS than 2MB'; 
    exit(); 
    } 
    #check the size of the image ends here 
    #Rename images 
    $sub = substr(md5(rand()),0,7); 
    #the above generates char and numbesr 
    $rand = rand(0,100000); 
    $rename = $rand.$sub.$name; 
    #Rename images ends here 
    $move = move_uploaded_file($temp_name,$target.$rename); 
#code to deal with the picture uploads ends here 
}} 
?> 
<?php 
try{ 
$query="INSERT INTO tish_user(username,Password,Previllage,date_created) 
VALUES(:username,:Password,:Previllage,:date_created)"; 
$insert = $con->prepare($query); 
$insert->execute(array(
':username'=>$user, 
':Password'=>$Password, 
':Previllage'=>$Previllage, 
':date_created'=>$date_created)); 
#end of first table 
################################################ 
#You select the first Id and put it in a variable then 
$id_last = ("SELECT LAST_INSERT_ID()"); 
$result =$con->prepare($id_last); 
$result->execute(); 
$last_id = $result->fetchColumn(); 
############################## Last Id query Ends here 
#insert into clientinfo table 
$clientinfor="INSERT INTO tish_clientinfo 
(title, firstname, lastname, nickname, idnumber, client_code, 
company, country, city, province, address, cell, 
tel, webaddress, satifiedstatus, email, job_approval, cash_with_vat, 
cash_paid, date_registered,user_id) 
VALUES(:title,:firstname,:lastname,:nickname,:idnumber,:client_code, 
:company,:country,:city,:province,:address, 
:cell,:tel,:webaddress,:satifiedstatus, :email, :job_approval, 
:cash_with_vat,:cash_paid, :date_registered,$last_id)"; 
$clientinfor_insert = $con->prepare($clientinfor); 
$clientinfor_insert->execute(array(
':title'=>$title, 
':firstname'=>$firstname, 
':lastname'=>$lastname, 
':nickname'=>$nickname, 
':idnumber'=>$idnumber, 
':client_code'=>$client_code, 
':company'=>$company, 
':country'=>$country, 
':city'=>$city, 
':province'=>$province, 
':address'=>$address, 
':cell'=>$cell, 
':tel'=>$tel, 
':webaddress'=>$webaddress, 
':satifiedstatus'=>$satifiedstatus, 
':email'=>$email, 
':job_approval'=>$job_approval, 
':cash_with_vat'=>$cash_with_vat, 
':cash_paid'=>$cash_paid, 
':date_registered'=>$date_registered 
)); 
#end of clien infor 
################################################ 
$security="INSERT INTO tish_security(ip_address,user_id) 
VALUES(:ip_address,$last_id)"; 
$security_insert = $con->prepare($security); 
$security_insert->execute(array(
':ip_address'=>$ip_address)); 
##########################end of security 
############ images 
$images ="INSERT INTO tish_images(user_id,image_name,date_registered) 
VALUES($last_id,:image_name,:date_registered)"; 
$images_insert = $con->prepare($images); 
$images_insert->execute(array(
':image_name'=>$rename, 
':date_registered'=>$date_created)); 

##############################category 
$cats = $vals = array(); 
foreach ((array) $_POST['category_name'] as $cat) { 
    if ('' !== ($cat = trim($cat))) { 
     $cats[] = $cat; 
     $vals[] = "({$last_id}, ?)"; 
    } 
} 
################################################ 
$sql = 'INSERT INTO tish_catigory (user_id, category_name) VALUES'. join(',', $vals); 
$sth = $con->prepare($sql); 
foreach ($cats as $i => $cat) { 
    $sth->bindValue($i+1, $cat, PDO::PARAM_STR); 
} 
$sth->execute(); 

}catch(PDOException $e){ 
echo $e->getMessage(); 
} 


var_dump($last_id); 


?> 
+1

Так что не работает, какую ошибку вы получаете? –

+1

Спасибо Java West west ответил на это и его работу – humphrey

ответ

1

Это должно работать, я проверил его и он работал

<?php 
try{ 
$query="INSERT INTO tish_user(username,Password,Previllage,date_created) 
VALUES(:username,:Password,:Previllage,:date_created)"; 
$insert = $con->prepare($query); 
$insert->execute(array(
':username'=>$user, 
':Password'=>$Password, 
':Previllage'=>$Previllage, 
':date_created'=>$date_created)); 
#end of first table 
################################################ 
#You select the first Id and put it in a variable then 
$id_last = ("SELECT LAST_INSERT_ID()"); 
$result =$con->prepare($id_last); 
$result->execute(); 
$last_id = $result->fetchColumn(); 
############################## Last Id query Ends here 
#insert into clientinfo table 
$clientinfor="INSERT INTO tish_clientinfo 
(title, firstname, lastname, nickname, idnumber, client_code, 
company, country, city, province, address, cell, 
tel, webaddress, satifiedstatus, email, job_approval, cash_with_vat, 
cash_paid, date_registered,user_id) 
VALUES(:title,:firstname,:lastname,:nickname,:idnumber,:client_code, 
:company,:country,:city,:province,:address, 
:cell,:tel,:webaddress,:satifiedstatus, :email, :job_approval, 
:cash_with_vat,:cash_paid, :date_registered,$last_id)"; 
$clientinfor_insert = $con->prepare($clientinfor); 
$clientinfor_insert->execute(array(
':title'=>$title, 
':firstname'=>$firstname, 
':lastname'=>$lastname, 
':nickname'=>$nickname, 
':idnumber'=>$idnumber, 
':client_code'=>$client_code, 
':company'=>$company, 
':country'=>$country, 
':city'=>$city, 
':province'=>$province, 
':address'=>$address, 
':cell'=>$cell, 
':tel'=>$tel, 
':webaddress'=>$webaddress, 
':satifiedstatus'=>$satifiedstatus, 
':email'=>$email, 
':job_approval'=>$job_approval, 
':cash_with_vat'=>$cash_with_vat, 
':cash_paid'=>$cash_paid, 
':date_registered'=>$date_registered 
)); 
#end of clien infor 
################################################ 
$security="INSERT INTO tish_security(ip_address,user_id) 
VALUES(:ip_address,$last_id)"; 
$security_insert = $con->prepare($security); 
$security_insert->execute(array(
':ip_address'=>$ip_address)); 
##########################end of security 
############ images 
#code to deal with the picture uploads 
#target folder 
$target = 'image_uploads/'; 
    if(isset($_FILES['image_name'])===true){ 
    $files = $_FILES['image_name']; 
    for($x = 0 ; $x < count($files['name']); $x++){ 
    $name = $files['name'][$x] ; 
    $temp_name = $files['tmp_name'][$x]; 
    #extention filter it takes only the extension want 
$allowed ='gif,png,jpg,pdf'; 
$extension_allowed= explode(',',$allowed); 
$file_extention = pathinfo($name, PATHINFO_EXTENSION); 
if(array_search($file_extention,$extension_allowed)){ 
}else { 
echo 'We only allow gif, png ,jpg'; 
    exit(); 
} #extention filter ends here 
    #check the size of the image 
    $file_size = $files['size'][$x]; 
    if($file_size > 2097152){ 
    echo 'The file should be lesS than 2MB'; 
    exit(); 
    } 
    #check the size of the image ends here 
    #Rename images 
    $sub = substr(md5(rand()),0,7); 
    #the above generates char and numbesr 
    $rand = rand(0,100000); 
    $rename = $rand.$sub.$name; 
    #Rename images ends here 
    $move = move_uploaded_file($temp_name,$target.$rename); 
#code to deal with the picture uploads ends here 


$images ="INSERT INTO tish_images(user_id,image_name,date_registered) 
VALUES($last_id,:image_name,:date_registered)"; 
$images_insert = $con->prepare($images); 
$images_insert->execute(array(
':image_name'=>$rename, 
':date_registered'=>$date_created)); 
}} 
##############################category 
$cats = $vals = array(); 
foreach ((array) $_POST['category_name'] as $cat) { 
    if ('' !== ($cat = trim($cat))) { 
     $cats[] = $cat; 
     $vals[] = "({$last_id}, ?)"; 
    } 
} 
################################################ 
$sql = 'INSERT INTO tish_catigory (user_id, category_name) VALUES'. join(',', $vals); 
$sth = $con->prepare($sql); 
foreach ($cats as $i => $cat) { 
    $sth->bindValue($i+1, $cat, PDO::PARAM_STR); 
} 
$sth->execute(); 


############# property table########################################################## 
/*$property ="INSERT INTO tish_propertyinfo(user_id,date_registered) 
VALUES($last_id,:date_registered)"; 
$property_insert = $con->prepare($images); 
$property_insert->execute(array(':date_registered'=>$date_created)); 
*/}catch(PDOException $e){ 
echo $e->getMessage(); 
} 
#for the insert check boxes 

var_dump($last_id); 
#images 

?> 
Смежные вопросы