0

我有一个插入更多 3 个表的代码。发生的情况是,如果我同时创建一个新帐户,我会在图像表中为该帐户上传图像。该代码做得很好,但现在的问题是它为每个图像提供了一个唯一的引用 user_Id,但为该用户上传的所有图像都应该具有来自用户表的相同 ID。我正在使用一个关系数据库,其中用户表有一个主键 user_id 作为外键引用其他表。下面是我的代码: 蚂蚁帮助将不胜感激。

<?php
#connect to the db
require_once('db.inc.php');
?>
<?php
#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 
?>
<?php 
$date_created= date('y-m-d h:i:s a');
$username=(isset($_POST['username']))? trim($_POST['username']): '';
$Previllage =(isset($_POST['Previllage']))? trim($_POST['Previllage']): '';
#second tanble values 
$title=(isset($_POST['title']))? trim($_POST['title']): '';
$firstname=(isset($_POST['firstname']))? trim($_POST['firstname']): '';
$lastname=(isset($_POST['lastname']))? trim($_POST['lastname']): '';
$client_code=(isset($_POST['client_code']))? trim($_POST['client_code']): '';
$job_approval=(isset($_POST['job_approval']))? trim($_POST['job_approval']): '';
$address=(isset($_POST['address']))? trim($_POST['address']): '';
$cell=(isset($_POST['cell']))? trim($_POST['cell']): '';
$tel=(isset($_POST['tel']))? trim($_POST['tel']): '';
$email=(isset($_POST['email']))? trim($_POST['email']): '';
$company=(isset($_POST['company']))? trim($_POST['company']): '';
$province=(isset($_POST['province']))? trim($_POST['province']): '';
$username= substr(md5(rand()),0,7);
$Pas=substr(md5(rand()),0,4);
$Password =(md5($Pas));
$user =(isset($_POST['$user']))? trim($_POST['$user']): '';
$ip_address = $_SERVER['REMOTE_ADDR'];
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'=>$username,
':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,user_id)
VALUES(:title,:firstname,:lastname,$last_id)";
$clientinfor_insert = $con->prepare($clientinfor);
$clientinfor_insert->execute(array(
':title'=>$title,
':firstname'=>$firstname,
':lastname'=>$lastname));
#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));
############# 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();
}

  } 
}
?>
4

1 回答 1

0

图像表中的 user_id 是否有自动增量?并且 checknwether $last_id 包含任何内容,我认为ni 是 NULL。

于 2013-02-16T09:05:27.463 回答