简体   繁体   中英

inserting multiple photos into mysql database

Here is my code, it has connection with database included:

session_start();

require 'config2.php';
require_once 'user.class.php';

$target = "uploads/"; 
$target1 = "uploads/"; 
$target = $target . basename( $_FILES['photo']['tmp_name']); 
$target1 = $target1 . basename( $_FILES['photo1']['tmp_name']); 

//This gets all the other information from the form 
$login = $_SESSION['login'];    
$name=$_POST['name']; 
$name1 =$_POST['name1'];
$pic=($_FILES['photo']['name']); 
$pic1=($_FILES['photo1']['name1']); 

$id=$_SESSION['id'];

// Connects to your Database 
print_r($_POST);
print_r($_FILES);
print_r($_SESSION);
print_r($_GET);

//$op = mysql_query("select id from users where id = '$id' ");
//Writes the information to the database 
mysql_query("INSERT INTO users (name, photo, name1, photo1) VALUES ('$name', '$photo', '$name1', '$photo1') where login = '$login'") ; 

// mysql_query("UPDATE users SET name='$name', photo = '$pic', name1 = '$name1', photo1 = '$pic1' WHERE  login = '$login' ");
//Writes the photo to the server 
if(move_uploaded_file($_FILES['photo']['tmp_name'] ,$target) )
{ 
  //Tells you if its all ok 
  echo "The file ". basename( $_FILES['uploadedfile']['name'] ). " has been uploaded,        and your information has been added to the directory"; 
  echo "<a href = 'profile.php?id=$id'>back</a>"; 
} 

if(move_uploaded_file($_FILES['photo1']['tmp_name'] ,$target1) )
{ 
  //Tells you if its all ok 
  echo "The file ". basename( $_FILES['uploadedfile']['name1'] ). " has been uploaded, and your information has been added to the directory"; 
  echo "<a href = 'profile.php?id=$id'>back</a>"; 
} 
else { 
  //Gives and error if its not 
  echo "Sorry, there was a problem uploading your file."; 
} 

This code works only for one image (uploads one image name into database field -photo), and only if I change insert to update. How to make it work with multiple images and with insert command ?

Enter a comma-separated list of rows.

INSERT INTO users (name, photo, name1, photo1) 
VALUES 
('$name', '$photo', '$name1', '$photo1'),
('$name', '$photo3', '$name2', '$photo4'),
('$name', '$photo5', '$name3', '$photo6');

Look at your indices and names of variables. You use $_FILES['uploadedfile']['name1'] and $_FILES['photo1']['tmp_name'] . Are you sure that this is correct? POST data is in $_POST array, files information is in $_FILES . Names of uploaded files is in $_FILES['photo1']['name'] . You will never have $_FILES['uploadedfile']['name1'] set.

Also, what table structure do you have? Have you two name fields and two filepath fields?

Your query is incorrect. If you want to update table row, use UPDATE ... WHERE ... query. If you want to add new one, use INSERT without WHERE . You have commented correct one.

Also, use PDO and prepared statements, really. Your code allows SQL injections.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM