Current location: Hot Scripts Forums » Programming Languages » PHP » mysql and checkboxex


mysql and checkboxex

Reply
  #1 (permalink)  
Old 08-25-10, 08:56 PM
williamh26 williamh26 is offline
Wannabe Coder
 
Join Date: Jul 2010
Posts: 132
Thanks: 2
Thanked 0 Times in 0 Posts
mysql and checkboxex

What is wrong with this code PLEASE HELP ME
PHP Code:

<?php
$con 
mysql_connect("localhost","checkbox","checkbox");
if (!
$con)
{
die(
'Could not connect: ' mysql_error());



$test=$_POST['team'];
foreach (
$team as $hour)
{
    
$sql="INSERT INTO team (myfield) VALUES ('$hour')";
    
mysql_query($sql) or die(mysql_error());
}

}
?>
<body>
<form method="post" action="team.php">
<table border="1">
<tr><td><input type="checkbox" name="tutorhours[]" value="hour1"></td><td>9:00 am-9:30 am</td></tr> <br />
<tr><td><input type="checkbox" name="tutorhours[]" value="hour2"></td><td>9:30 am-10:30 am</td></tr> <br />
<tr><td><input type="checkbox" name="tutorhours[]" value="hour3"></td><td>10:30 am-11:00 am</td></tr> <br />
<tr><td><input type="checkbox" name="tutorhours[]" value="hour4"></td><td>11:00 am - 12:00 pm</td></tr> <br />
<tr><td><input type="checkbox" name="tutorhours[]" value="hour5"></td><td>12:00 pm - 12:30 pm</td></tr> <br />
<tr><td><input type="checkbox" name="tutorhours[]" value="hour6"></td><td>12:30 pm -  1:00 pm</td></tr> <br />
<tr><td><input type="checkbox" name="tutorhours[]" value="hour7"></td><td>1:00 pm -  1:30 pm</td></tr> <br />
<tr><td><input type="checkbox" name="tutorhours[]" value="hour8"></td><td>1:30 pm -  2:00 pm</td></tr> <br />
<tr><td><input type="checkbox" name="tutorhours[]" value="hour9"></td><td>2:00 pm -  2:30 pm</td></tr>  <br />
<tr><td><input type="checkbox" name="tutorhours[]" value="hour10"></td><td>2:30 pm -  3:00 pm</td></tr>  <br />
<tr><td><input type="checkbox" name="tutorhours[]" value="hour11"></td><td>3:00 pm -  3:30 pm</td></tr>  <br />
<tr><td><input type="checkbox" name="tutorhours[]" value="hour12"></td><td>3:30 pm -  4:00 pm</td></tr>  <br />
<tr><td><input type="checkbox" name="tutorhours[]" value="hour13"></td><td>4:00 pm -  4:30 pm</td></tr>  <br />
<tr><td><input type="checkbox" name="tutorhours[]" value="hour14"></td><td>4:30 pm -  5:00 pm</td></tr>  <br />
<tr><td><input type="checkbox" name="tutorhours[]" value="hour15"></td><td>5:00 pm -  5:30 pm</td></tr>  <br />
<tr><td><input type="checkbox" name="tutorhours[]" value="hour16"></td><td>5:30 pm -  6:00 pm</td></tr>  <br />
<tr><td><input type="checkbox" name="tutorhours[]" value="hour17"></td><td>6:00 pm -  6:30 pm</td></tr>  <br />
<tr><td><input type="checkbox" name="tutorhours[]" value="hour18"></td><td>6:30 pm -  7:00 pm</td></tr>  <br />
<tr><td><input type="checkbox" name="tutorhours[]" value="hour19"></td><td>7:00 pm -  7:30 pm</td></tr>  <br />
<tr><td><input type="checkbox" name="tutorhours[]" value="hour20"></td><td>7:30 pm -  8:00 pm</td></tr>  <br />

</table>
<input type="submit" name="submit" value="submit">
</form>
</body>
 </html>

