• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1000
  • Last Modified:

radio button to insert multiple rows in mysql

Follow up of http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/Q_24374738.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.
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";
        }
    ?>

Open in new window

0
gamebits
Asked:
gamebits
1 Solution
 
Roger BaklundCommented:
I think the problem is with your for() loop. Use a foreach loop instead. The $count contains the number of copies (1-5), I assume this goes into the 'owned' column. Use the radio group from the other question instead of the checkbox, and this for 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'])){
 
  foreach($card_id as $id=>$count) {
    $query2 = "INSERT INTO `My_Cards` (`own_id`, `card_id`, `set_id`, `user_id`, `owned`, `wanted`, `trade`)
       VALUES (NULL, '$id', '$series_id', '$userid', $count, '0', '0')";
    $result = mysql_query($query2);
    echo "Done";
  }
}else{
  for($t=0;$t<4;$t++){
    foreach($card_id as $id=>$count) {
      $query2 = "INSERT INTO `My_Cards` (`own_id`, `card_id`, `set_id`, `user_id`, `owned`, `wanted`, `trade`)
         VALUES (NULL, '$id', '$series_id', '$userid', $count, '0', '0')";
      $result = mysql_query($query2);
    }
  }
  echo "Done";
}
?>

Open in new window

0
 
gamebitsAuthor Commented:
hmm! this would work although now my drag and drop won't (was based on one entry per card) on the bright side I will saved a lot of space in the db.

Thank you very much for your help, greatly appreciated.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now