Forum Moderators: coopster
Well, I'm programming a site that uses a dynamic field insertion into a mySQL database. In short, the user can decide on the specific page if he adds ONE line (to be added to the database) or two/three/four and so on, by clicking on a "ADD LINE", that adds up a table row with pre-set inputs.
The inputs have names, and after the user clicks the "ADD TO DB" button, I want to add the number of rows he set up into the db, using an array.
Can I use array names ALREADY in the field names?
for instance:
<input type=text name="QuestionParams[1]['Text']">
<input type=text name="QuestionParams[1]['Number']">
<input type=text name="QuestionParams[2]['Text']">
<input type=text name="QuestionParams[2]['Number']">
(of course, if there are any more records, the system uses the same fields with a change of the counter)
I tried using this code to insert the records into the db:
$TotalQuestions=(count($QuestionParams));
for ($i=1;$i<=$TotalQuestions;$i++)
{
$Query = "INERT INTO tblQuestions(qNum,qText) VALUES('".$i."','".$QuestionParams[$i]["Text"]."')";
$queryexe = mysql_query($Query);
}
Anyone knows what I should do?
Thankyou!
my first step at this point is to take a good look at what is actually available in the POST array to make sure I didn't blow something on the form, it also helps me to understand what exactly I need to test for, try this
echo '<pre>';
print_r($_POST);
echo '</pre>';
this will dump the POST array to the browser and you can look through it to see what exactly is going on. I would put it in right near the top of the form processing script before you start messing around with all of the post data.
But, it still didn't help completely. What I got is this:
[QuestionParams] => Array
(
[\'1\'] => Array
(
[\'Subj\'] => test2
[\'Text\'] => sdfsdfsf
[\'Num\'] => 1
[\'Weight\'] => 200
[\'Ans1\'] => ans1nla
[\'Ans2\'] => ans2lala
[\'Ans3\'] =>
[\'Ans4\'] =>
)
)
And my current problem is how to get those \' off. I tried setting my initial field names as
QuestionParams[1][Text]
instead of QuestionParams['1']['Text']
but it doesnt work!
Ideas? :\\
Thanks btw ;) great forum hehe
~mooey
first, SORRY I was a bit frustrated, so I posted too early - I forgot to delete the excess " from some other inputs and that caused the code to not recognize my fields. After erasing them all, it's WORKING!
Sorry, I see there's no "edit" feature on the forum so I can't delete my previous "not working" post .. err.. appologies :)
lots of thanks!
~mooey
I used this form
<form name='testarr1' method='post' action='spitout.php'>
<p>1
<br><input type=text name="text1">
<br><input type=text name="number1">
<p>2
<br><input type=text name="text2">
<br><input type=text name="number2">
<p>3
<br><input type=text name="text3">
<br><input type=text name="number3">
<p>4
<br><input type=text name="text4">
<br><input type=text name="number4">
<p><input type='submit' value='testarr'>
</form>
which can easily be generated dynamically, same field names, just append a number to the base string using a counter
then I used this to test the processing (by the way, you missed an N in the word INSERT)
<?
echo '<pre>';
print_r($_POST);
echo '</pre>';
$counter = 1;
while (isset($_POST['text' . $counter])) {
$Query = "INSERT INTO tblQuestions(qNum,qText) VALUES('".$counter."','".$_POST['text' . $counter]."')";
echo "<p>$counter: ",$Query;
$counter++;
}
?>
we construct the varnames in the loop, we don't need to count them, I assumed that if you are entering a question there must always be text and used that as the control field. It checks for testx where x is the present counter value.
light and tight and gets the job done ;)
<added>posted with out seeing mooey's reply
[edited by: jatar_k at 8:19 pm (utc) on May 2, 2005]