Php 图像';s表创建不同的外键

Php 图像';s表创建不同的外键,php,mysql,Php,Mysql,我有一个代码,可以插入到3个以上的表中。如果我同时创建了一个新帐户,就会在images表中上载该帐户的图像。代码做得很好,但现在的问题是它为每个图像提供了一个唯一的引用用户Id,但是为该用户上载的所有图像都应该具有来自用户表的相同Id。我使用的是一个关系数据库,其中用户表有一个主键用户Id,该Id被其他表引用为其外键。下面是我的代码:感谢蚂蚁的帮助 <?php #connect to the db require_once('db.inc.php'); ?> <?php #co

我有一个代码,可以插入到3个以上的表中。如果我同时创建了一个新帐户,就会在images表中上载该帐户的图像。代码做得很好,但现在的问题是它为每个图像提供了一个唯一的引用用户Id,但是为该用户上载的所有图像都应该具有来自用户表的相同Id。我使用的是一个关系数据库,其中用户表有一个主键用户Id,该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();
}

  } 
}
?>


图像表中的用户id是否自动递增?如果checknwether$last\u id包含任何内容,我认为ni为NULL。

图像表中的用户id是唯一键吗?您是否将此代码用于特定用户上传的每个图像?用于数据库检查和其他问题?好问题:为每个图像上传新用户创建代码?正如@user2008945所问:)好的,你有一个带有用户名和图像上传字段的表单(简化)。您想要的是加载一个图像,然后再次执行同样的操作,并且不创建新用户?然后你需要把这两件事分开。将用户id放在隐藏字段中,或者如果会话中有该id,则使用该id。只要您在上传图像时创建一个新用户,您就会遇到这个问题。在图像表中没有自动增量。对于最后一个Id,我如何检查其是否为空?请参阅我得到的字符串(2)“11”字符串(2)“12”字符串(2)“13”字符串(2)“14”