Jump to content

Aghh!!!


Eugene

Recommended Posts

I need some serious help. Here are 2 pieces of code from a webpage.

[code]function add_msn() {
echo "<div class=\"tableborder\"><div class=\"maintitle\">Add members to the Database here</div>";
echo "How many members would you like to add?";
echo "<form method=\"post\">
<select name=\"number\">";
for($i = 1; $i <= 100; $i++) {
echo "
<option value=\"$i\">
$i
</option>";
}
echo "
</select>
<br><br>
<input type=\"submit\" value=\"Submit\" name=\"submit\"><br><br>
</form>";

$number = $_POST['number'];
$submit = $_POST['submit'];


if($submit) {
echo "</div><br></td></tr>
<tr>
<td>&nbsp;</td>
<td valign=\"top\">
<div class=\"tableborder\">
<form method=\"post\" onsubmit=\"return formValidation(this)\">
<div class=\"maintitle\">Enter your Member names here</div><br><br>";
for($i = 1; $i <= $number; $i++) {
echo "$i)&nbsp;<br>
Real Name:&nbsp;<input type=\"text\" name=\"rsid\"><br>
MSN Identity:&nbsp;<input type=\"text\" name=\"msnid\"><br>
Member Rank:<select name=\"rank\">";
mysql_connect("*", "*", "*") or die(mysql());
mysql_select_db("*") or die(mysql());

$result = mysql_query("SELECT * FROM *")
or die(mysql_error());

while($row = mysql_fetch_array( $result )) {
$dbrank = $row['rank'];

echo "<option>$dbrank</option>";
}
echo "
</select>
<br>";
}


echo "<input type=\"submit\" name=\"sbtdb\" value=\"Submit MSN list\">
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type=\"reset\">
</form>
</div>";
}

[/code]

That works fine and so does this:

[code]
$dbsubmit = $_POST['sbtdb'];
$msnid = $_POST['msnid'];
$rank = $_POST['rank'];
$rsid = $_POST['rsid'];

if($dbsubmit) {

mysql_connect("*", "*", "*") or die(mysql_error());
mysql_select_db("*") or die(mysql_error());


mysql_query("
INSERT INTO MSN
(ID, rsid, msnid, rank)
VALUES('', '$rsid', '$msnid', '$rank')
")
or die(mysql_error());
echo "$rsid has been entered into the database!<br>";

}[/code]

See, all the coding is correct, but everytime on the test page, when I want to enter more than 1 MSN Identity, it only enters the LAST entered one. How can I enter more than one? I've tried arrays, function, thats about all. I don't know what else to do.
Link to comment
https://forums.phpfreaks.com/topic/7261-aghh/
Share on other sites

first off you need to alter the form so that the inputs do not 'overwrite' the previous input name...

[code]<?php

....
$result = mysql_query("SELECT * FROM *")
or die(mysql_error());

for($i = 1; $i <= $number; $i++) {
echo "$i)&nbsp;<br>
Real Name:&nbsp;<input type=\"text\" name=\"rsid[]\"><br>
MSN Identity:&nbsp;<input type=\"text\" name=\"msnid[]\"><br>
Member Rank:<select name=\"rank[]\">";
mysql_connect("*", "*", "*") or die(mysql());
mysql_select_db("*") or die(mysql());


mysql_data_seek($result , 0);
while($row = mysql_fetch_array( $result )) {
echo "<option>$row['rank']</option>";
}
echo "
</select>
<br>";
}

...

?>[/code]

So that creates an array of your inputs automatically...


to teh insert statement....


[code]<?php


if($dbsubmit) {

mysql_connect("*", "*", "*") or die(mysql_error());
mysql_select_db("*") or die(mysql_error());

$no_entered = coount($_POST['rsid']);

$tempstr = NULL:

for ($i = 0; $i < $no_entered; $i++) {
if ($i > 0) {
$temp .= ", ";
}
$temp . = "('' , '$_POST['rsdi'][$i]', '$_POST['msndi'][$i]', '$_POST['rank'][$i]')";
}


$qry = "INSERT INTO MSN
(ID, rsid, msnid, rank)
VALUES " . $qry . ";

$qry = mysql_query($qry)
or die(mysql_error());
echo "CHANGE THIS MESSAGE MAYBE SHOW THE NUMBER OF AFFECTED ROWS!";

}

?>
[/code]

something liek that will do ya.
Link to comment
https://forums.phpfreaks.com/topic/7261-aghh/#findComment-26431
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.