Jump to content

Mysql table values with comma


dk4210

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.