Last edited by job0107; 08-25-10 at 11:49 PM.
Reply With Quote
  #2 (permalink)  
Old 08-25-10, 11:47 PM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
Quote:
Originally Posted by williamh26 View Post
What is wrong with this code PLEASE HELP ME
PHP Code:

<?php
$con 
mysql_connect("localhost","checkbox","checkbox");
if (!
$con)
{
die(
'Could not connect: ' mysql_error());



$test=$_POST['team'];
foreach (
$team as $hour)
{
    
$sql="INSERT INTO team (myfield) VALUES ('$hour')";
    
mysql_query($sql) or die(mysql_error());
}

}
?>
<body>
<form method="post" action="team.php">
<table border="1">
<tr><td><input type="checkbox" name="tutorhours[]" value="hour1"></td><td>9:00 am-9:30 am</td></tr> <br />
<tr><td><input type="checkbox" name="tutorhours[]" value="hour2"></td><td>9:30 am-10:30 am</td></tr> <br />
<tr><td><input type="checkbox" name="tutorhours[]" value="hour3"></td><td>10:30 am-11:00 am</td></tr> <br />
<tr><td><input type="checkbox" name="tutorhours[]" value="hour4"></td><td>11:00 am - 12:00 pm</td></tr> <br />
<tr><td><input type="checkbox" name="tutorhours[]" value="hour5"></td><td>12:00 pm - 12:30 pm</td></tr> <br />
<tr><td><input type="checkbox" name="tutorhours[]" value="hour6"></td><td>12:30 pm -  1:00 pm</td></tr> <br />
<tr><td><input type="checkbox" name="tutorhours[]" value="hour7"></td><td>1:00 pm -  1:30 pm</td></tr> <br />
<tr><td><input type="checkbox" name="tutorhours[]" value="hour8"></td><td>1:30 pm -  2:00 pm</td></tr> <br />
<tr><td><input type="checkbox" name="tutorhours[]" value="hour9"></td><td>2:00 pm -  2:30 pm</td></tr>  <br />
<tr><td><input type="checkbox" name="tutorhours[]" value="hour10"></td><td>2:30 pm -  3:00 pm</td></tr>  <br />
<tr><td><input type="checkbox" name="tutorhours[]" value="hour11"></td><td>3:00 pm -  3:30 pm</td></tr>  <br />
<tr><td><input type="checkbox" name="tutorhours[]" value="hour12"></td><td>3:30 pm -  4:00 pm</td></tr>  <br />
<tr><td><input type="checkbox" name="tutorhours[]" value="hour13"></td><td>4:00 pm -  4:30 pm</td></tr>  <br />
<tr><td><input type="checkbox" name="tutorhours[]" value="hour14"></td><td>4:30 pm -  5:00 pm</td></tr>  <br />
<tr><td><input type="checkbox" name="tutorhours[]" value="hour15"></td><td>5:00 pm -  5:30 pm</td></tr>  <br />
<tr><td><input type="checkbox" name="tutorhours[]" value="hour16"></td><td>5:30 pm -  6:00 pm</td></tr>  <br />
<tr><td><input type="checkbox" name="tutorhours[]" value="hour17"></td><td>6:00 pm -  6:30 pm</td></tr>  <br />
<tr><td><input type="checkbox" name="tutorhours[]" value="hour18"></td><td>6:30 pm -  7:00 pm</td></tr>  <br />
<tr><td><input type="checkbox" name="tutorhours[]" value="hour19"></td><td>7:00 pm -  7:30 pm</td></tr>  <br />
<tr><td><input type="checkbox" name="tutorhours[]" value="hour20"></td><td>7:30 pm -  8:00 pm</td></tr>  <br />

</table>
<input type="submit" name="submit" value="submit">
</form>
</body>
 </html>
