Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

radio button to insert multiple rows in mysql

Posted on 2009-05-02
2
Medium Priority
?
833 Views
Last Modified: 2013-12-12
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
Comment
Question by:gamebits
2 Comments
 
LVL 39

Accepted Solution

by:
Roger Baklund earned 2000 total points
ID: 24287440
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
 
LVL 28

Author Comment

by:gamebits
ID: 24287869
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

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question