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

multiple combo box values to be inserted in the database

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

0
mattibutt
Asked:
mattibutt
  • 8
  • 4
  • 2
1 Solution
 
agamalCommented:
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
0
 
absxCommented:
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

0
 
mattibuttAuthor 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

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
mattibuttAuthor 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

0
 
mattibuttAuthor Commented:
it is schedule1.php file

0
 
agamalCommented:
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

0
 
mattibuttAuthor Commented:
yes they are
0
 
absxCommented:
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

0
 
mattibuttAuthor Commented:
it did do it thanks very much
0
 
mattibuttAuthor Commented:
thankyou so much it was very kind of you
0
 
agamalCommented:
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
0
 
agamalCommented:
sorry file here

test.zip
0
 
mattibuttAuthor 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
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 8
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now