什么是未定义的常量错误php?

时间:2014-03-10 14:46:41

标签: javascript php mysql mysqli syntax-error

我正在尝试使用php在mysql数据库中插入Data。我首先使用ajax将值传递给php,但是php会抛出以下错误,不确定什么是错误的?

Use of undefined constant bus - assumed 'bus' in C:\wamp\www\Abbotsford Bus Transit App - Calling Files From Server\setUserPost.php on line 28

Use of undefined constant direction - assumed 'direction' in C:\wamp\www\Abbotsford Bus Transit App - Calling Files From Server\setUserPost.php on line 28

Use of undefined constant stopname - assumed 'stopname' in C:\wamp\www\Abbotsford Bus Transit App - Calling Files From Server\setUserPost.php on line 28

  <etc>

mysqli_query(): Empty query in C:\wamp\www\Abbotsford Bus Transit App - Calling Files From Server\setUserPost.php on line 31

Error: Unknown column 'bus' in 'field list'

xmlhttp.open("GET","setUserPost.php?direction="+direction+"&busnum="+busnum+"&dayofweek="+dow+"&stopname="+stopname+"&time="+time+"&status="+value+"&comments="+comments,true);

<?php

header("Access-Control-Allow-Origin: *");

//$bus = intval($_GET['busnum']);

$bus = 1;
$direction = $_GET['direction'];
$stopname = $_GET['stopname'];
$status = $_GET['status'];
$comments = $_GET['comments'];
$btime = $_GET['time'];
$dayofweek = $_GET['dayofweek'];
$username = "annoy";


 $con=mysqli_connect("localhost","root","abc","xyz","3306");


mysqli_select_db($con,"xyz");

mysqli_query($con,"INSERT INTO cfv_postbusupdate (BusNumber, Direction, StopNames, Status, comments, username, dayofweek, time) VALUES (".bus.", '".direction."', '".stopname."', '".status."', '".comments."', '".username."', '".dayofweek."', '".btime."' )");


if (!mysqli_query($con,$sql))
  {
  die('Error: ' . mysqli_error($con));
  }

echo "1 record added";

mysqli_close($con);


?>

CREATE TABLE `cfv_postbusupdate` (
  `BusNumber` int(11) NOT NULL,
  `Direction` varchar(100) DEFAULT 'Not Provided',
  `StopNames` varchar(300) DEFAULT 'Not Provided',
  `Status` varchar(45) DEFAULT 'Not Provided',
  `comments` varchar(150) DEFAULT NULL,
  `username` varchar(45) DEFAULT 'anonymous_user',
  `dayofweek` varchar(45) DEFAULT NULL,
  `time` varchar(20) DEFAULT NULL,
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `DatePosted` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) 

2 个答案:

答案 0 :(得分:4)

您在变量名称前缺少美元符号:

mysqli_query($con,"INSERT INTO cfv_postbusupdate (BusNumber, Direction, StopNames, 
    Status, comments, username, dayofweek, time) VALUES (".bus.", '".direction."', 
    '".stopname."', '".status."', '".comments."', '".username."', '".dayofweek."',
    '".btime."' )");

应该是

mysqli_query($con,"INSERT INTO cfv_postbusupdate (BusNumber, Direction, StopNames, 
    Status, comments, username, dayofweek, time) VALUES (".$bus.", '".$direction."',
    '".$stopname."', '".$status."', '".$comments."', '".$username."', '".$dayofweek."',
    '".$btime."' )");

答案 1 :(得分:2)

尝试将mysqli_query语句替换为:

mysqli_query($con,"INSERT INTO cfv_postbusupdate (BusNumber, Direction, StopNames, Status, comments, username, dayofweek, time) VALUES (".$bus.", '".$direction."', '".$stopname."', '".$status."', '".$comments."', '".$username."', '".$dayofweek."', '".$btime."' )");

你忘了变量之前的$&#39;名。