We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

multiple combo box values to be inserted in the database

mattibutt
mattibutt asked
on
Medium Priority
779 Views
Last Modified: 2013-12-12
hi
i want to insert values into the database from multiple combo boxes each combo box contains dynamic value loaded from the database
the problem is i don't know how to post these values in php
the following two items i want to insert in the database
dropdown selected value ( show_id,  time)
i have created the button and database table but i dont know how to write the post statement and link it to button as it needs to insert multiple values
<form name="search" action="schedule1.php" method="post">
         <input name="submit" type="submit" value="Add category" class="inputbutton" />
</form>

<?
 
$res = mysql_query('select id,name from pm_shows');  # order by ?
if(!$res) die('SQL error: '.mysql_error());       # report error, if any
$dropdown = '<option>-- Please Select Show--</option>';
while($row = mysql_fetch_assoc($res))
  $dropdown .= '<option value="'.$row['id'].'">'.
               htmlentities($row['name']).'</option>';
 
			   $starttime = 0 - date('Z');     # date('Z') is to adjust for timezone
			   
			   
$endtime = 8*60*60 - date('Z');
 
$res = mysql_query('select * from pm_shows');  # load schedule
if(!$res) die('SQL error: '.mysql_error());
$schedules = array();
while($sch = mysql_fetch_assoc($res))
  $schedules[$sch['slot']] = $sch['show_id'];  # store in array
 
function showcell($slot,$time) {
  global $dropdown, $schedules;  # show dropdown & schedulex data
  if(isset($schedules[$slot])) {
    $show_id = $schedules[$slot];
    $val = ' value="'.$show_id.'"';
    $thisdropdown = str_replace($val.'>',$val.' selected="selected">',$dropdown); 
  } else 
    $thisdropdown = $dropdown;
  return date('H:i',$time).' '.
         '<select name="slot_'.$slot.'">'.$thisdropdown.'</select>';
}
 
$slot = 1;  # first slot
echo '<table>';
for($i=$starttime;$i<$endtime;$i+=30*60) { 
  echo '<tr>'.
       '<td>'.showcell($slot,$i+(4*60*60)).'</td>'.
       '<td>'.showcell($slot+16,$i+(12*60*60)).'</td>'.
       '<td>'.showcell($slot+32,$i+(20*60*60)).'</td>'.
 
       '</tr>';
  $slot += 1;
     
	
}
echo '</table>';
 
 
 
		
?>

Open in new window

Comment
Watch Question

Commented:
You didn't select or connect to database ...please inform me with your file names .. i  know that you send form to schedule1.php .... is the code attached for schedule1.php?

and sample DB dump to test it on my machine will be easier  to avoid table/DB names differance

Commented:
Hi,

Check out this simple example on passing combo box values from a PHP form to a PHP handler:


<form method="post" action="test.php">
<select multiple="multiple" name="select1[]" size="5">
<option value="1">John</option>
<option value="2">Rick</option>
<option value="3">Kathy</option>
<option value="4">Marie</option>
</select>
<select name="select2">
<option value="1">Monday</option>
<option value="2">Wednesday</option>
<option value="3">Friday</option>
<option value="4">Sunday</option>
</select>
 
<button type="submit" name="submit">Submit!</button>
</form>
 
 
<?PHP
 
// Test if submit button clicked
if(isset($_POST["submit"])) {
 
  // If some values were chosen in select1
  if(isset($_POST["select1"])) {
    
    // For every value
    foreach($_POST["select1"] as $selected) {
      echo "Selected in select1: $selected <br>\n";
    }
  }
 
  // If some value was chosen in select2
  if(isset($_POST["select2"])) {
    echo "Selected in select2: ".$_POST["select2"]."<br>\n";
  }
}
 
?>

Open in new window

Author

Commented:
hi
it is connected with the database using other files the current feature which is working basically combo box are loading values from the table pm_shows


