Log in

View Full Version : $_POST[] comma seperated string, exlode and explode again before insert into DB



antonyf
02-02-2014, 12:15 PM
Hi i have a comma separated. Each seperated value contains a string which is seperated by a "|".. I need to explode the second string and insert the multiple data into the dB..

here is my code:



here is my post data example:


// name|surname|acc@domain.com|contact,johnny|smith|jsmith@jjjjj.com|contact, and so on.. I need to insert the individual data into the db and if successful echo 1 else error..

$contacts = $_POST['conts'];
if(!empty($contacts)){
$one[] = explode(',',$contacts);
foreach($one as $two){
$three[] = explode('|', $two);
foreach($three as $four){
$sql="INSERT INTO jos_mycont (`prop_userid`,`category`,`firstname`,`surname`,`email`) VALUES ('$thisJRUser->id','$four[3]','$four[0]','$four[1]','$four[2]')";
mysql_query($sql,$contLink);
}

if (!mysql_query($sql,$contLink)){
echo "Unable to insert client details";
exit;
} else {
echo '1';
exit;
}
}
} else {
echo 'No contacts selected';
exit;
}

djr33
02-02-2014, 05:05 PM
Ok. Is there a question? Please be specific about what is not working in the code.

traq
02-02-2014, 11:22 PM
You might look at the str_getcsv() (http://php.net/str_getcsv) function. Beyond that, you'd need to be more specific about what your question is.