How to upload multiple image in one record database SQL?

Hii,

I have managed to get some code working using the PHP Manual. It uploads multiple images to my server, however i am having problems inserting each file path to my database…?

This is my FORM code with 4 images input :

<form method="POST" action='' enctype='multipart/form-data'>
	<table>
					<tr>
			<td width='200'>NSS </td><td> <input type='text' style='padding:5px;width:250px;' name="nss" placeholder='nomor statistik sekolah' required='required' /></td></tr>
		<tr>
			<td>NPSN </td><td> <input type='text' style='padding:5px;width:250px;' name="npsn" placeholder='npsn'  /></td></tr>
		<tr>
			<td>Nama Sekolah </td><td> <input type='text' style='padding:5px;width:250px;' name="nama_sekolah" placeholder='Nama Sekolah'  required='required' /></td></tr>
		<tr>
			<td>Jenis Sekolah </td><td><select name="jenis_sekolah" required='required'>
				<option value=''>--Negeri/Swasta--</option>
				<option value='Negeri'>Negeri</option>
				<option value='Swasta'>Swasta</option>
			</select></td></tr>
		<tr>
			<td>Akreditasi</td><td><select name="akreditasi" required='required'>
				<option value=''>--PILIH AKREDITASI--</option>
				<option value='A'>A</option>
				<option value='B'>B</option>
				<option value='C'>C</option>
				<option value='D'>D</option>
				<option value='E'>E</option>
				<option value='TT'>Belum Terakreditasi</option>
			</select></td></tr>
		<tr>
			<td>Alamat </td><td> <input type='text' style='padding:5px;width:250px;' name="alamat" placeholder='Alamat Lengkap'  required='required' /></td></tr>
		<tr>
			<td>Telepon </td><td> <input type='text' style='padding:5px;width:250px;' name="tlp" placeholder='(0291)-xxx/085xxx'  /></td></tr>	
		<tr>
			<td>Websites </td><td> <input type='text' style='padding:5px;width:250px;' name="web" placeholder='Alamat website / blog'  /></td></tr>
		<tr>
			<td>Jumlah Guru </td><td> <input type='text' style='padding:5px;width:250px;' name="jmlguru" placeholder='jumlah guru'  required='required' /></td></tr>
		<tr>
			<td>Jumlah Siswa </td><td> <input type='text' style='padding:5px;width:250px;' name="jmlsiswa" placeholder='jumlah siswa'  required='required' /></td></tr>
		<tr>
			<td>Kecamatan</td><td><select name="kecamatan" required='required'>
				<option value=''>--PILIH KECAMATAN--</option>
				<option value='BAE'>BAE</option>
				<option value='DAWE'>DAWE</option>
				<option value='GEBOG'>GEBOG</option>
				<option value='KALIWUNGU'>KALIWUNGU</option>
				<option value='KOTA'>KOTA</option>
				<option value='JATI'>JATI</option>
				<option value='JEKULO'>JEKULO</option>
				<option value='MEJOBO'>MEJOBO</option>
				<option value='UNDAAN'>UNDAAN</option>
			</select></td></tr>
		<tr>
			<td colspan='2'><center>Koordinat Geografis Sekolah</center></td></tr>
		<tr>
			<td>Lintang</td><td> <input type='text' style='padding:5px;width:250px;' name="lat" placeholder='-6.xxxxx' required='required'/></td></tr>
		<tr>
			<td>Bujur </td><td> <input type='text' style='padding:5px;width:250px;' name="lon" placeholder='110.xxxxx'  required='required' /></td></tr>
		<tr>
			<td>Images </td><td> <input type='file' style='padding:5px;width:250px;background:#f0f0f0;' name="file1" id='f1' /><br /></td></tr>
					<tr><td> <input type='file' style='padding:5px;width:250px;background:#f0f0f0;' name="file2" id='f2' /><br /></td></tr>
					<tr><td> <input type='file' style='padding:5px;width:250px;background:#f0f0f0;' name="file3" id='f3' /><br /></td></tr>
					<tr><td> <input type='file' style='padding:5px;width:250px;background:#f0f0f0;' name="file4" id='f4' /><br /></td></tr>
		<tr>
			<td>&nbsp;</td><td><br /> <input type='submit' class='submit' name='submit' value='Simpan' /> 
								<input type='reset' class='submit' value='Reset' /></td></tr>
	</table>
	 <a class="more" href="data.php">Kembali</a> 
</form>

this is my code to save image and insert data to database : (*but this code bellow is not work)

