dk4210 Posted November 18, 2010 Share Posted November 18, 2010 Hello Guys, I am trying to insert a value in my table like this image.jpg,image2.jpg,image3.jpg Currently it just runs the image names together Here is my code.. I tried to use explode, but it just adds the word "array" in the table. Please advise $filename2 = $phstring; } $csv = $filename2; $fphoto = explode(',', $csv); // Insert ad details into mysql $query = "INSERT INTO ads (ad_type,ad_title,ad_body,ad_category,ad_photo,ad_member,ad_status,ad_city,ad_state,ad_zip)VALUES('".$ad_type."','".$ad_title."','".$ad_body."','".$catid."','".$fphoto."','".$vname."','".$ad_status."','".$city."','".$state."','".$zip."')"; Link to comment https://forums.phpfreaks.com/topic/219096-mysql-table-values-with-comma/ Share on other sites More sharing options...
litebearer Posted November 18, 2010 Share Posted November 18, 2010 are you trying to put ALL the content of $filename2 into ONE field of the table? Link to comment https://forums.phpfreaks.com/topic/219096-mysql-table-values-with-comma/#findComment-1136148 Share on other sites More sharing options...
dk4210 Posted November 18, 2010 Author Share Posted November 18, 2010 That is correct sir.. [attachment deleted by admin] Link to comment https://forums.phpfreaks.com/topic/219096-mysql-table-values-with-comma/#findComment-1136156 Share on other sites More sharing options...
BlueSkyIS Posted November 18, 2010 Share Posted November 18, 2010 $fphoto = implode(',', $csv); Link to comment https://forums.phpfreaks.com/topic/219096-mysql-table-values-with-comma/#findComment-1136159 Share on other sites More sharing options...
dk4210 Posted November 18, 2010 Author Share Posted November 18, 2010 Hi Blue Sky, Getting an error Updated code $filename2 = $phstring; } $csv = $filename2; $fphoto = implode(',', $csv); // Insert ad details into mysql $query = "INSERT INTO ads (ad_type,ad_title,ad_body,ad_category,ad_photo,ad_member,ad_status,ad_city,ad_state,ad_zip)VALUES('".$ad_type."','".$ad_title."','".$ad_body."','".$catid."','".$fphoto."','".$vname."','".$ad_status."','".$city."','".$state."','".$zip."')"; // Error checking to see if the query was successful $result = mysql_query($query) or die (mysql_error()."in <br>$query" ); Warning: implode() [function.implode]: Invalid arguments passed in Link to comment https://forums.phpfreaks.com/topic/219096-mysql-table-values-with-comma/#findComment-1136162 Share on other sites More sharing options...
ninedoors Posted November 18, 2010 Share Posted November 18, 2010 Make sure that $csv is an array. One way you can check it is: $test = is_array($csv) ? 'yes' : 'no'; echo $test; or you can print_r($csv); Link to comment https://forums.phpfreaks.com/topic/219096-mysql-table-values-with-comma/#findComment-1136170 Share on other sites More sharing options...
litebearer Posted November 18, 2010 Share Posted November 18, 2010 BTW what is the 'structure' of the content of $filename2? Link to comment https://forums.phpfreaks.com/topic/219096-mysql-table-values-with-comma/#findComment-1136186 Share on other sites More sharing options...
dk4210 Posted November 18, 2010 Author Share Posted November 18, 2010 Hi ninedoors, When I run the code you provided it says no.. $filename2="/images/default.jpg"; }else{ echo "This is the file name" . $phstring; $filename2 = $phstring; $test = is_array($filename2) ? 'yes' : 'no'; echo $test; } // Insert ad details into mysql $query = "INSERT INTO ads (ad_type,ad_title,ad_body,ad_category,ad_photo,ad_member,ad_status,ad_city,ad_state,ad_zip)VALUES('".$ad_type."','".$ad_title."','".$ad_body."','".$catid."','".$filename2."','".$vname."','".$ad_status."','".$city."','".$state."','".$zip."')"; // Error checking to see if the query was successful $result = mysql_query($query) or die (mysql_error()."in <br>$query" ); The content for this is images names inserted into the database Link to comment https://forums.phpfreaks.com/topic/219096-mysql-table-values-with-comma/#findComment-1136298 Share on other sites More sharing options...
litebearer Posted November 18, 2010 Share Posted November 18, 2010 Can you show the code that populates the file . Link to comment https://forums.phpfreaks.com/topic/219096-mysql-table-values-with-comma/#findComment-1136306 Share on other sites More sharing options...
dk4210 Posted November 18, 2010 Author Share Posted November 18, 2010 Here ya go lite if (isset($_POST['Submitad'])) { echo "Congrats ad has been submitted"; //print_r($_POST); $catid = $_POST['catname']; $ad_title = $_POST['ad_title']; $ad_body = $_POST['description']; $vname = $_POST['viewname']; $vphone = $_POST['vphone']; $city = $_POST['city']; $state = $_POST['state']; $zip = $_POST['zip']; $subn = $_SESSION['fname'] . $_SESSION['lname'] ; $ad_status = '1'; // Future use $ad_type = 'free'; // Find out what images are in the temp_photo directory foreach (glob("temp_photo/$subn*.*") as $filename) { echo "$filename<br>"; $string = $filename; $filename2 = basename($string); // Combining all the vars into one for the query down below $phstring .= $filename2; // Create a folder name for each new members image in the perm directory if(!file_exists($subn)) { @mkdir("ad_photos/" . $subn); } //echo "This is the directory" . $dp; // Move photos to perm directory being that the form as been submitted copy("temp_photo/$filename2","ad_photos/$subn/$filename2"); } // Remove all temp files foreach (glob("temp_photo/$subn*.*") as $filename3) { $tmpfile = $filename3; unlink($tmpfile); } /* Lets do some cleaning up the temp directory, If there are users that upload images, but do not submit, then we want to clean that up randomly */ $expiretime=3600; //expire time in minutes $tmpFolder="temp_photo/"; $fileTypes="*.*"; foreach (glob($tmpFolder . $fileTypes) as $Filename2) { //echo "$Filename2<br>"; // Read file creation time $FileCreationTime = filectime($Filename2); // Calculate file age in seconds $FileAge = time() - $FileCreationTime; // Is the file older than the given time span? if ($FileAge > ($expiretime * 60)){ // Now do something with the olders files... //print "The file $Filename2 is older than $expire_time minutes\n"; //deleting files: unlink($Filename2); } } // If there are no images uploaded insert the default image if($phstring == ""){ $filename2="/images/default.jpg"; }else{ echo "This is the file name" . $phstring; $filename2 = $phstring; $test = is_array($filename2) ? 'yes' : 'no'; echo $test; } // Insert ad details into mysql $query = "INSERT INTO ads (ad_type,ad_title,ad_body,ad_category,ad_photo,ad_member,ad_status,ad_city,ad_state,ad_zip)VALUES('".$ad_type."','".$ad_title."','".$ad_body."','".$catid."','".$filename2."','".$vname."','".$ad_status."','".$city."','".$state."','".$zip."')"; // Error checking to see if the query was successful $result = mysql_query($query) or die (mysql_error()."in <br>$query" ); exit; } Link to comment https://forums.phpfreaks.com/topic/219096-mysql-table-values-with-comma/#findComment-1136332 Share on other sites More sharing options...
litebearer Posted November 19, 2010 Share Posted November 19, 2010 Sorry about the delay. In your most recent code, you do this ... $phstring .= $filename2; The problem you are experiencing is due to the fact that as you add additional names to the above variable, you are NOT placing a 'delimiter' between the names, you are simply 'crushing' them one after another. Try changing the above, to this $phstring .= $filename2 . ","; That way it will have commas between each name. Of course, you will have to remove the LAST comma once all the file names have been added. I think that should solve your problem (no need to explode or implode anything) Link to comment https://forums.phpfreaks.com/topic/219096-mysql-table-values-with-comma/#findComment-1136573 Share on other sites More sharing options...
dk4210 Posted November 19, 2010 Author Share Posted November 19, 2010 Hi Lite! That worked! I think everyone thought it was an array but it was in the foreach loop. How would I get rid of the last comma? Again thanks for your help.. Link to comment https://forums.phpfreaks.com/topic/219096-mysql-table-values-with-comma/#findComment-1136581 Share on other sites More sharing options...
dk4210 Posted November 19, 2010 Author Share Posted November 19, 2010 Do I use the str_replace function for that? Link to comment https://forums.phpfreaks.com/topic/219096-mysql-table-values-with-comma/#findComment-1136583 Share on other sites More sharing options...
litebearer Posted November 19, 2010 Share Posted November 19, 2010 yes, str_replace should work fine Link to comment https://forums.phpfreaks.com/topic/219096-mysql-table-values-with-comma/#findComment-1136585 Share on other sites More sharing options...
dk4210 Posted November 19, 2010 Author Share Posted November 19, 2010 Would you mind giving me an example? I see how to use the function but not sure how to tell it to replace the last comma Link to comment https://forums.phpfreaks.com/topic/219096-mysql-table-values-with-comma/#findComment-1136586 Share on other sites More sharing options...
litebearer Posted November 19, 2010 Share Posted November 19, 2010 $str = substr($str,'',-1); Link to comment https://forums.phpfreaks.com/topic/219096-mysql-table-values-with-comma/#findComment-1136589 Share on other sites More sharing options...
dk4210 Posted November 19, 2010 Author Share Posted November 19, 2010 Worked great! Thanks again.. does the "-1" mean at the end of the filename? Link to comment https://forums.phpfreaks.com/topic/219096-mysql-table-values-with-comma/#findComment-1136614 Share on other sites More sharing options...
litebearer Posted November 19, 2010 Share Posted November 19, 2010 -1 means all except for the last character Link to comment https://forums.phpfreaks.com/topic/219096-mysql-table-values-with-comma/#findComment-1136619 Share on other sites More sharing options...
dk4210 Posted November 19, 2010 Author Share Posted November 19, 2010 Fair enough... Thanks for your help! Link to comment https://forums.phpfreaks.com/topic/219096-mysql-table-values-with-comma/#findComment-1136628 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.