2012-05-04 30 views
-3

我正在从当前正在开发的患者系统获取当前的葡萄糖读数。我使用java脚本获取当前的日期/时间,并通过表单隐藏字段过去。在下面的脚本中,我将日期部分存储在3个单独的变量中,然后将它们分组为1,这样我就可以在mysql的插入查询中使用它。我得到的错误是PHP中连接3个变量时出现错误

解析错误:语法错误,意外',' 希望有人可以找到这个错误,因为我不明白我在变量之间放置','我做错了。下面是代码:

<? 
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(); 
?> 
+1

字符串连接使用句点而不是逗号。 http://php.net/manual/en/language.operators.string.php – jasonbar

+2

错误告诉你,在PHP中,你不使用连接。改变点。另外,使用JavaScript日期取决于客户端的时间。 Mysql已经有了获取日期和时间的功能,我会让你搜索它们。 –

+0

@EvanMulawski你如何学习新语言的基本语法?它不会试图在快速和肮脏的10行脚本中使用它吗? – Louis

回答

0
$Date = $Year, "-", $Month, "-", $Day; //line with error 

应该是在PHP

$Date = $Year. "-". $Month. "-". $Day; //<- Full "." and no "," 
+0

谢谢@BabyAzerty。 Nifty,请接受我对这个错误的歉意 – Satya

+0

我并不是在攻击你个人:s,只是通知你:D。我已经删除了我的downvote :)('-1'是其他一些老兄的) –

+0

aah我不担心abt downvote,只是我犯了一个错误,所以道歉 – Satya

1

字符串连接采用.没有,doc

0

你也可以使用sprintf与变量插值字符串:

$Date = sprintf('%s-%s-%s', $Year, $Month, $Day); 
$Time = sprintf('%s:%s', $Hours, $Minutes); 
0

我唯一一次真正使用逃脱串联是使用功能,否则花括号{}是你的朋友!

$Date = "{$Year}-{$Month}-{$Day}";

你不必担心忘记时间或运行到尴尬"'"情况。爱花括号...爱他们!

+0

我知道这是个人喜好,但我从不关心括号。我认为这是因为大多数IDE不会突出显示括号内的变量,因为它们被视为字符串。 –

相关问题