timmah1 Posted July 13, 2012 Share Posted July 13, 2012 I'm not sure what the problem is here, this has worked on 2 different sites before, but for some reason it's not now This upload gives the user the option of uploading 1, or multiple files by just clicking the plus sign to add another upload field, it uploads the files with the new names with no problem, the problem lies when it inserts into the database. Instead of creating 2 lines or more with the different file names, it's putting the same file name for each file that is uploaded I need to be able to insert each new_file name into the database. This is the code that gives the user the option <a href="javascript:_add_more();" title="Add more"><img src="./images/plus_icon.png" border="0"></a> <div id="dvFile">Click the <img src="./images/plus_icon.png" border="0" width="15"> symbol to add more photos<br /> <input type="file" name="item_file[]"> </div></div And here is the code for uploading if(count($_FILES["item_file"]['name'])>0) { //check if any file uploaded $GLOBALS['msg'] = ""; //initiate the global message for($j=0; $j < count($_FILES["item_file"]['name']); $j++) { //loop the uploaded file array $filen = $_FILES["item_file"]['name']["$j"]; //file name $ran = rand (); $new_file = $ran.$filen; $path = '../app_ads/'.$new_file ; //generate the destination path if(move_uploaded_file($_FILES["item_file"]['tmp_name']["$j"],$path)) { //upload the file $GLOBALS['msg'] .= "File(s) ".$new_file . " uploaded successfully<br>"; //Success message } } foreach($_FILES["item_file"]['name'] as $v){ $sql1 = mysql_query("INSERT INTO app_ads(app_id,app_ad,active) VALUES('$appid','$new_file','1')") or die(mysql_error()); } } Any help would be greatly appreciated. Thanks in advance Quote Link to comment Share on other sites More sharing options...
scootstah Posted July 13, 2012 Share Posted July 13, 2012 Well, the problem lies in the fact that you aren't using the iterated variable from the foreach() loop, but instead using the $new_file variable that came from the for() loop. Since it is no longer in the for() loop, it only holds the value for the last iteration. Really, though, you don't need two loops here, you can do everything in the for() loop. Also, you really shouldn't be looping the INSERT, but rather inserting multiple values. Here's what I would do: $insert_data = array(); for($j=0; $j < count($_FILES["item_file"]['name']); $j++) { //loop the uploaded file array $filen = $_FILES["item_file"]['name']["$j"]; //file name $ran = rand (); $new_file = $ran.$filen; $path = '../app_ads/'.$new_file ; //generate the destination path if(move_uploaded_file($_FILES["item_file"]['tmp_name']["$j"],$path)) { //upload the file $GLOBALS['msg'] .= "File(s) ".$new_file . " uploaded successfully<br>"; //Success message // add to $insert_data $insert_data[] = "($appid, '" . mysql_real_escape_string($new_file) . "', 1)"; } } // insert the data mysql_query("INSERT INTO app_ads(app_id, app_ad, active) VALUES " . implode(',', $insert_data) . "); Quote Link to comment Share on other sites More sharing options...
timmah1 Posted July 13, 2012 Author Share Posted July 13, 2012 Thank you, the problem now is that it won't insert into the database $insert_data = array(); for($j=0; $j < count($_FILES["item_file"]['name']); $j++) { //loop the uploaded file array $filen = $_FILES["item_file"]['name']["$j"]; //file name $ran = rand (); $new_file = $ran.$filen; $path = '../app_ads/'.$new_file ; //generate the destination path if(move_uploaded_file($_FILES["item_file"]['tmp_name']["$j"],$path)) { //upload the file $GLOBALS['msg'] .= "File(s) ".$new_file . " uploaded successfully<br>"; //Success message // add to $insert_data $insert_data[] = "($appid, '" . mysql_real_escape_string($new_file) . "', 1)"; } } //$value = implode(',', $insert_data); $sql = mysql_query("INSERT INTO app_ads(app_id, app_ad, active) VALUES(".implode(',', $insert_data).""); header("Location: page_articles.php#main-tabs-3-tab"); Quote Link to comment Share on other sites More sharing options...
scootstah Posted July 13, 2012 Share Posted July 13, 2012 Remove the parentheses around VALUES. VALUES(".implode(',', $insert_data)." Should be VALUES ".implode(',', $insert_data)." Quote Link to comment Share on other sites More sharing options...
timmah1 Posted July 13, 2012 Author Share Posted July 13, 2012 Remove the parentheses around VALUES. VALUES(".implode(',', $insert_data)." Should be VALUES ".implode(',', $insert_data)." When I do that, I get an error on this line header("Location: page_articles.php#main-tabs-3-tab"); Which is right below the insert statement Quote Link to comment Share on other sites More sharing options...
scootstah Posted July 13, 2012 Share Posted July 13, 2012 And what is the error? Quote Link to comment Share on other sites More sharing options...
timmah1 Posted July 13, 2012 Author Share Posted July 13, 2012 And what is the error? Parse error: syntax error, unexpected T_STRING in /home/local/public_html/members/page_articles.php on line 43 Quote Link to comment Share on other sites More sharing options...
Jessica Posted July 13, 2012 Share Posted July 13, 2012 $sql = mysql_query("INSERT INTO app_ads(app_id, app_ad, active) VALUES(".implode(',', $insert_data).""); It should have parens, but you are missing one of them. Try doing it this way, it makes it easier to see. In your example, you have malformed SQL, and then when you changed it you messed up the PHP syntax. Too many quotes and parens in one line makes it easy to happen. $insert_data_str = implode(',', $insert_data); $query = "INSERT INTO app_ads(app_id, app_ad, active) VALUES($insert_data_str)"; $result = mysql_query($query); Quote Link to comment Share on other sites More sharing options...
scootstah Posted July 13, 2012 Share Posted July 13, 2012 $sql = mysql_query("INSERT INTO app_ads(app_id, app_ad, active) VALUES(".implode(',', $insert_data).""); It should have parens, but you are missing one of them. Try doing it this way, it makes it easier to see. In your example, you have malformed SQL, and then when you changed it you messed up the PHP syntax. Too many quotes and parens in one line makes it easy to happen. $insert_data_str = implode(',', $insert_data); $query = "INSERT INTO app_ads(app_id, app_ad, active) VALUES($insert_data_str)"; $result = mysql_query($query); No, the parentheses are in the array, which is being imploded. Multiple INSERT syntax is like this: VALUES (1,2,3), (4,5,6), (7,8,9) What you are suggesting ends up like this (based off my code): VALUES( (1,2,3), (4,5,6), (7,8,9) ) Quote Link to comment Share on other sites More sharing options...
timmah1 Posted July 13, 2012 Author Share Posted July 13, 2012 $sql = mysql_query("INSERT INTO app_ads(app_id, app_ad, active) VALUES(".implode(',', $insert_data).""); It should have parens, but you are missing one of them. Try doing it this way, it makes it easier to see. In your example, you have malformed SQL, and then when you changed it you messed up the PHP syntax. Too many quotes and parens in one line makes it easy to happen. $insert_data_str = implode(',', $insert_data); $query = "INSERT INTO app_ads(app_id, app_ad, active) VALUES($insert_data_str)"; $result = mysql_query($query); Thank you. I no longer have the error, but it still does not insert the data $insert_data = array(); for($j=0; $j < count($_FILES["item_file"]['name']); $j++) { //loop the uploaded file array $filen = $_FILES["item_file"]['name']["$j"]; //file name $ran = rand (); $new_file = $ran.$filen; $path = '../app_ads/'.$new_file ; //generate the destination path if(move_uploaded_file($_FILES["item_file"]['tmp_name']["$j"],$path)) { //upload the file $GLOBALS['msg'] .= "File(s) ".$new_file . " uploaded successfully<br>"; //Success message // add to $insert_data $insert_data[] = "$appid, '" . mysql_real_escape_string($new_file) . "', 1"; } } //$value = implode(',', $insert_data); $insert_data_str = implode(',', $insert_data); $query = "INSERT INTO app_ads(app_id, app_ad, active) VALUES($insert_data_str)"; $result = mysql_query($query); header("Location: page_articles.php#main-tabs-3-tab"); Quote Link to comment Share on other sites More sharing options...
scootstah Posted July 13, 2012 Share Posted July 13, 2012 I no longer have the error, but it still does not insert the data It doesn't insert the data because the SQL is incorrect. If you do a mysql_error() after the query, you will see the SQL error. This is what it needs to be: $insert_data_str = implode(',', $insert_data); $query = "INSERT INTO app_ads(app_id, app_ad, active) VALUES $insert_data_str"; $result = mysql_query($query); Quote Link to comment Share on other sites More sharing options...
timmah1 Posted July 13, 2012 Author Share Posted July 13, 2012 Nevermind, I had double $$ before insert_data_str It works now Thanks for everybody's help Quote Link to comment Share on other sites More sharing options...
scootstah Posted July 13, 2012 Share Posted July 13, 2012 You have two $'s for the $insert_data_str variable. Do this for me, if you would: 1. Put this code at the top of your script: error_reporting(-1); ini_set('display_errors', 1); to make sure errors are being displayed properly. 2. Replace $result = mysql_query($query); with $result = mysql_query($query) or die("MySQL error!<br />Query: $query<br />Error: " . mysql_error()); Post back with the exact errors as displayed on your screen (if there is any). Quote Link to comment Share on other sites More sharing options...
Jessica Posted July 13, 2012 Share Posted July 13, 2012 Multiple INSERT syntax is like this: VALUES (1,2,3), (4,5,6), (7,8,9) What you are suggesting ends up like this (based off my code): VALUES( (1,2,3), (4,5,6), (7,8,9) ) I see now, thanks for explaining. My point about mismatched parens was still true though, he had an opening with no closing, and then bad PHP syntax. I agree with scootstah that OP needs error reporting on. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.