I am getting the current glucose readings from a patient system that i am developing. I used java script to get the current date/time and past it through form hidden fields. In the script below i have stored the date parts in 3 separate variables and then i concatinate them into 1, so that i can use that in the insert query for mysql. The error i am getting is
Parse error: syntax error, unexpected ',' Hope someone can find the mistake, as i do not understand what i'm doing wrong by putting ',' between variables. Here is the code:
<?
SESSION_START();
include("DatabaseConnection.php");
//gather form data into variables
//gather parts of the date from hidden input fields
$Day = $_POST['Day'];
$Month = $_POST['Month'];
$Year = $_POST['Year'];
$Date = $Year, "-", $Month, "-", $Day; //line with error
//get hours and minutes from hidden fields
$Minutes = $_POST['Minutes'];
$Hours = $_POST['Hours'];
//concatinate date into 1 variable
$Time = $Hours, ":", $Minutes;
$GlucoseLevel = $_POST['GlucoseLevel'];
$SBP = $_POST['SBP'];
$DBP = $_POST['DBP'];
$Comments = $_POST['Comments'];
//store current user's id
$User_id = $_SESSION['User_id'];
//query for inserting reading
$ReadingInsert = "insert into reading
(Reading_id,
User_id,
Date,
Time,
GlucoseLevel,
SBP,
DBP,
Comments)
values(null,
'$User_id',
'$Date',
'$Time',
'$GlucoseLevel',
'$SBP',
'$DBP',
'$Comments')";
//run insert query
mysql_query($ReadingInsert) or die("Cannot insert reading");
`enter code here`mysql_close();
?>
$Date = $Year, "-", $Month, "-", $Day; //line with error
should be
$Date = $Year. "-". $Month. "-". $Day; //<- Full "." and no ","
String concatenation in php uses .
not ,
doc
In PHP you use .
to concatenate strings, try:
$Date = $Year . "-" . $Month "-" . $Day;
see:
You could also use sprintf to interpolate strings with variables:
$Date = sprintf('%s-%s-%s', $Year, $Month, $Day);
$Time = sprintf('%s:%s', $Hours, $Minutes);
The only time I really use escaped concatenation is to use a function, else curly brackets {}
are your friend!
$Date = "{$Year}-{$Month}-{$Day}";
You don't have to worry about forgetting periods or running into awkward "'"
situations. Love curly brackets... LOVE THEM!