使用INSERT DATEDIFF接收语法错误

时间:2014-07-23 09:26:55

标签: php mysql sql

我收到此错误,而不是将DATEDIFF插入数据库。我似乎无法找到问题所在。我试着检查语法。有人可以帮忙吗?

// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

// escape variables for security
$pdate = mysqli_real_escape_string($con, $_POST['pdate']);
$mdate = mysqli_real_escape_string($con, $_POST['mdate']);
$amt = mysqli_real_escape_string($con, $_POST['amt']);
$first = mysqli_real_escape_string($con, $_POST['first']);
$last = mysqli_real_escape_string($con, $_POST['last']);
$pid = mysqli_real_escape_string($con, $_POST['pid']);
$cno = mysqli_real_escape_string($con, $_POST['cno']);
$madd = mysqli_real_escape_string($con, $_POST['madd']);
$bene = mysqli_real_escape_string($con, $_POST['bene']);
$swc = mysqli_real_escape_string($con, $_POST['swc']);
$bacc = mysqli_real_escape_string($con, $_POST['bacc']);
$bank = mysqli_real_escape_string($con, $_POST['bank']);
$badd = mysqli_real_escape_string($con, $_POST['badd']);
$bno = mysqli_real_escape_string($con, $_POST['bno']);      

$sql="INSERT INTO contacts (
      nodays, interest, pdate, mdate, amt, first, last,
      pid, cno, madd, bene, swc, bacc, bank, badd, bno
    ) 
    VALUES (
      DATEDIFF($mdate,$pdate) AS nodays,
      (DATEDIFF($mdate,$pdate) * $amt / 365 * 0.1) AS interest,
      '$pdate','$mdate','$amt','$first','$last',
      '$pid','$cno','$madd','$bene','$swc','$bacc','$bank','$badd','$bno'
    )";



    if (!mysqli_query($con,$sql)) {
      die('Error: ' . mysqli_error($con));
    }
    echo "Client record has been added to the database!";

    mysqli_close($con);

3 个答案:

答案 0 :(得分:1)

问题是您尝试同时执行2个查询 - 仅以逗号分隔。这不起作用。 Seperagte他们并逐个执行。

答案 1 :(得分:0)

您要在联系人中插入另外两列:

您对所有列说INSERT INTO CONTACTS,但随后使用 SELECT *, DATEDIFF... FROM CONTACTS。因此,您尝试在同一个表中插入更多列。

答案 2 :(得分:0)

我假设您在插入时尝试计算nodaysinterest,这应该可行:

INSERT INTO contacts (
  nodays, interest, pdate, mdate, amt, first, last,
  pid, cno, madd, bene, swc, bacc, bank, badd, bno
) 
VALUES (
  DATEDIFF($mdate,$pdate),
  (DATEDIFF($mdate,$pdate) * $amt / 365 * 0.1),
  '$pdate','$mdate','$amt','$first','$last',
  '$pid','$cno','$madd','$bene','$swc','$bacc','$bank','$badd','$bno'
);