mysql - 在有效的INSERT语法上返回false

时间:2015-02-25 04:39:13

标签: php mysql

我试图找出mysql在这里抛出的错误,但我不知道如何解决这个问题。我以前遇到过同样的问题,但它只是由值变量中的无效语法引起的,但这一次我找不到它的错误。有人可以帮帮我吗?

我的mysql表:

CREATE TABLE IF NOT EXISTS `merit` (
  `id` int(11) NOT NULL,
  `id_user` text,
  `id_num` text,
  `category` text,
  `timeoccur` text,
  `location` text,
  `remarks` text
)

ALTER TABLE `merit`
  ADD PRIMARY KEY (`id`);

ALTER TABLE `merit`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;

php代码:

function add_merit($d) {
    //id_user,id_num,type,time_occur,venue,description
    global $con; //this global tested and works
    foreach ($d as $k=>$v) {
        if ($v == "") {
            header('location: merit.php?method=error');
            die();
        }
        else {
            continue;
        }
    }
    $ic = $d['ic'];
    $type = $d['type']; 
    $loc = $d['loc'];
    $rem = $d['rem'];
    $uid = uid(); // function call on other php, tested and works
    $rs = $con->query("INSERT INTO merit(id_user,id_num,category,timeoccur,location,remarks) VALUES ('$uid','$ic','$type','$loc','$rem')");
    die(var_dump($rs)); //this returns false
    if ($rs) {
        //header('location: merit.php?method=success');
        die('ok');
    }
    else {
        //header('location: merit.php?method=error');
        die('err');
    }
}

5 个答案:

答案 0 :(得分:3)

您尚未传递字段变量: -

$rs = $con->query("INSERT INTO merit(id_user,id_num,category,timeoccur,location,remarks) VALUES ('$uid','$ic','$type','$loc','$rem')");

答案 1 :(得分:2)

选择6个字段进行插入,传递5个值

columns :- id_user,id_num,category,timeoccur,location,remarks
values :- '$uid','$ic','$type','$loc','$rem'

请查看并在查询中发送正确的值。

答案 2 :(得分:2)

你有6个字段值。缺少一个字段

$rs = $con->query("INSERT INTO merit(id_num,category,timeoccur,location,remarks) VALUES ('$uid','$ic','$type','$loc','$rem')");

答案 3 :(得分:1)

你错误的原因是你告诉mysql编辑6个表字段,但你只给它5个VALUES()。它们都必须等于相同的数量。

答案 4 :(得分:0)

在查询中,您必须传递6个字段,并且您传递了5个字段 所以更新您的查询如下:

 $rs = $con->query("INSERT INTO merit(id_num,category,timeoccur,location,remarks) VALUES ('$uid','$ic','$type','$loc','$rem')");