Jump to content

last field overwriting when uploading multiple images into mysqli d.b.


erok

Recommended Posts

with these codes second picture goes twice to database. I am unable to upload first picture. What am i doing wrong?
Any help appreciated
$pid = mysqli_insert_id($con) ;
if (!empty($_FILES['image']) || !empty($_POST['caption']))
{ //1
$formOk = true;
//Assign Variables
$path = $_FILES['image']['tmp_name'] ;
$name = $_FILES['image']['name'] ;
$type = $_FILES['image']['type'] ;
$caption = $_POST['caption'] ;
// check for file size.
if (filesize($path) > 2000000)
{ //4
$formOk = false;
echo "Error: File size must be less than 2000 KB.";
} //4+
if ($formOk)
{
// read file contents
$content2 = file_get_contents($path);
$content = mysqli_real_escape_string($con, $content2);
$caption = mysqli_real_escape_string($con, $caption) ;
}
}
if (!empty($_FILES['image']) || !empty($_POST['caption']))
{ //1
$formOk = true;
//Assign Variables
$path1 = $_FILES['image']['tmp_name'] ;
$name1 = $_FILES['image']['name'] ;
$type1 = $_FILES['image']['type'] ;
$caption1 = $_POST['caption'] ;
// check for file size.
if (filesize($path1) > 2000000)
{ //4
$formOk = false; //// something wrong here
echo "Error: File size must be less than 2000 KB.";
} //4+
if ($formOk)
{
// read file contents
$content2 = file_get_contents($path);
$content1 = mysqli_real_escape_string($con, $content2);
$caption1 = mysqli_real_escape_string($con, $caption) ;
$sql = "insert into images (pid, name, type, content, caption) values ('{$pid}', '{$name}', '{$type}', '{$content}', '{$caption}'), ('{$pid}', '{$name1}', '{$type1}', '{$content1}', '{$caption1}')";
$sql= mysqli_query($con, $sql) ;
Link to comment
Share on other sites

for the first image; I used $path, $name, $type, $content, $caption variables and

for the second image; $path1, $name1, $type1, $content1, $caption1 variables. At least I thought that way.

I am trying to upload two pictures at separate rows, not two images in the same row.

Link to comment
Share on other sites

I am trying to upload to two separate rows

 

$path =$_FILES['image']['tmp_name'];

$path1=$_FILES['image1']['tmp_name]; This way they all need to go to same row.

 

How can upload two separate pictures to two separate rows?

Link to comment
Share on other sites

Then you're obviously using the same data for both insert queries. Print out both SQL insert queries if you don't believe me, they will be the same. You're using the same data for both queries. Look:

$name = $_FILES['image']['name'] ;$name1 = $_FILES['image']['name'] ;
Both variables are set to $_FILES['image']['name']. They have the same value. You are using the same data for both sets of variables:
$path = $_FILES['image']['tmp_name'] ;$name = $_FILES['image']['name'] ;$type = $_FILES['image']['type'] ;$caption = $_POST['caption'] ;
$path1 = $_FILES['image']['tmp_name'] ;$name1 = $_FILES['image']['name'] ;$type1 = $_FILES['image']['type'] ;$caption1 = $_POST['caption'] ;
You're using 2 different sets of variable names to hold the same data coming from $_FILES and $_POST.
Link to comment
Share on other sites

I am NOT saying i am doing the right thing. I have a problem and i am looking to solution.

" if you have 2 SQL insert queries then it will insert 2 rows, with whatever data you have in each row. " i tried this and that way inserting the second picture twice.

I also tried using array, giving error message. Do not insert any row at all.

I tried PREPARE. it insert the row but give blob 0. Means no image inserted. Somehow prepare do not insert image. Maybe it is only for the string data.

I need to insert each picture to separate rows.

Any help appreciated

Link to comment
Share on other sites

I know what you're trying to do, I don't think you understand what the problem is though. The problem has nothing to do with how you're using the database. You SHOULD be using prepared statements, always, but that's not the problem. The problem is that the data you are getting from the form is the same for both images. You are not getting different data from $_FILES and $_POST for the second image, you are using the exact same data that you used for the first image. I don't know what your form looks like, so I can't guess what data you should be using or even if you have your form set up correctly, but the problem is that your PHP script is only getting data from the form for a single image. What you're doing with the database doesn't matter, it's the form data.

Link to comment
Share on other sites

Why didn't you just change these lines like was suggested (to point to something other than the same thing as for $name, $path, etc), to be whatever the second image in your form was? That's exactly what JSG said

 

$name1 = $_FILES['image']['name'] ;$path1 = $_FILES['image']['tmp_name'] ;$name1 = $_FILES['image']['name'] ;$type1 = $_FILES['image']['type'] ;$caption1 = $_POST['caption'] ;

 

Doesn't sound like the solution was better then the original problem.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...