1

I have a .php page that lets the user select the time-slot of a specialist by displaying a form for each one, but when I select the time the database must get updated with the new values but it's not.

This is the form's code in assigntotimeslot.php, the specialists list gets displayed fine though.

<?php
$qry=" SELECT * FROM Specialist"; //selecting all specialists to display
$result =mysql_query($qry); //create query

//check whether it was successful or not

if($result) {
$did = 0;
Print " <table style='width:100%'>";

while($info=mysql_fetch_array($result))
{
if($did==0){



Print "<form action='assigntotimeslot_php.php?id='".$info['Spec_ID']."' method='POST' enctype='multipart/form-data' style='display:inline;'   

<tr>
     <td><figure style='text-align:centre;'>
  <img STYLE='border: thin solid grey;left:00px;top:00px;'src='img/idimg.jpg' alt='idimage' width='110' height='110'>
  <figcaption>".$info['Spec_Name']."<br/>".$info['Spec_ID']."<br/>".$info['Specialty']. "</br>  From: <select name='cars'>


  <option name='Time_in' value='08:00'>08:00AM</option>
  <option name='Time_in' value='09:00'>09:00AM</option>
  <option name='Time_in' value='10:00'>10:00AM</option>
  <option name='Time_in' value='11:00'>11:00AM</option>
  <option name='Time_in' value='12:00'>12:00AM</option>
    <option name='Time_in' value='02:00'>02:00PM</option>
  <option name='Time_in' value='04:00'>04:00PM</option>
  <option name='Time_in' value='05:00'>05:00PM</option>



</select>


 TO: <select name='cars'>


  <option name='Time_out' value='08:00'>08:00PM</option>
  <option name='Time_out' value='09:00'>09:00PM</option>
  <option name='Time_out' value='10:00'>10:00PM</option>
  <option name='Time_out' value='11:00'>11:00PM</option>
  <option name='Time_out' value='12:00'>12:00PM</option>
    <option name='Time_out' value='02:00'>02:00PM</option>
  <option name='Time_out' value='04:00'>04:00PM</option>
  <option name='Time_out' value='05:00'>05:00PM</option>


<input name='Submit' type='submit' value='Assign' />  </br></br></br></figcaption>
</figure></td> </form>";

$did++;
continue; }



if($did==1){

Print "<form action='assigntotimeslot_php.php?id='".$info['Spec_ID']."' method='POST' enctype='multipart/form-data' style='display:inline;'   

<tr>
     <td><figure style='text-align:centre;'>
  <img STYLE='border: thin solid grey;left:00px;top:00px;'src='img/idimg.jpg' alt='idimage' width='110' height='110'>
  <figcaption>".$info['Spec_Name']."<br/>".$info['Spec_ID']."<br/>".$info['Specialty']. "</br>  From: <select name='cars'>


  <option name='Time_in' value='08:00'>08:00AM</option>
  <option name='Time_in' value='09:00'>09:00AM</option>
  <option name='Time_in' value='10:00'>10:00AM</option>
  <option name='Time_in' value='11:00'>11:00AM</option>
  <option name='Time_in' value='12:00'>12:00AM</option>
    <option name='Time_in' value='02:00'>02:00PM</option>
  <option name='Time_in' value='04:00'>04:00PM</option>
  <option name='Time_in' value='05:00'>05:00PM</option>



</select>


 TO: <select name='cars'>


  <option name='Time_out' value='08:00'>08:00PM</option>
  <option name='Time_out' value='09:00'>09:00PM</option>
  <option name='Time_out' value='10:00'>10:00PM</option>
  <option name='Time_out' value='11:00'>11:00PM</option>
  <option name='Time_out' value='12:00'>12:00PM</option>
    <option name='Time_out' value='02:00'>02:00PM</option>
  <option name='Time_out' value='04:00'>04:00PM</option>
  <option name='Time_out' value='05:00'>05:00PM</option>


<input name='Submit' type='submit' value='Assign' />  </br></br></br></figcaption>
</figure></td></tr> </form>";
$did--;

}


}
Print " </table>";

}


else echo "There are no specialists";

    ?>

And here is the code in assigntotimeslot_php.php

// Start database connection
$Time_in = (isset($_POST['Time_in']) ? $_POST['Time_in'] : null);
$Time_out = (isset($_POST['Time_out']) ? $_POST['Time_in'] : null);

//Create query

$qry="UPDATE Specialist SET Time_in='".$Time_in."',Time_out='".$Time_out."";

$result=mysql_query($qry);
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • I think your HTML syntax isn't correct and thats why. – Class Nov 20 '14 at 00:25
  • the page runs fine but only the part of updating doesn't work , could you please tell me where do you think my html isn't correct – user3419062 Nov 20 '14 at 00:29
  • check this: $Time_out.""; I think you mean $Time_out."'"; – Len_D Nov 20 '14 at 00:31
  • To start, you are missing a closing single quote around `$Time_out` – Crackertastic Nov 20 '14 at 00:31
  • the `name` goes with the `select` tag and you are missing the closing `select` tag. I imagine if you do a `var_dump($_POST)` you'd just get cars. You will also want to add a `WHERE` clause otherwise it will update all records to those values. – Class Nov 20 '14 at 00:31
  • I fixed it and it's like this now: $qry="UPDATE Specialist SET `Time_in`=".$Time_in.", `Time_out` = '".$Time_out."' WHERE `Spec_ID` =" .$Spec_ID); but still my database doesn't get updated :( – user3419062 Nov 20 '14 at 00:38
  • I closed **Select** tag but still, the database doesn't get updated – user3419062 Nov 20 '14 at 00:40
  • the **_php** gives me this **error:** Notice: Undefined index: id in C:\xampp\htdocs\sw\assigntotimeslot_php.php on line 15. **where id is defined as:** $Spec_ID=$_GET['id']; – user3419062 Nov 20 '14 at 00:48
  • $_POST['Time_in'] is null since select name = 'cars'. Use – anhlc Nov 20 '14 at 00:55
  • thank you, i fixed it.. but is there a way so the page _php can see the **id** sent to it, i don't know why it gives me error.. the error i pasted above – user3419062 Nov 20 '14 at 00:59
  • can you please tell me if you think there is an error in **form tag** i think it's the one causing this – user3419062 Nov 20 '14 at 01:08
  • All these sql injection vulnerabilities. I hope you don't use this on an actual site. – Darren Nov 20 '14 at 01:13
  • what do you mean by sql injection vulnerabilities? i really want to make it perfect but don't know why it's not working – user3419062 Nov 20 '14 at 01:14
  • I recommend you have a good read of this http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php – The Mighty Rubber Duck Nov 20 '14 at 01:14
  • the last result i came with, is that when i click on submit an empty page gets displayed with the url **http://localhost/sw/assigntotimeslot_php.php?id=** i don't know why id has no value – user3419062 Nov 20 '14 at 01:37

1 Answers1

0

You have given name to option. You should give name to select tag. Instead of
<select name='cars'> give <select name='Time_in'> and <select name='Time_out'>

Sumit Patil
  • 556
  • 1
  • 5
  • 19