Your code doesn't make any sense.
First, there is no input element named "team", so $_POST["team"] doesn't exist.
Next, there is no variable named $team which you are using in your foreach loop.
Also, there is no point to your INSERT query because $hour doesn't have any meaning by itself.
And next, there is no relevance to your check boxes because they are not related to anything (team, player, etc...) and you are not collecting their values when the form is submitted (there is no $_POST["tutorhours"] variable), so you are not doing anything with them.
And last, your logic appears to be flawed.
__________________
Jerry Broughton

Last edited by job0107; 08-25-10 at 11:59 PM.
Reply With Quote
  #3 (permalink)  
Old 08-25-10, 11:53 PM
williamh26 williamh26 is offline
Wannabe Coder
 
Join Date: Jul 2010
Posts: 132
Thanks: 2
Thanked 0 Times in 0 Posts
thank you, can help me to do it please
Reply With Quote
  #4 (permalink)  
Old 08-26-10, 12:03 PM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
What are you trying to do?
__________________
Jerry Broughton
Reply With Quote
  #5 (permalink)  
Old 08-26-10, 10:22 PM
williamh26 williamh26 is offline
Wannabe Coder
 
Join Date: Jul 2010
Posts: 132
Thanks: 2
Thanked 0 Times in 0 Posts
I am trying to populate from db the following"

_Start Time__ EndTime___ Book_____
9:00 am 9:30 am X --> check box
9:30 am 10:00am X --> check box


This is the table is tutorschedule
fields:
tutorscheduleid int increment
tutorid
starttime
endtime
book

So Populate the schedule of every tutor during the day. Then the customer can click the time available and to the db as an appointment. Then if someone else look for the same tutor it will show the checked and unchecked times (the checked times will be disabled so means it was already booked for someone else)
Reply With Quote
  #6 (permalink)  
Old 08-26-10, 10:47 PM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
The tutorscheduleid should not be an auto-increment field.
It should be used to indicate a month of the year using values 1 through 12.
And then there would be an entry for each time slot for each tutorid for each month (tutorscheduleid).
The book field, starttime field and endtime field would not be needed.
Instead you would need only one field like scheduletime.

Example:
tutorscheduleid | tutorid | scheduletime
1 | 1 | hour1
1 | 1 | hour5
1 | 1 | hour9
1 | 1 | hour14
1 | 2 | hour1
1 | 2 | hour2
1 | 2 | hour3
1 | 2 | hour4

The following data would indicate that tutor 1 would have
hour1, hour5, hour9 and hour14 booked for Janurary.
And tutor 2 would have
hour1, hour2, hour3 and hour4 booked for Janurary.

After you have the table setup like this,
then you can query the table and dynamically create a form for each tutor using the values you already have in the form you showed earlier.

You will need two pieces of code.
One that will create the forms using the data in the database to show which times are booked and which are available.
And another that will update and/or add booked times to the database.

Another method would be to create a table with a field for each possible
scheduletime (20 of them) and then you would only need one record for each tutor for each month.
This type of table, though it would have more fields (22 total fields), would be easier to program for.

That table might look like this:
tutorscheduleid | tutorid | h1 | h2 | h3 | h4 | h5 | h6 | h7 | h8 | h9 | h10 | h11 | h12 | h13 | h14 | h15 | h16 | h17 | h18 | h19 | h20 |
1 | 1 | 1 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 |
1 | 2 | 1 | 1 | 1 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |

The following data would indicate that tutor 1 would have
hour1, hour5, hour9 and hour14 booked for Janurary.
And tutor 2 would have
hour1, hour2, hour3 and hour4 booked for Janurary.

Before you begin to write the program you will need to choose a schema for your table.
I personally would choose the second method as it would be easier to create a program for it.
And you already have most of the basic form created that you could use as a guide.

It would also be helpful if you had another table that would contain the tutorName and tutorid.
It could be used to create an HTML select box that would be used to lookup a tutor's schedule.
Also you could have another HTML select box that would contain all the possible months that the tutors may be available.
It could then be used in conjunction with the tutors Name to lookup a schedule for a particular month.

__________________
Jerry Broughton

