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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.