我正在为一个医学网站创建日历。日历的功能是,如果我单击它的任何日期,如2013-05-01,它会将该日期添加到数据库中,并导航到下一页,在那里我必须选择上午9点到下午6点之间的时间。例如,如果我选择上午9点之类的任何时间,则a代码将检查数据库中是否已存在日期为“2013-05-01”和时间为“09:00:00”的条目,如果该条目已存在,则会重定向到时间页,并显示一条消息,说明该时间已安排,然后我会单击另一个时间,如“10:00:00”,这样它会再次检查并假设数据库中没有日期为“2013-05-01”和时间为“10:00:00”的条目,然后它会添加此条目。。。。
我成功地得到了这样一个观点:如果数据库中已经有一个条目,它会通知我“这个时间已经安排好了”,然后我单击时间“10:00:00”,但它显示我的sql代码中有一些错误,但我已经检查了在phpmyadmin中运行的代码,它工作正常……我不知道有什么问题。。。。
这是我得到的错误:
qry_check update失败:SQL语法有错误;请检查
对应于右侧MySQL服务器版本的手册
在“2013-05-01”附近使用的语法,在第1行使用time=“10:00:00”
下面是日历页“re_sch_time.php”的代码
<?php
if(isset($_GET['result']))
{
$result=$_GET['result'];
if($result=='false')
{
echo "<p>Sorry! This time is already selected for appointment</p>";
}
else
{
echo "<p>Appointment Reschedualed</p>";
}
}
?>
<?php
if(isset($_GET['id']))
{
$id=$_GET['id'];
}
if(isset($_GET['date']))
{
$dat=$_GET['date'];
}
?>
<a href="re_sch_time_selected.php?time=nine&&id=<?php echo $id; ?>&&date=<?php echo $dat ?>">9</a>
<a href="re_sch_time_selected.php?time=ten&&id=<?php echo $id; ?>&&date=<?php echo $dat ?>">10</a>
下面是页面代码,我们可以在其中选择time're_sch_time_selected.php'
<?php
if(isset($_GET['time']))
{
$time=$_GET['time'];
if(isset($_GET['id']))
{
$id=$_GET['id'];
}
if(isset($_GET['date']))
{
$dat=$_GET['date'];
}
switch($time)
{
case "nine":
$qry_check=mysql_query("SELECT * FROM patient WHERE date='".$dat."' AND time='09:00:00'");
if(!$qry_check)
{
die("qry_check update failed ".mysql_error());
}
else
{
while($time_check=mysql_fetch_array($qry_check))
{
echo $time_check['time'];
$pos=strpos($time_check['time'],'09:00:00');
if($pos===false)
{
$qry_time=mysql_query("UPDATE patient SET time='09:00:00' WHERE id='".$id."'");
if(!$qry_time)
{
die("qry time update failed:".mysql_error());
}
else{
header("Location: re_sch_time.php?result=true&id='".$id."'&date='".$dat."'");
}
}
else{
header("Location: re_sch_time.php?result=false&id='".$id."'&date='".$dat."'");
}
}// while time check ends here
}
break;
case "ten":
$qry_check=mysql_query("SELECT * FROM patient WHERE date='".$dat."' AND time='10:00:00'");
if(!$qry_check)
{
die("qry_check update failed: ".mysql_error());
}
else
{
while($time_check=mysql_fetch_array($qry_check))
{
echo $time_check['time'];
$pos=strpos($time_check['time'],'10:00:00');
if($pos===false){
$qry_time=mysql_query("UPDATE patient SET time='10:00:00' WHERE id='".$id."'");
if(!$qry_time){
die("qry time update failed:".mysql_error());
}else{
header("Location: re_sch_time.php?result=true&id='".$id."'&date='".$dat."'");
}
}
else{
header("Location: re_sch_time.php?result=false&id='".$id."'&date='".$dat."'");
}
}// while time check ends here
}
break;
}
注:
当我选择的日期和时间不在数据库中时,此代码运行得非常好,例如,如果没有日期为“2013-05-01”和时间为“09:00:00”的条目,则不会出现任何错误,条目将插入表中。。。。
最佳答案
你在某个地方弄乱了撇号,因为在错误信息中很清楚。仔细看,每个日期后都有一个双撇号。
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '2013-05-01'' AND time='10:00:00''
关于php - 代码正确时在mysql中出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16364285/