0

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>";
}
6
  • Where is the code that executes your query? Commented Nov 27, 2013 at 20:53
  • 1
    I might be completely off my rocker but shouldn't you be calling your values like this $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 Commented Nov 27, 2013 at 21:25
  • I agree with MonkeyZeus, however you should probably change your input name settings. ` $desc[] ` should really just be $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. Commented Nov 27, 2013 at 21:31
  • Yes the user can add the form as many times as they want and it reappears on the screen. So there would be repeating form inputs. '$arrCount = count($_POST["itemname"]); $name = $_POST["itemname"]; $donor = $_POST["itemdonor"]; $descr = $_POST["itemdesc"]; $time = $_POST["itemtime"]; $hide = $_POST["hidden"]; for($i=0; $i<$arrCount; $i++){ $sql = "INSERT into bookhjr10_items ('name', 'donor', 'time', 'descr', 'hidden') VALUES ('$name[$i]', '$donor[$i]', '$descr[$i]', '$time[$i]', $hide[$i]')"; }' Commented Nov 27, 2013 at 21:34
  • Have a look at this answer on SO, it may help you out stackoverflow.com/a/14735483/1415724 - I Google'd "insert into mysqli php input array brackets" and found that answer. It's hard for me to say exactly where the problem is with your text inputs. @Pureblood Commented Nov 27, 2013 at 21:58

2 Answers 2

1

desc is a reserved word and needs to be wrapped in backticks.

Such as:

`desc`

$sql = "INSERT into items (name, donor, time, `desc`, hidden) VALUES ('$name[$i]', '$donor[$i]', '$desc[$i]', '$time[$i]', $hide[$i]')";

As MonkeyZeus stated and was about to write the same comment (at that very same time), it's best to wrap table names and columns with backticks.

For example:

$sql = "INSERT into `items` (`name`, `donor`, `time`, `desc`, `hidden`) VALUES ('$name[$i]', '$donor[$i]', '$desc[$i]', '$time[$i]', $hide[$i]')";

EDIT:

I noticed there is a missing quote for

  '$time[$i]', $hide[$i]'

- - - - - - - - - - - - - ^

Change to:

'$time[$i]', '$hide[$i]'

Full line of code:

$sql = "INSERT into `items` (`name`, `donor`, `time`, `desc`, `hidden`) VALUES ('$name[$i]', '$donor[$i]', '$desc[$i]', '$time[$i]', '$hide[$i]')";
Sign up to request clarification or add additional context in comments.

12 Comments

I usually recommend encasing ALL fields and table names in backticks when using MySQL
Yes I agree. I was just going to write that just as you wrote your comment. @MonkeyZeus
Thanks. I fixed that error but the date is still not getting to the datebase. I edited my original post to show full function. The image function is working fine and the file is getting uploaded. It is just that the text fields of the form are not getting through
I think $arrCount = count($_POST["itemname"]); definitely suggests the possibility of multiple items. Good catch on the mysql_ and mysqli_!
Also consider what MonkeyZeus said in a comment above. @Pureblood and the PHPfiddle phpfiddle.org/lite/code/scz-i1p he made.
|
0

I would suggest you change desc column because desc is a reserved mysql word for order, an application of it would be:

    SELECT * FROM my_table ORDER BY id DESC

thus you cannot use desc as a column name. To get info about such errors, its good to use the

    mysql_error()

function after every query i.e

    mysql_query($sql) or die(mysql_error())

So change that column name from desc to another name that's not a mysql reserved word. Good luck!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.