0

I am trying to update the table below timetable, I want to be able to input the day and subject to change via a selection box form and "add" another id (say 103) to ethics on tuesday at 2pm.

With my current code i am selecting the options in the selection form but it when submitted it returns a value of Ethics Ethics Ethics Ethics Ethics (5 times). Like so- 5x Returned Value

Also when i get into the page i get the following errors...

Notice: Undefined index: day in /Applications/XAMPP/xamppfiles/htdocs/assignment/stuTimetable.php on line 304

Notice: Undefined index: subject in /Applications/XAMPP/xamppfiles/htdocs/assignment/stuTimetable.php on line 305

Any help is greatly appreciated.....

PHP

                        $day = $_POST['day'];
                        $subject = $_POST['subject'];

                        $sqlevent = "SELECT '$subject' FROM timetable WHERE       Day = '$day'";
                        $resultevent = mysql_query($sqlevent);
                        $row2 = mysql_fetch_row($resultevent);
                        $resultevent2 = $row2[0];
                        echo $resultevent2;

                        if($resultevent2 = true){ 

                        $sqlevent2 = "UPDATE timetable SET 9am = 'Hello' WHERE Day = '$day'";

                        }



                <form action="<?php $_PHP_SELF ?>" id="showapp" method="POST">
                <input name="submitapp" type="submit" value="Show Appointments">
                </form>
                <br>
                <form action="<?php $_PHP_SELF ?>" id="timeForm" method="POST">
                <h1>Book a slot:</h1>
                <br>
                Day:
                <br>
                  <select name="day" form="timeForm">
                      <option value="">--Select--</option>
                      <option value="Monday">Monday</option>
                      <option value="Tuesday">Tuesday</option>
                      <option value="Wednesday">Wednesday</option>
                      <option value="Thursday">Thursday</option>
                      <option value="Friday">Friday</option>
                  </select>
                <br>
                Subject:
                <br>
                  <select name="subject" form="timeForm">
                      <option value="">--Select--</option>
                      <option value="Meta-Physics">Meta-Physics</option>
                      <option value="Moral Theory">Moral Theory</option>
                      <option value="Ethics">Ethics</option>
                      <option value="Baking">Baking</option>
                      <option value="Appointment">Appointment</option>
                  </select>
                <br>
                Password:
                <br>
                <input type="password" name="password" placeholder="Type Password to Confirm Booking">
                <br>
                <input name="submit" type="submit" value="Submit">
                </form>
TIGER
  • 2,864
  • 5
  • 35
  • 45
  • I appreciate my table is probably not the best designed... – Razold1406 Dec 16 '16 at 11:23
  • add if(isset($_POST['submit'])) – Shanu k k Dec 16 '16 at 11:26
  • 4
    Possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – Qirel Dec 16 '16 at 11:27
  • Same result, i dont get it updating my cell. – Razold1406 Dec 16 '16 at 11:29
  • That should probably be where you start. You should probably have several tables instead of one. I would create a `subject` table containing: `subject_id`, `subject_title`, `subject_desc`, etc... then a `class` table: `class_id`, `class_title`, `class_desc`, `subject_id`. Notice the `subject_id` is also in the `class` table. This is a foreign key back to the `subject` table. After that, a `schedule` table containing: `sched_id`, `class_id`, `start_dtt`, `end_dtt`, etc... – gmiley Dec 16 '16 at 11:30
  • Aside from the redesign suggestion, and more to the point of your question, every time you post this form, you are performing a `SELECT` and an `UPDATE`. If an item does not exist in your table, nowhere are you performing an `INSERT`. You should perform a `SELECT Count(*)...` to determine if the item exists, if it doesn't then perform an `INSERT` instead of an `UPDATE`. Honestly though, and don't take this the wrong way, but it seems like you don't yet have a grasp of how either PHP or SQL fully works individually. I would work a few more tutorials and focus on smaller tasks before combining. – gmiley Dec 16 '16 at 11:32
  • Please don't use `mysql_*` try `mysqli_*` or `PDO` instead. – S.I. Dec 16 '16 at 11:33

2 Answers2

1

You're getting this error because:

$day = $_POST['day'];
$subject = $_POST['subject'];

are executed once you load the page and at the first time these array elements are not defined.

It's only when you submit the form where $_POST is going hold the right values. So you have to check if you're coming from a form submit

if(isset($_POST['sumbit'])) {
// the update code here
}

Note that I've use submit. You usually have to check any required field because if the field is empty any element of the indexed array would use this field's name as and array key.

pekechis
  • 288
  • 6
  • 18
-2

Check first if you are handling a post request using something like

if(isset($_POST['day'])) {
// the update code here
}