pm show code which are loaded inside the combobox
CREATE TABLE `pm_shows` (
  `id` int(10) NOT NULL auto_increment,
  `Name` varchar(45) character set latin1 collate latin1_bin NOT NULL default '',
  `Description` varchar(1000) collate latin1_german2_ci NOT NULL,
  `Genre` varchar(50) collate latin1_german2_ci NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=37 DEFAULT CHARSET=latin1 COLLATE=latin1_german2_ci AUTO_INCREMENT=37 ;
 
-- 
-- Dumping data for table `pm_shows`
-- 
 
INSERT INTO `pm_shows` VALUES (23, 0x66786668, 'dsshl iyio', 'a');
INSERT INTO `pm_shows` VALUES (24, 0x787863, 'cx', '65');
INSERT INTO `pm_shows` VALUES (25, 0x466173746572, 'its the best', '0');
INSERT INTO `pm_shows` VALUES (26, 0x4b6e6f636b6f7574, 'its the best sport show ever', '0');
INSERT INTO `pm_shows` VALUES (27, 0x4c544b4c, 'many', '0');
INSERT INTO `pm_shows` VALUES (28, 0x64787864, 'dd', '0');

Open in new window

Author

Commented:
what i wanna do is write them back the selected combo box values as individual records into the schedulex table


CREATE TABLE `shedulex` (
  `schedule_id` int(11) NOT NULL auto_increment,
  `slot` tinyint(11) NOT NULL default '0',
  `show_id` int(11) NOT NULL default '0',
  `time` varchar(50) collate latin1_german2_ci NOT NULL,
  PRIMARY KEY  (`schedule_id`)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=latin1 COLLATE=latin1_german2_ci AUTO_INCREMENT=2 ;

Open in new window

Author

Commented:
it is schedule1.php file

Commented:
If this code is your form code ........please confirmthen you can receive the values from slot_1 =slot_48 on schedule1.php using $_POST['slot_1'] => $_POST["slot_48"]and then do what ever you like ... i will prepare a sample schedule1.php that can echo from slot_1 to slot_48 and send it

please confirm the below code is your form code ...
<form name="search" action="schedule1.php" method="post">
         <input name="submit" type="submit" value="Add category" class="inputbutton" />
<?
mysql_connect('localhost','root','root');
mysql_select_db(test);
$res = mysql_query('select id,name from pm_shows');  # order by ?
if(!$res) die('SQL error: '.mysql_error());       # report error, if any
$dropdown = '<option>-- Please Select Show--</option>';
while($row = mysql_fetch_assoc($res))
  $dropdown .= '<option value="'.$row['id'].'">'.
               htmlentities($row['name']).'</option>';
 
                           $starttime = 0 - date('Z');     # date('Z') is to adjust for timezone
 
 
$endtime = 8*60*60 - date('Z');
 
$res = mysql_query('select * from pm_shows');  # load schedule
if(!$res) die('SQL error: '.mysql_error());
$schedules = array();
while($sch = mysql_fetch_assoc($res))
  $schedules[$sch['slot']] = $sch['show_id'];  # store in array
 
function showcell($slot,$time) {
  global $dropdown, $schedules;  # show dropdown & schedulex data
  if(isset($schedules[$slot])) {
    $show_id = $schedules[$slot];
    $val = ' value="'.$show_id.'"';
    $thisdropdown = str_replace($val.'>',$val.' selected="selected">',$dropdown);
  } else
    $thisdropdown = $dropdown;
  return date('H:i',$time).' '.
         '<select name="slot_'.$slot.'">'.$thisdropdown.'</select>';
}
 
$slot = 1;  # first slot
echo '<table>';
for($i=$starttime;$i<$endtime;$i+=30*60) {
  echo '<tr>'.
       '<td>'.showcell($slot,$i+(4*60*60)).'</td>'.
       '<td>'.showcell($slot+16,$i+(12*60*60)).'</td>'.
       '<td>'.showcell($slot+32,$i+(20*60*60)).'</td>'.
 
       '</tr>';
  $slot += 1;
 
 
}
echo '</table>';
?>
</form>

Open in new window

Author

Commented:
yes they are
Commented:
Hi mattibutt,

Here's a working example.

<?
 
function showcell($slot,$time) {
  global $dropdown, $schedules;  # show dropdown & schedulex data
  if(isset($schedules[$slot])) {
    $show_id = $schedules[$slot];
    $val = " value=\"".$show_id."\"";
    $thisdropdown = str_replace($val.">",$val." selected=\"selected\">",$dropdown); 
  } else 
    $thisdropdown = $dropdown;
    
  return date("H:i",$time)." <input type=\"hidden\" name=\"slottime[$slot]\" value=\"$time\" /><select name=\"slot[".$slot."]\">".$thisdropdown."</select>\n";
}
 
define("DEFAULT_SLOT","-- Please select show --");
 
 
if(isset($_POST["submit"])){
	$sql = "DELETE FROM shedulex;";
	mysql_query($sql) or die("DB error");
	foreach($_POST["slot"] as $slot => $value)
		if($value!=DEFAULT_SLOT){
			$value = mysql_real_escape_string($value);
			$slot = mysql_real_escape_string($slot);
			$time = mysql_real_escape_string($_POST["slottime"][$slot]);
			$sql = "INSERT INTO shedulex (slot, show_id, time) VALUES ($slot, $value, $time);";
			mysql_query($sql);
		}
}
 
 
 
$res = mysql_query("select id,name from pm_shows");  # order by ?
if(!$res) die("SQL error: ".mysql_error());       # report error, if any
 
$dropdown = "<option>".DEFAULT_SLOT."</option>\n";
while($row = mysql_fetch_assoc($res))
  $dropdown .= "<option value=\"".$row["id"]."\">".
               htmlentities($row["name"])."</option>\n";
 
$starttime = 0 - date("Z");     # date("Z") is to adjust for timezone                        
$endtime = 8*60*60 - date("Z");
 
$res = mysql_query("select * from shedulex");  # load schedule
if(!$res) die("SQL error: ".mysql_error());
$schedules = array();
while($sch = mysql_fetch_assoc($res))
  $schedules[$sch["slot"]] = $sch["show_id"];  # store in array
 
 
 
$slot = 1;  # first slot
echo "<form method=\"post\" action=\"schedule1.php\">\n";
echo "<table>\n";
for($i=$starttime;$i<$endtime;$i+=30*60) { 
  echo "<tr>\n".
       "<td>\n".showcell($slot,$i+(4*60*60))."</td>\n".
       "<td>\n".showcell($slot+16,$i+(12*60*60))."</td>\n".
       "<td>\n".showcell($slot+32,$i+(20*60*60))."</td>\n".
 
       "</tr>\n";
  $slot += 1;
            
}
echo "</table>\n";
echo "<button type=\"submit\" name=\"submit\">Submit</button>\n";
echo "</form>\n"; 
 
 
                
?>

Open in new window

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
it did do it thanks very much

Author

Commented:
thankyou so much it was very kind of you

Commented:
i attach both form and schedule1 ... rename *.txt to *.php and tell me what you need next ....

in schedule1.php i only echo the results we can replace echo line with insert statment or whatever you like

Commented:
sorry file here

test.zip

Author

Commented:
hi agamal
i still have one more issue which i can ask as a new question the issue is basically i want to create schedule for each date currently it is creating a single instance of schedule if i want to create a unqiue schedule for each date i will ask that as a new  question and post the QUESTION LINK HERE i really appreciate your help unfortunately i couldnt award you points
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.