2013-02-16 48 views
0

我有一個代碼插入到更多的3個表。會發生什麼情況是,如果我同時創建一個新帳戶,我會在圖像表中爲該帳戶上傳圖像。代碼很好,但現在的問題是它爲每個圖像提供一個唯一引用的user_Id,但爲該用戶上傳的所有圖像都應該使用來自用戶表的相同ID。我正在使用關係數據庫,其中由用戶表具有主鍵user_id被引用到其他表作爲其外鍵。波紋管是我的代碼:螞蟻的幫助將不勝感激。圖像的表創建differend外鍵

<?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(); 
} 

    } 
} 
?> 
+0

是圖像表唯一鍵中的user_id?您是否使用此代碼來處理由特定用戶上傳的所有圖片? – luckystars 2013-02-16 09:07:43

+0

沒有它的不唯一 – humphrey 2013-02-16 09:11:26

+0

進行數據庫檢查http://pastebin.com/7ZJhxzyi – humphrey 2013-02-16 09:14:06

回答

0

圖像表中的user_id是否存在自動增量?而checknwether $ last_id包含任何東西,我會認爲是NULL。

+0

的外鍵在圖像表中沒有auto_increment。爲最後一個ID如何檢查是否其空或不plse幫助 – humphrey 2013-02-16 09:10:09

+2

數據庫檢查plse http://pastebin.com/7ZJhxzyi – humphrey 2013-02-16 09:14:23

+1

var_dump($ last_id) – 2013-02-16 09:41:24