<?php
	if (isset($_POST['submit']))
	{
		$tipe_file1	= $_FILES['file1']['type'];
		$tipe_file2	= $_FILES['file2']['type'];
		$tipe_file3	= $_FILES['file3']['type'];
		$tipe_file4	= $_FILES['file4']['type'];
		if ($tipe_file1 == "image/jpeg" || $tipe_file1 == "image/jpg"){
	$aMyUploads = array();
	foreach ($_FILES as $aFile) {
		if(0 === $aFile['error']){
		$newLocation = 'foto/'.$aFile['name'];
		if(0 === $aFile['error'] && (false !== move_uploaded_file($aFile['tmp_name'], $newLocation))){
		$aMyUploads[] = $newLocation;
		} else {
		$aMyUploads[] = '';
		}
	}
}
	print_r($aMyUploads);
mysql_query("INSERT INTO smamakudus (nss, npsn, nama_sekolah, jenis_sekolah, akreditasi, alamat, tlp, web, jmlguru, jmlsiswa, kecamatan, lat, lon, image1, image2, image3, image4,) VALUES
(
'".$_POST['nss']."',
'".$_POST['npsn']."',
'".$_POST['nama_sekolah']."',
'".$_POST['jenis_sekolah']."',
'".$_POST['akreditasi']."',
'".$_POST['alamat']."',
'".$_POST['tlp']."',
'".$_POST['web']."',
'".$_POST['jmlguru']."',
'".$_POST['jmlsiswa']."',
'".$_POST['kecamatan']."',
'".$_POST['lat']."',
'".$_POST['lon']."',
'".$_POST['alamat']."',
'".$aMyUploads[0]."',
'".$aMyUploads[1]."',
'".$aMyUploads[2]."',
'".$aMyUploads[3]."',
'".$aMyUploads[4]."',)") or die(mysql_error());;
 echo "<script>alert('Successfully added')</script>";
	}else{
		echo "<script>alert('Failed to add data! Images must be inputed at last one and filetype JPG/JPEG')</script>
		<script>setTimeout(\"self.history.back();\",0000);</script>";
		}
}
echo "</div>
	</div>";

?>

and my problem is the images all upload to the server fine, but insert data that does not work…
Are something error in my $_POST code…?

Can you elaborate on how it doesn’t work? Do you get a MySQL Error?

i don’t get any error, but all data that i’m input all not send in my database…

try putting:

$aMyUploads[] = 'test';

If you get a load of ‘test’ inputs in your table, then you know that the issue is to do with the ‘if’ logic.

The query doesn’t look correct to me. Leaving aside using the deprecated mysql() calls rather than mysqli or PDO, you list a set of column names that end

lat, lon, image1, image2, image3, image4,)

but you supply the following data:

$_POST['lat']
$_POST['lon']
$_POST['alamat']
$aMyUploads[0]
$aMyUploads[1]
$aMyUploads[2]
$aMyUploads[3]
$aMyUploads[4]

so you have an extra entry ‘alamat’, and an extra image as you supply potentially five, you specify 17 columns but provide 19 values. I’m also not sure what that hanging comma at the end of the column names in the query would do.

ok, i’ll try it.

This does look odd, i’m surprised you don’t get a MySQL error for it.

<?php
	if (isset($_POST['submit']))
	{ //Open 1
		$tipe_file1	= $_FILES['file1']['type']; // Why are we doing this?
		$tipe_file2	= $_FILES['file2']['type']; // Also, if the files are meant to be doing the same thing
		$tipe_file3	= $_FILES['file3']['type']; // Probably should have just called the field 'image[]' or something
		$tipe_file4	= $_FILES['file4']['type']; // And used an array. But we'll roll with it.
		if ($tipe_file1 == "image/jpeg" || $tipe_file1 == "image/jpg"){ //$_FILES['file1']['type'] works here instead. But instead... [Open2]
	$aMyUploads = array();
	foreach ($_FILES as $aFile) { //Open3
             //Better yet, lets put the type check if HERE, and check $aFile['type'] instead.
		if(0 === $aFile['error']){ //Open4
		$newLocation = 'foto/'.$aFile['name'];
		if(0 === $aFile['error'] && (false !== move_uploaded_file($aFile['tmp_name'], $newLocation))){ //We did already check $aFile['error']. [Open5]
		$aMyUploads[] = $newLocation;
		} else {
		$aMyUploads[] = '';
		} //[Close5]
	}//Close4
}//Close3

    	print_r($aMyUploads); //I assume this looks 'right' to you at this point.
    mysql_query("INSERT INTO smamakudus (nss, npsn, nama_sekolah, jenis_sekolah, akreditasi, alamat, tlp, web, jmlguru, jmlsiswa, kecamatan, lat, lon, image1, image2, image3, image4,) VALUES
    (
    '".$_POST['nss']."',
    '".$_POST['npsn']."',
    '".$_POST['nama_sekolah']."',
    '".$_POST['jenis_sekolah']."',
    '".$_POST['akreditasi']."',
    '".$_POST['alamat']."',
    '".$_POST['tlp']."',
    '".$_POST['web']."',
    '".$_POST['jmlguru']."',
    '".$_POST['jmlsiswa']."',
    '".$_POST['kecamatan']."',
    '".$_POST['lat']."',
    '".$_POST['lon']."',
    '".$_POST['alamat']."', //This shouldnt be here.
    '".$aMyUploads[0]."',
    '".$aMyUploads[1]."',
    '".$aMyUploads[2]."',
    '".$aMyUploads[3]."',
    '".$aMyUploads[4]."',)") or die(mysql_error());; //Double Semicolon All the way cross the sky what does it mean?
//Also... you've got 5 entries for "$aMyUploads" here, but you only have 4 fields.
     echo "<script>alert('Successfully added')</script>";
    	}else{ 
            // This should be an abort sequence inside the foreach; instead of asserting that the file type is one of the two assert that if it is not-either of the two, throw a message and continue;
    		echo "<script>alert('Failed to add data! Images must be inputed at last one and filetype JPG/JPEG')</script>
    		<script>setTimeout(\"self.history.back();\",0000);</script>";
    		}//[Close2]
    }//[Close1]
    echo "</div>
    	</div>";
?>

MySQL should fail this query every time because the number of values provided (19) doesn’t match the number of field names (17).

Additional notes:

  1. Check into Normalizing your data.
  2. mysql_ is deprecated and being removed in PHP7. Consider strongly learning PDO instead, or at the very least, upgrading to mysqli_.

I fix it with specify 17 columns value, but i get this error

“Array ( [0] => foto/005.jpg [1] => foto/006.jpg ) You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘)’ at line 19”.

*I’m newbie in php n sql… Help me :frowning:

The number of columns specified and the number of column values must match. If you specify17 columns, you need 17 column values.

Also you seem to be doing this:
(value1, value2, value3,)

You shouldn’t have a comma before the closing bracket. So like this:
(value1, value2, value3)

Yeah i’m newbie in programming PHP n SQL …hihihi,
i’m using old php n my_sql cause some script that i use need run in php 5.2

i’m fix this value like this :

   mysql_query("INSERT INTO smamakudus (nss, npsn, nama_sekolah, jenis_sekolah, akreditasi, alamat, tlp, web, jmlguru, jmlsiswa, kecamatan, lat, lon, foto, foto2, foto3, foto4) VALUES
(
'".$_POST['nss']."',
'".$_POST['npsn']."',
'".$_POST['nama_sekolah']."',
'".$_POST['jenis_sekolah']."',
'".$_POST['akreditasi']."',
'".$_POST['alamat']."',
'".$_POST['tlp']."',
'".$_POST['web']."',
'".$_POST['jmlguru']."',
'".$_POST['jmlsiswa']."',
'".$_POST['kecamatan']."',
'".$_POST['lat']."',
'".$_POST['lon']."',
'".$aMyUploads[0]."',
'".$aMyUploads[1]."',
'".$aMyUploads[2]."',
'".$aMyUploads[3]."')") or die(mysql_error());;

but the values still doesn’t send to my table “smamakudus”.
parsed like this '".$_POST['name']."' or '$_POST[name]' which allowed?

What does your mysql_error say now?

OH GOD, n thanks guys… It fixed with this code above…
Not send cause the $_POST values is not match with the column in the table… HIihihiii…
Many Thanks… :smile:

@StarLion : help me, how can i describe that all 4 file images must have filetype “JPEG/JPG” ?
in this code bellow showing that only file 1 must type “JPG/JPEG”

$tipe_file1	= $_FILES['file1']['type']; // Why are we doing this?
	$tipe_file2	= $_FILES['file2']['type']; // Also, if the files are meant to be doing the same thing
	$tipe_file3	= $_FILES['file3']['type']; // Probably should have just called the field 'image[]' or something
	$tipe_file4	= $_FILES['file4']['type']; // And used an array. But we'll roll with it.
	if ($tipe_file1 == "image/jpeg" || $tipe_file1 == "image/jpg" //how should be this code? Thanks

As i mention a few lines down, what i would do is move that if down into the foreach, invert the condition, and use it as a skipper.

foreach($_FILES AS $aFile) {
  if ($aFile['type'] != "image/jpeg" && $aFile['type'] != "image/jpg") { echo "Image ".$aFile['name']." was not a JPEG. Skipping."; continue; }

Key to thinking: You want to check EACH file. So…the code shoulld be part of the for each

Good news.
mysqli_ PHP ver. 5.0.0 +
PDO PHP ver 5.1 +

Do yourself a favor and leave mysql_

Thanks “Star…” It works

Why mysql should be leaved??

And what advantages n disadvantages if using mysqli or PDO beside mysql?

It’s being removed in PHP7. So your code will break eventually in the near future.