0

Code updated to reflect double quote vs single quote.

Instead of populating with the date_requested value from the mysql table, I am getting the following.

Date  <input type="text" id="datepicker" name="date_requested" value="$date_requested"> <br/><br/>

This line is placing $date_requested in the text box.

I have also tried

Date  <input type="text" id="datepicker" name="date_requested" value="<?php echo "$date_requested" ?>"/> <br/><br/>

This line places php echo in the text box.

Any direction is appreciated. Here is the surrounding section of code:

if($_SERVER['REQUEST_METHOD'] == "POST")
{
    mysql_connect("localhost", "root","") or die(mysql_error()); //Connect to server
    mysql_select_db("first_db") or die("Cannot connect to database"); //Connect to database
    $query = mysql_query("Select * from time_off_requests Where time_off_key='$time_off_key'"); // SQL Query
    $date_requested = $_POST["date_requested"];
    $time_off_begin = $_POST["time_off_begin"];
    $time_off_end = $_POST["time_off_end"];//date
    $use_pto = $_POST["use_pto"];
    $user = $_SESSION["user"];
    $time_off_key = $_SESSION["time_off_key"];
    mysql_query("UPDATE time_off_requests SET date_requested='$date_requested', time_off_begin='$time_off_begin', time_off_end='$time_off_end', use_pto='$use_pto' WHERE time_off_key='$time_off_key'") ;
    header("location: home.php");
}
if($tok_exists)
    {
    Print '
    <form action="edit.php" method="POST">
        Date  <input type="text" id="datepicker" name="date_requested" value="<?php echo "$date_requested" ?>"/> <br/><br/>
        All Day  <input type="checkbox" name="all_day[]" value="no" /> <br/>
        Start Time  <input type="time"  id="timepicker" name="time_off_begin" /> <br/>
        End Time  <input type="time" id="timepicker2" name="time_off_end" /> <br/> <br/>
        Use PTO <Select name="use_pto">
        <option value="">Select</option>
        <option value="Yes">Yes</option>
        <option value="No">No</option>
        </select> <br/><br/>
       <input type ="submit" value="Edit Request">  
    </form>
    ';
    }
    else
    {
        Print '<h2 align="center">There is no data to be edited.</h2>';
    }
?>
  • `$tok_exists` where is that assigned? – Funk Forty Niner Dec 12 '18 at 02:04
  • Higher in the code, it is showing as true for the if statement and generating the form. The form is also updating the table as desired just not showing the existing information in the form box. – Chris Przybylski Dec 12 '18 at 02:05
  • Need to review strings in single vs double quotes - http://php.net/manual/en/language.types.string.php - specifically variable parsing. As your variable (`$date_requested`) is inside single quotes -> `Print ' ...`, it will never parse. – Sean Dec 12 '18 at 03:00
  • Thanks @Sean, I was not aware of the difference between the single and double quotes. I have adjusted it accordingly but the returns remain the same. I updated the question to better show the return issue. – Chris Przybylski Dec 12 '18 at 04:53
  • If you have `Print ' ...`, then change `... value=""...` to `...value=" '.$date_requested.' "...`. Basic string concatenation - `'.$date_requested.'` – Sean Dec 12 '18 at 05:02
  • Thank you, I think this revealed the problem. Produced the following error: Undefined variable: date_requested in /Applications/XAMPP/xamppfiles/htdocs/timeclock/edit.php on line 102. I assume this means there is an issue with the query that I will need to investigate further. – Chris Przybylski Dec 12 '18 at 05:31

1 Answers1

0

You are mixing single and double quotes wrong. An easier way to set the values into the string from the variables is using variable interpolation, which requires double quotes ("").

So

Print "<form action='edit.php' method='POST'>
    Date  <input type='text' id='datepicker' name='date_requested' value='$date_requested'/> <br/><br/>
</form>";

would indeed output

<form action='edit.php' method='POST'>
    Date  <input type='text' id='datepicker' name='date_requested' value='2018-12-12'/> <br/><br/>
</form>

This is because PHP sees the variable $date_requested inside a normal string enclosed within double quotes and replaces it with its value. The single quotes around the variable are interpreted literally and outputted as the quotes enclosing the HTML attribute's value

gbmcarlos
  • 81
  • 2
  • 3