I'm trying to figure out how to take the array $_POST['custom'] and insert the values into separate rows in a mysql database.
var_dump($_POST['custom']) gives the array array(1) { [0]=> string(12) "item1, item2, " },
I've tried foreach and echo to see if I can produce the lines individually, but I just get the entire array returned:
$array = $_POST['custom'];
foreach ($array as $item)
{
echo $item,'-';
}
and the output is item1, item2, - instead of being item1-item2
what I'm trying to do is insert each item into a separate row, along with the user_id that purchased the item - for example:
user_id | item
-----------------
user_1 | item1
user_1 | item2
I can insert into a single row by using json_encode($_POST['custom']) and then your standard insert query even though the formatting is a bit strange with the quotes and [] (I've tried both implode and explode they don't work - but I can live with that for now). That table looks like
user_id | item
-----------------
user_1 | ["item1,item2,"]
My main issue is how to get the array of values item1,item2,etc to be used as a individual value to insert data into a single row per item so that it looks like the first table above.
EDIT
Html form
<form name="paypalCheckout" action="<?php echo PPCART_URL; ?>" method="post">
<input type="HIDDEN" name="business" value="<?php echo PPCART_ACCOUNT; ?>">
<input type="HIDDEN" name="cmd" value="_cart">
<input type="HIDDEN" name="upload" value="1">
<input type="hidden" name="currency_code" value="<?php echo PPCART_CURRENCY; ?>">
<input type="hidden" name="lc" value="<?php echo PPCART_COUNTRY; ?>">
<input type="hidden" name="return" value="<?php echo PPCART_RETURN_URL; ?>">
<?php
if (isset ($_SESSION['paypalCart']))
{
foreach($_SESSION['paypalCart'] as $product)
{
$custom .= $product['name'].",";
}
}
?>
<input type="hidden" name="custom" value="<?php echo $custom?>">
//<input type="hidden" name="custom[]" value="<?php echo $custom?>"> I have also tried this
the relevant part is the $custom variable - it is initialized already as $custom="";
Solution
<?php
if (isset ($_SESSION['paypalCart']))
{
foreach($_SESSION['paypalCart'] as $product)
{
$custom .= $product['name'].",";
}
}
?>
<input type="hidden" name="custom" value="<?php echo $custom?>">
then in the processing php file I have
$db = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
$user_id = "Test";
$custom = $_POST['custom'];
//print_r($_POST['custom']); This is good to see what your array looks like before it gets trimmed
$trim = rtrim($custom, ",");
$explode = explode(',',$trim);
//print_r($_POST['custom']); Good way to check what it looks like after being trimmed to make sure you have trimmed it correctly.
foreach ($explode as $course_id)
{
$sql = $db->query("INSERT INTO `subscriptions`(`user_id`, `course_id`) VALUES ('".$user_id."','".$course_id."')");
if($sql)
{
echo "it's good";
}
else
echo "no good", mysql_error();
}
Array ( [0] => item1,item2, )explode() expects parameter 2 to be string, array given inorArray to string conversion inArrayand I used both","and','with no luck