gamebits
asked on
radio button to insert multiple rows in mysql
Follow up of https://www.experts-exchange.com/questions/24374738/Dynamically-generated-group-of-radio-buttons.html
This is more complex than expected (for me anyway)
I have a database with baseball cards, each card has its own id and is part of a set.
As it is a user can select a set and by checking the appropriate checkbox (one for each card) add the selected cards to his own database.
For each card added a new row is created in the table, the information recorded is `own_id`, `card_id`, `set_id`,
`user_id`, `owned`, `wanted`, `trade`
If a user has 5 copies of the same card 5 rows will be created in the table, the reason why I do not have a field and increment the number in that field is because I have another script with a drag and drop capability which allow the user to either move some card to his trade list or simply delete them.
The script work as it is but somebody point out the application would be a lot more user friendly if we could add multiple copy of the same card at once and multiple cards in the same time as well.
Great I thought it would be easy all I needed was 5 columns with radio button with a value of 1 to 5 so the user could insert 1, 2,3,4 or 5 cards in one shot.
An Expert (cxr) provided the code to make the radio button to work as intented, I just realise now that my original script to do the insert is not up to the new challenge.
The 2 scripts are included below I have 2 button on the form one is to insert single card the other one insert 4 copies of the cards selected (required by collectors of Magic the Gathering cards) (there is actually more than just baseball cards in the db)
Refer to the link at the beginning for the code for the radio button provided by cxr.
Any suggestion on how to implement the functionality I'm trying to achieve would be very welcome.
This is more complex than expected (for me anyway)
I have a database with baseball cards, each card has its own id and is part of a set.
As it is a user can select a set and by checking the appropriate checkbox (one for each card) add the selected cards to his own database.
For each card added a new row is created in the table, the information recorded is `own_id`, `card_id`, `set_id`,
`user_id`, `owned`, `wanted`, `trade`
If a user has 5 copies of the same card 5 rows will be created in the table, the reason why I do not have a field and increment the number in that field is because I have another script with a drag and drop capability which allow the user to either move some card to his trade list or simply delete them.
The script work as it is but somebody point out the application would be a lot more user friendly if we could add multiple copy of the same card at once and multiple cards in the same time as well.
Great I thought it would be easy all I needed was 5 columns with radio button with a value of 1 to 5 so the user could insert 1, 2,3,4 or 5 cards in one shot.
An Expert (cxr) provided the code to make the radio button to work as intented, I just realise now that my original script to do the insert is not up to the new challenge.
The 2 scripts are included below I have 2 button on the form one is to insert single card the other one insert 4 copies of the cards selected (required by collectors of Magic the Gathering cards) (there is actually more than just baseball cards in the db)
Refer to the link at the beginning for the code for the radio button provided by cxr.
Any suggestion on how to implement the functionality I'm trying to achieve would be very welcome.
The form
<?php
session_start();
include 'CONNECTION TO DB';
$series_title = $_POST['series_title'];
$company = $_POST['company'];
$year = $_POST['year'];
$series_id = $_POST['series_id'];
$userid = $_POST['userid'];
print "
<body bgcolor=\"#E2EBED\">";
print "<table width=\"100%\">
<tr><td>";
$sql = ("SELECT * FROM Non_Sports WHERE series_id = '$series_id'");
$result = mysql_query($sql);
$num = mysql_num_rows($result);
print "<form action=\"singles_mass_upload_TEST.php\" method=\"POST\" name=\"testform\" id=\"testform\">
<input type=\"hidden\" name=\"series_title\" value=\"$series_title\">
<input type=\"hidden\" name=\"company\" value=\"$company\">
<input type=\"hidden\" name=\"year\" value=\"$year\">
<input type=\"hidden\" name=\"series_id\" value=\"$series_id\">
<input type=\"hidden\" name=\"userid\" value=\"$userid\">
<table border=1 cellpadding=2 cellspacing=0 width=\"400\" bgcolor=\"white\">
<tr><td colspan=2 align=\"left\" bgcolor=\"#317082\" style=\"font-family: Trebuchet MS, Lucida Sans Unicode, Arial, sans-serif; color: #fff\"><input type=\"checkbox\" name=\"chkall\" onclick=\"chkAll('card_id[]',this.checked)\" >Select All</td></tr>";
while($data = mysql_fetch_array($result)){
$string = $data[card_title];
$string = stripslashes(strip_tags($string));
echo "<tr><td width=\"5\"><input type=\"checkbox\" name=\"card_id[]\" value=\"$data[card_id]\"> <td>$string</td></tr>";
}
print "<tr><td colspan=2><input type=\"submit\" name=\"single\" value=\"Single\"> <input type=\"submit\" name=\"playset\" value=\"Play Set\" align=\"right\"></td></tr>
</table>
</form></td>";
?>
AND the REceiving script
<?php
session_start();
include 'CONNECT TO DB';
$userid = $_POST['userid'];
$series_id = $_POST['series_id'];
$company = $_POST['company'];
$year = $_POST['year'];
$card_id = $_POST['card_id'];
if(isset($_POST['single'])){
for ($i=0;$i<count($card_id);$i++) {
$id=$card_id[$i];
$query2 = ("INSERT INTO `My_Cards` (`own_id` , `card_id` , `set_id` , `user_id` , `owned` , `wanted` , `trade`)VALUES (
NULL , '$id', '$series_id', '$userid', '1', '0', '0')");
$result = mysql_query($query2);
echo "Done";
}
}else{
for($t=0;$t<4;$t++){
for ($i=0;$i<count($card_id);$i++) {
$id=$card_id[$i];
$query2 = ("INSERT INTO `My_Cards` (`own_id` , `card_id` , `set_id` , `user_id` , `owned` , `wanted` , `trade`)VALUES (
NULL , '$id', '$series_id', '$userid', '1', '0', '0')");
$result = mysql_query($query2);
}
}
echo "Done";
}
?>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you very much for your help, greatly appreciated.