Last edited by job0107; 08-26-10 at 11:35 PM.
Reply With Quote
  #7 (permalink)  
Old 08-26-10, 11:58 PM
williamh26 williamh26 is offline
Wannabe Coder
 
Join Date: Jul 2010
Posts: 132
Thanks: 2
Thanked 0 Times in 0 Posts
Thank you for ur advise that what i have right now

<?php
$error = "Could not connect to the database";
mysql_connect('localhost','root','') or die($error);
mysql_select_db('learningcenter') or die($error);


$query = "SELECT scheduletime from tutorschedule where tutorid=$tutorid";
$result = mysql_query($query);
$row=mysql_fetch_assoc($result);
$scheduletime = $row['scheduletime'];

echo"<table border='1'>";
echo"<tr>";
echo"<td>$scheduletime</td>";
echo"</tr>";
echo"</table>";
?>

that show me the time of each tutor, my next question will be how set up the code in order to show available and unavailble time?? do i need another field??

Again thank you

I was thinking if i have another two fields like Open and Close type TINYINT as a link

echo"<table border='1'>";
echo"<tr><td>Schedule</td><td>Close</td><td>Open</td></tr>";
echo"<tr>";
echo"<td>$scheduletime</td>";
echo"</tr>";
echo"</table>";

wHAT U THINK??


<?php
$error = "Could not connect to the database";
mysql_connect('localhost','root','') or die($error);
mysql_select_db('learningcenter') or die($error);


$query = "SELECT scheduletime, open , close from tutorschedule where tutorid=$tutorid";
$result = mysql_query($query);
$row=mysql_fetch_assoc($result);
$scheduletime = $row['scheduletime'];
$open = $row['open'];
$close =$rwo['close'];

echo"<table border='1'>";
echo"<tr><td>Schedule</td><td>Open</td><td>Close</td></tr>";
echo"<tr>";
echo"<td>$scheduletime</td><td>$open</td><td>$close</td>";
echo"</tr>";
echo"</table>";
?>

Last edited by williamh26; 08-27-10 at 12:13 AM.
Reply With Quote
  #8 (permalink)  
Old 08-27-10, 12:23 AM
williamh26 williamh26 is offline
Wannabe Coder
 
Join Date: Jul 2010
Posts: 132
Thanks: 2
Thanked 0 Times in 0 Posts
I have this waht u think??
<?php
$error = "Could not connect to the database";
mysql_connect('localhost','root','') or die($error);
mysql_select_db('learningcenter') or die($error);


$query = "SELECT scheduletime, open , close from tutorschedule where tutorid=$tutorid";
$result = mysql_query($query);
$row=mysql_fetch_assoc($result);
$scheduletime = $row['scheduletime'];
$open = $row['open'];
$close =$row['close'];

echo"<table border='1'>";
echo"<tr><td>Schedule</td><td>Open</td><td>Close</td></tr>";
echo"<tr>";

echo"<td>$scheduletime</td><td><a href='index.php?content=timeopen&start=$open'>Open </a></td><td><a href='index.php?content=timeclose&start=$close'>Cl ose</a></td>";
echo"</tr>";
echo"</table>";
?>
Reply With Quote
  #9 (permalink)  
Old 08-27-10, 01:01 AM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
You didn't read what I said very carefully.
You need to think about it some more.
I could write it all for you, but that wouldn't help you to learn.
And if you aren't really interested in learning then I would be more than happy to write code for you if you are willing to pay $25.00 an hour for my time.
$100.00 minimum.
__________________
Jerry Broughton
Reply With Quote
  #10 (permalink)  
Old 08-27-10, 08:31 AM
williamh26 williamh26 is offline
Wannabe Coder
 
Join Date: Jul 2010
Posts: 132
Thanks: 2
Thanked 0 Times in 0 Posts
That's is what i asked you, cuz i wanna learn!!!!!! i just learning!!!!!! and in order to learn i have to ask questions... right???
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump


All times are GMT -5. The time now is 08:19 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.