Alright I am trying to upload images and then an image description. The image is uploaded to the database but the text fields are not.
I thought my code was correct but for some reason nothing is going to the database. I think nothing is getting stored into the array and I'm not sure why.
Here is my form.
<form method="post" enctype="multipart/form-data" action="process.php">
<div id="filediv">
<div id="imagefiles">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<label>Upload File:
<input name="userfile[]" type="file" id="userfile" multiple></label>
<label>Item Name: <input name='itemname[]' type="text"></label>
<label>Item Description: <input name="itemdesc[]" type="text"></label>
<label>Item Timeframe: <input name='itemtime[]' type="text"></label>
<label>Item Donor: <input name='itemdonor[]' type="text"></label>
<label>Hidden Information: <input name='hidden[]' type="text"></label>
</div>
</div>
<br>
<input type="button" id ="thebutton" value="Add Another File" />
<input name="UploadFile" type="submit" />
</form>
Here is my function
function uploadFile()
{
include 'functions.php';
hasSession();
session_start();
$dbhost = '';
$dblogin = '';
$dbpass = '!';
$dbbase = '';
$conn = mysqli_connect($dbhost, $dblogin, $dbpass, $dbbase);
if(mysqli_connect_errno()){
printf("Can't connect to MySQL Server. Errorcode: %s\n", mysqli_connect_error());
exit;
}
$arrCount = count($_POST["itemname"]);
$name[] = $_POST["itemname"];
$donor[] = $_POST["itemdonor"];
$desc[] = $_POST["itemdesc"];
$time[] = $_POST["itemtime"];
$hide[] = $_POST["hidden"];
for($i=0; $i<$arrCount; $i++){
$sql = "INSERT into items (name, donor, time, desc, hidden) VALUES ('$name[$i]', '$donor[$i]', '$desc[$i]', '$time[$i]', $hide[$i]')";
}
$numFiles = count($_FILES['userfile']['name']);
for($x=0;$x<$numFiles;$x++){
//($_FILES['userfile'][$counter]['error'] == 0) &&
if (($_FILES['userfile']['size'][$x] > 0)){
$fileName = $_FILES['userfile']['name'][$x];
$tmpName = $_FILES['userfile']['tmp_name'][$x];
$fileSize = $_FILES['userfile']['size'][$x];
$fileType = $_FILES['userfile']['type'][$x];
if ((($_FILES['userfile']['type'][$x] == "image/gif")
|| ($_FILES['userfile']['type'][$x]== "image/jpeg")
|| ($_FILES['userfile']['type'][$x] == "image/png")
|| ($_FILES['userfile']['type'][$x] == "image/pjpeg")))
{
$fp=fopen($tmpName,'r');
$content=fread($fp,filesize($tmpName));
$SourceImage=imagecreatefromstring($content);
$SourceWidth=imagesx($SourceImage);
$SourceHeight=imagesy($SourceImage);
$ratio=$SourceWidth/$SourceHeight;
$DestWidth=100;
$DestHeight=$DestWidth/$ratio;
$DestinationImage=imagecreatetruecolor($DestWidth,$DestHeight);
imagecopyresampled($DestinationImage,$SourceImage,0,0,0,0,$DestWidth,$DestHeight,$SourceWidth,$SourceHeight);
ob_start();
imagejpeg($DestinationImage);
$BinaryThumbnail = ob_get_contents();
ob_end_clean();
$thumb = addslashes($BinaryThumbnail);
$content=addslashes($content);
fclose($fp);
$fileName = addslashes($fileName);
mysql_connect('hartslogmuseum.db.11661984.hostedresource.com','hartslogmuseum','Alexandria1!');
mysql_select_db('hartslogmuseum');
mysql_query("INSERT INTO images (name,size,type,content,thumbnail) VALUES ('$fileName','$fileSize','$fileType','$content','$thumb')") or die('Error, query failed');
}else{
$fp=fopen($tmpName,'r');
$content=fread($fp,filesize($tmpName));
$content=addslashes($content);
fclose($fp);
$fileName = addslashes($fileName);
$link = mysqli_connect('!');
mysqli_select_db($link,'');
mysqli_query($link,"INSERT INTO images (name,size,type,content) VALUES ('$fileName','$fileSize', '$fileType','$content')");
}
}else{
echo "Error: ". $_FILES['userfile']['error'][$x]."<br/>";
}
}
echo "<script>alert('The file(s) has been uploaded');location.replace('uploaded.php');</script>";
}
$name[0][$i]since you are assigning them like this$name[] = $_POST["itemname"];. If you want to leave your SQL unchanged then can you try assigning the variables like this$name = $_POST["itemname"];? PHPFiddle$desc. Otherwise this variable will resolve to an array of length one where the value of that one pair is an array of values of itemdesc.