-2

同じlast_idで複数の画像を挿入しようとしていますが、コードが探しているものを正確に実行しないという問題があります。2つのテーブルがあります。1つはユーザーテーブルと画像のテーブルなので、各ユーザーが画像をアップロードするときに必要です。最後のIDを取得して画像のテーブルで使用する必要がありますが、各画像には一意の画像IDが割り当てられます。ユーザーテーブルには主キーとしてuser_idがあり、イメージテーブルにはfkおよびimage_dpkとしてuser_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);


?>
4

1 に答える 1

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 

?>
于 2013-02-19T13:25:24.753 に答える