简体   繁体   English

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

[英]mysql - returns false on valid INSERT syntax

I tried to figure out an error thrown by mysql here, but I do not know on how to solve this issue. 我试图找出mysql抛出的错误,但我不知道如何解决这个问题。 I had the same problem before but it was only caused by invalid syntax in values variable, but this time round I cannot find what's wrong with it. 我以前遇到过同样的问题但它只是由值变量中的无效语法引起的,但这一次我找不到它的错误。 Can anyone help me out please? 有人可以帮帮我吗?

My mysql table: 我的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;

The php code: 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');
    }
}

你还没有传递字段变量: -

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

You are selecting 6 fields for insertion and passing 5 values 您正在选择6个字段进行插入并传递5个值

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

so have a look and send proper values in query. 所以看看并在查询中发送适当的值。

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

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

The reason why you are getting this wrong is because you tell mysql to edit 6 table fields but you are only giving it 5 VALUES(). 你错误的原因是你告诉mysql编辑6个表字段,但你只给它5个VALUES()。 They both have to equal the same amount. 它们都必须等于相同的数量。

在查询中,您必须传递6个字段,并且您传递了5个字段,因此请按如下方式更新查询:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM