Jump to content


Photo

Aghh!!!


  • Please log in to reply
3 replies to this topic

#1 Eugene

Eugene
  • Members
  • PipPipPip
  • Advanced Member
  • 126 posts

Posted 13 April 2006 - 12:13 AM

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

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>";
}


That works fine and so does this:

$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>";

}

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.

#2 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 13 April 2006 - 01:00 AM

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

<?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>";
}

...

?>

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


to teh insert statement....


<?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!";

}

?>

something liek that will do ya.
follow me on twitter @PHPsycho

#3 Eugene

Eugene
  • Members
  • PipPipPip
  • Advanced Member
  • 126 posts

Posted 13 April 2006 - 03:12 PM

A bunch of parse errors appear after I use that...

#4 Eugene

Eugene
  • Members
  • PipPipPip
  • Advanced Member
  • 126 posts

Posted 13 April 2006 - 04:13 PM

[a href=\"http://imageshack.us\" target=\"_blank\"][img src=\"http://img464.imageshack.us/img464/6687/help2jn.png\" border=\"0\" alt=\"IPB Image\" /][/a]

That what appears in the database if I use Arrays. Can anyone help please!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users