using an array to insert data into mysql
| From: | Rodney Davis | Date: | Thu, 19 Oct 2000 02:22:57 +0000 |
| Subject: | using an array to insert data into mysql | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-21049@lists.php.net to get a copy of this message | ||
I have created <input> tags based on a given variable from the
following form:
<td valign="top" width="122"><b>No. of
Variables</b>:<br>
<input type="text" name="number" size="15"
value="<?php print
"$number"; ?>">
</td>
I then create the appropriate number of fields with the following script
(thanks all for the previous help):
<?php
echo "<table><tr>";
for ($i = 0; $i < $number; $i++)
{
echo "<td>$i</td>\n <td><input type=\"text\"
name=\"variable[]\"
size=\"15\"></td>\n";
if (!($i % 3))
echo "<tr>\n";
}
}
echo "</tr></table>";
?>
Now I want to insert that data into a mysql table using the following
script.:
function addQuestion($question_id, $questionaire, $question_no, $format,
$variable, $question, $question_jp)
{
global $link;
if (!$question_id)
{
$query = "INSERT INTO questions set questionaire='$questionaire',
question_no='$question_no',
question='$question', question_jp='$question_jp'";
$result = mysql_query($query, $link);
if (!$result)
die ("updateOrg update error: ".mysql_error());
$sql = "SELECT question_id as question_id from questions WHERE
questionaire='$questionaire' AND question_no='$question_no'";
$res = mysql_query ($sql, $link);
if (!$res)
die ("addQuestion fatal error: ".mysql_error());
while ($ret = mysql_fetch_array($res))
$question_id = $ret["question_id"];
for ($i = 0; $i < count($variable[$i]); $i++)
{
$query_two = "INSERT INTO variable set questionaire='$questionaire',
";
$query_two .= "format='$format', variable='$variable[$i]',
question_id='$question_id'";
$result_two = mysql_query($query_two, $link);
if (!$result_two)
die ("addQuestion update error: ".mysql_error());
}
}
Unfortunately this only inserts the data from the last field and ignores the
others. I
must not be putting the data into an array correctly. My <input> tags have
the
name value as "name="variable[]" .
What am I doing wrong? Any help would be much appreciated.
thanks in advance,
rodney