Note that there are some explanatory texts on larger screens.

plurals
  1. POimage's table creates differend foreign keys
    text
    copied!<p>I have a code which inserts into more 3 tables . what happens is that if I create a new account at the same time I upload images for that account in the images table . The code does it very well but now the problem is it gives each image a unique referenced user_Id yet all images uploaded for that user should be with the same Id from the user table .I am using a relational database where by the user table has a primary key user_id which is referenced to other tables as their foreign key . bellow is my code : ant help will be appreciated .</p> <pre><code>&lt;?php #connect to the db require_once('db.inc.php'); ?&gt; &lt;?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 &lt; 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 &gt; 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 ?&gt; &lt;?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-&gt;prepare($query); $insert-&gt;execute(array( ':username'=&gt;$username, ':Password'=&gt;$Password, ':Previllage'=&gt;$Previllage, ':date_created'=&gt;$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-&gt;prepare($id_last); $result-&gt;execute(); $last_id = $result-&gt;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-&gt;prepare($clientinfor); $clientinfor_insert-&gt;execute(array( ':title'=&gt;$title, ':firstname'=&gt;$firstname, ':lastname'=&gt;$lastname)); #end of clien infor ################################################ $security="INSERT INTO tish_security(ip_address,user_id) VALUES(:ip_address,$last_id)"; $security_insert = $con-&gt;prepare($security); $security_insert-&gt;execute(array( ':ip_address'=&gt;$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-&gt;prepare($images); $images_insert-&gt;execute(array( ':image_name'=&gt;$rename, ':date_registered'=&gt;$date_created)); ############# property table########################################################## /*$property ="INSERT INTO tish_propertyinfo(user_id,date_registered) VALUES($last_id,:date_registered)"; $property_insert = $con-&gt;prepare($images); $property_insert-&gt;execute(array(':date_registered'=&gt;$date_created)); */}catch(PDOException $e){ echo $e-&gt;getMessage(); } } } ?&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload