Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 216
  • Last Modified:

adding several values to the database

hi
i am trying to create a tv schedule i have created a basic prototype with the help of one expert now i am trying to write this to database but i am having problem
each drop down box contain a show name i want to store this into the schedulex table
two things i want to do
1) i want to add a unique schedule for each date
2) adding the showid, slot start time
CREATE TABLE `schedule` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `Date` datetime NOT NULL default '0000-00-00 00:00:00',
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=latin1 COLLATE=latin1_german2_ci AUTO_INCREMENT=2 ;
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 DEFAULT CHARSET=latin1 COLLATE=latin1_german2_ci AUTO_INCREMENT=1 ;


$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 schedule');  # 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

schedule.bmp
0
mattibutt
Asked:
mattibutt
  • 4
  • 2
1 Solution
 
mattibuttAuthor Commented:
hi
i have tried to add the details in the schedulex table but nothing seems to be happening i dont know what i am doing wrong
 <?
include('header.php');
?>
 
<div >
 
</div>
<div id="content">
<h2>Add Schedule</h2>
<script language="JavaScript" src="ts_picker.js">
 
//Script by Denis Gritcyuk: tspicker@yahoo.com
//Submitted to JavaScript Kit (http://javascriptkit.com)
//Visit http://javascriptkit.com for this script
 
</script>
 
<form name="tstest">
<input type="Text" name="timestamp" value="">
<a href="javascript:show_calendar('document.tstest.timestamp', document.tstest.timestamp.value);"><img src="cal.gif" width="16" height="16" border="0" alt="Click Here to Pick up the timestamp"></a>
	
  
</form>
<form name="search" action="schedule1.php" method="post">
   	<input name="submit" type="submit" value="Add category" class="inputbutton" />
</form>
<br />
<?=$info_err_msg?> 
<?=$info_msg?>
<div class="info_msg_err">
  <strong></strong> 
</div>
<table width="100%" border="0" cellspacing="1" cellpadding="3">
  <tr>
    <td valign="top"></tr>
</table>
 
<p>
  <?
 
$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>';
if(!empty($_POST['submit']) && $_POST['submit'] == 'Add category') {
 
	$show_id = $_POST['show_id'];
	$dropdown = $_POST['slot'];
	$show_id = addslashes(trim($_POST['show_id']));
	$dropdown = addslashes(trim($_POST['slot']));
	
	if(empty($show_id) || empty($dropdown)) 
		$info_msg = '<div class="info_msg_err">Please don\'t leave any empty fields.</div>';	
	else {	
		$query = mysql_query("SELECT name FROM pm_shows where name = '".$tag."'");
		$result = mysql_num_rows($query);
		
		if($result == 0) {
		
			if(eregi($pattern, $tag) === false) {
			//if(!ctype_alnum($tag)) {
			//	$info_err_msg = '<div class="info_msg_err">Please make sure that the tags are typed properly (alfa-numeric only).</div>';
			$info_err_msg = '<div class="info_msg_err">Please make sure that the Shows are typed properly (use only letters, numbers, "_" and "-").</div>';
			} else {
				mysql_query("INSERT INTO schedulex SET slot  = '".$show_id."', show_id  = '".$dropdown."',  time  = '".$starttime."'");
				$info_msg = '<div class="info_msg_ok">Show <b>'.$tag.'</b> has been added to the database.</div>';
			}
		
		} else {
				$info_msg = '<div class="info_msg_err">The \'Show\' must be unique. A Show  was found in your database.</div>';
 
		}
	}	
}
 
 
		
?>
</p>
<p>&nbsp; </p>
<form>
  <input name="submit" type="submit" value="Add" class="inputbutton" />
</form>
<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<form>
<input name="submit" type="submit" value="Update" class="inputbutton" />
</form>
<?
include('footer.php');
?>

Open in new window

0
 
Steve BinkCommented:
The code you posted does not show any attempt at inserting back into the database.  It does look like you have everything in place to get the correct information, though, so the rest should just be grabbing it from $_POST and dropping it into an INSERT statement.  Where, exactly, do you need help?  The relevant code would be most helpful.
0
 
mattibuttAuthor Commented:
hi
i want to grab each drop down box show id and start time if i could get help to insert these two items then i think i will able to do the rest.
writting both of these variables back to the database
$slot,$time
i have tried it like this but as you mentioned its not correct, what do i do?
mysql_query("INSERT INTO schedulex SET slot  = '".$show_id."', show_id  = '".$dropdown."',  time  = '".$starttime."'");
                                $info_msg = '<div class="info_msg_ok">Show <b>'.$tag.'</b> has been added to the database.</div>';
                        }

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>';
}

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
mattibuttAuthor Commented:
thanks buddy i got help on this question i think i have asked this question wrongly thats why it confused you
0
 
Steve BinkCommented:
1) how are you setting $show_id, etc?  Your example implies register_globals is on, which is very much not recommended.  You should be pulling your values from $_POST

2) if you examine the code you are using to render the form controls, you are not naming them show_id, etc.  You are naming them "slot_#", where (I assume) # is the time slot of that particular control.  Your insert code needs to reflect this

The best approach for you is to parse the data coming in from $_POST and organize it into your own array.  Then use that array to build a multi-row INSERT statement.

You already closed this question...do you still need help with it?  Use the "Request Attention" link to re-open it.
0
 
mattibuttAuthor Commented:
no thanks buddy i got help on this issue but thank you for your input
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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