Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

i had prepared below form, i need data insert to mysql through this form but problem is when i try to insert array_filter gives problem. I need only selected box values data to mysql.

example: when user selected select box 1st row and 5th row only these two rows should insert to mysql, instead of this 5 rows inserting with empty values remain data also inserting. please help

<form action="array-act.php" method="post">
<table width="500" border="0">
<tr>
<td><input name="name[]" value="One" type="text" /></td>
<td><input name="marks[]" type="text" /></td>
<td><input name="grade[]" type="text" /></td>
<td><select name="first[]"><option value="">Select</option><option value="SM">SM</option><option value="QB">QB</option></select></td>
</tr>
<tr>
<td><input name="name[]"  value="Two" type="text" /></td>
<td><input name="marks[]" type="text" /></td>
<td><input name="grade[]" type="text" /></td>
<td><select name="first[]"><option value="">Select</option><option value="SM">SM</option><option value="QB">QB</option></select></td>
</tr>
<tr>
<td><input name="name[]" value="Three" type="text" /></td>
<td><input name="marks[]" type="text" /></td>
<td><input name="grade[]" type="text" /></td>
<td><select name="first[]"><option value="">Select</option><option value="SM">SM</option><option value="QB">QB</option></select></td>
</tr>
<tr>
<td><input name="name[]" value="Four" type="text" /></td>
<td><input name="marks[]" type="text" /></td>
<td><input name="grade[]" type="text" /></td>
<td><select name="first[]"><option value="">Select</option><option value="SM">SM</option><option value="QB">QB</option></select></td>
</tr>
<tr>
<td><input name="name[]" value="Five" type="text" /></td>
<td><input name="marks[]" type="text" /></td>
<td><input name="grade[]" type="text" /></td>
<td><select name="first[]"><option value="">Select</option><option value="SM">SM</option><option value="QB">QB</option></select></td>
</tr>
</table>
<input name="Go" type="submit" />
</form>
<?php
$name = $_POST['name'];
$marks = $_POST['marks'];
$grade = $_POST['grade'];
$option = array_values(array_filter($_POST['first']));


$n = count($option);
for ($i = 0; $i < $n; $i++) 
{
$query = "INSERT INTO `table` (`name`, `marks`, `grade`, `option`) 
VALUES ('$name[$i]', '$marks[$i]', '$grade[$i]', '$option[$i]')"; 
// Here you must execute your query
$result = mysql_query($query) or die('Failed to connect to server: ' . mysql_error());
}
if($result)
{
echo "Data  Inserted";
}
else
{
echo "Data Not Inserted";
}
?>
share|improve this question
 
I can't see anything wrong with that code. Can you var_dump($_POST) and show the output? Preferably once before you filter it and then again after it. –  George Reith Jul 18 '13 at 8:00
add comment

3 Answers

I think you should give numbers to the name attribute of the elements of each row

<td><input name="name[1]" value="One" type="text" /></td>
<td><input name="marks[1]" type="text" /></td>
<td><input name="grade[1]" type="text" /></td>
<td><select name="first[1]"><option value="">Select</option><option value="SM">SM</option><option value="QB">QB</option></select></td>

and so on.

That would solve your problem. Right now each option list has the same name, so only one should be returned.

share|improve this answer
 
not working tried, it is working when i entered all fields data but it is not working when i entered data only to first and last row. –  Bolem Veeru Jul 18 '13 at 8:01
 
then in your main loop you should check if the all values exist. If one is missing, do not execute the query. –  John Krommidas Jul 18 '13 at 8:03
 
it is taking sequence order when inserting data. how is it possible? –  Bolem Veeru Jul 18 '13 at 8:07
 
the code is wrong. you should parse the whole array and check the first value of the entry you want to add to the database. –  John Krommidas Jul 18 '13 at 8:15
add comment

Although I can't see anything wrong with your original code, try this instead:

$name = $_POST['name'];
$marks = $_POST['marks'];
$grade = $_POST['grade'];
$first = $_POST['first'];
array_walk($first, 'insert');

function insert($v, $i) {
    if ($v !== "") {
        $query = "INSERT INTO `table` (`name`, `marks`, `grade`, `option`) VALUES ('$name[$i]', '$marks[$i]', '$grade[$i]', '$option[$i]')"; 
        $result = mysql_query($query) or die('Failed to connect to server: ' . mysql_error());
        if($result) {
            echo "Data  Inserted";
        } else {
            echo "Data Not Inserted";
        }
    }
}
share|improve this answer
 
Not worked thanks for u r effort. –  Bolem Veeru Jul 20 '13 at 5:21
 
@BolemVeeru It does work, I have tested it. The only reason it wouldn't be working is if your $_POST['first'] array isn't empty strings. You need to var_dump($_POST['first']) and show the output. –  George Reith Jul 21 '13 at 7:43
add comment

The code is correct. Only problem is in line :

 $option = array_values(array_filter($_POST['first']));

Here it looses the key indexes and re-index it. This is due to array_values.

share|improve this answer
 
can you tell me how to re-index it –  Bolem Veeru Jul 20 '13 at 6:08
 
you should not use array_values in $option = array_values(array_filter($_POST['first'])); –  user1843951 Jul 24 '13 at 10:16
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.