Jump to content

Insert Data From Array


son
 Share

Recommended Posts

I 'converted' a select to allow for multiple options and am not sure how to insert relevant rows into db. First I check if something is entered as: if (isset($_POST['category']) && ($_POST['category']) != 0) { $category = escape_data($_POST['category']); } else { $category = FALSE; $errors['category'] = '\'Category\' is required'; }I display the select drop down as:<p><label for="category"><?php echo check_error('category', 'Category*'); ?></label> <select name="category[]" id="category[]" multiple="multiple"><option value="0">Please select a category...</option><?php$q = 'SELECT category_id, category FROM categories ORDER BY category';$r = mysqli_query($dbc, $q);while (list($category_id, $category) = mysqli_fetch_array($r, MYSQLI_NUM)) {echo "<option value=\"$category_id\"";if((isset($_POST['category'])) && ($_POST['category'] == $category_id)){ echo ' selected="selected" '; }echo ">" . $category . "</option>\n";}echo "</select></p>";and try unsuccessfully to insert data into database as:foreach ($category as $v) { $productAssocQ = "INSERT INTO productCat (category_id, product_id) VALUES (array('$category', '$id'))"; { if ($productAssocRes = mysqli_query ($dbc, $productAssocQ)) echo "<p>The product has been added!<br />"; echo "<span class=\"small\"><a href=\"products.php\"><< Go back</a></span></p>"; include('inc/footer.php'); exit; } }I know this might be quite wrong, but not sure how else to do this...Son

Link to comment
Share on other sites

Have you printed $_POST['category'] to see what it's submitting when you select more than one?
I get the error message:An error occurred in script '/home/sites/domain.co.uk/ccl/del.php' on line 30:Array to string conversion and the following info clearly shows both selected option in array as
    [_POST] => Array        (            [MAX_FILE_SIZE] => 524288            [category] => Array                (                    [0] => 7                    [1] => 8                )            [designer] => 5

Do I need to change:

        if (isset($_POST['category']) && ($_POST['category']) != 0)	{		$category = escape_data($_POST['category']);	}	else	{		$category = FALSE;		$errors['category'] = '\'Category\' is required';		}

to allow for the array?Son

Link to comment
Share on other sites

An error occurred in script '/home/sites/domain.co.uk/ccl/del.php' on line 30:Array to string conversion
That's a Javascript error, you need to check which source line is line 30.You're using a function called escape_data, does that work on arrays? Or does that only work on string data? You're giving it the entire array. If you want to add those to a database, you'll need to either loop through the array and add each item, or join the array and add the entire list.
Link to comment
Share on other sites

That's a Javascript error, you need to check which source line is line 30.You're using a function called escape_data, does that work on arrays? Or does that only work on string data? You're giving it the entire array. If you want to add those to a database, you'll need to either loop through the array and add each item, or join the array and add the entire list.
How do you loop through the array and add each item? Have never done such a thing...Son
Link to comment
Share on other sites

You can use any loop structure to go through an array:http://www.php.net/manual/en/language.control-structures.phpA for loop or foreach loop are most common for arrays, but you could also use a while or do loop.How did you make it this far without ever looping through an array?

Edited by justsomeguy
Link to comment
Share on other sites

You can use any loop structure to go through an array:http://www.php.net/manual/en/language.control-structures.phpA for loop or foreach loop are most common for arrays, but you could also use a while or do loop.How did you make it this far without ever looping through an array?
Sorry, I did mean I have never done such a thing for a multiple select. I have used loops before and am familiar more or less with general structure. Do I have to do it for select the same way as with any array?Son
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...