Php 如何向MYSQL插入值

Php 如何向MYSQL插入值,php,mysql,pdo,insert,Php,Mysql,Pdo,Insert,当每个选民都用自己的身份证投票时,我试图给数据库增加价值。 我的db得到值,但它向下一行,我希望db得到值1到ID行,而不是向下一行 任何关于这个问题的解决方案请 这是我的密码 $before1 = $row['vote_one_before']; $after1 = $row['vote_one_after']; $before2 = $row['vote_two_before']; $after2 = $row['vote_two_before']; $befor

当每个选民都用自己的身份证投票时,我试图给数据库增加价值。 我的db得到值,但它向下一行,我希望db得到值1到ID行,而不是向下一行

任何关于这个问题的解决方案请

这是我的密码

$before1    = $row['vote_one_before'];
$after1     = $row['vote_one_after'];
$before2    = $row['vote_two_before'];
$after2     = $row['vote_two_before'];
$before3    = $row['vote_three_before'];
$after3     = $row['vote_three_after'];

$vote = 1;

if(($before1 == 0) and ($after1 == 0) and ($before2 == 0) and ($after2 == 0) and ($before3 == 0) and ($after3 == 0)){
    $stmt = $conn->prepare("INSERT INTO DB (vote_one_before) VALUES(':vote_one_before')");
    $stmt->execute(array(':vote_one_before'=> $vote));
}
问题1:如果/否则 并且不是可以在IF/ELSE函数中使用的运算符-您需要&&。将您的IF行更改为:

问题2:查询 您的查询也不正确。对于INSERT,格式为:

INSERT INTO `table_name` (`column_name`) VALUES (`new_value`);
除非DB是表名,否则您需要将查询修改为以下内容:

    $stmt = $conn->prepare("INSERT INTO `table_name` (vote_one_before) VALUES (:vote_one_before)");

还请注意,当使用占位符时,您不需要单引号或双引号,因此“:vote_one_before”变成:vote_one_before.

您在问题1中错了:PHP中有and运算符,由于括号的缘故,它在这里可以很好地工作。当然,当您希望赋值运算符的优先级高于逻辑运算时,可以使用and,例如,$a=$b,或者只有在$b为false时,才会执行某些操作,并且在if子句中通常会使用&。但是你在这里提供的信息完全不正确。查阅可能的副本
    $stmt = $conn->prepare("INSERT INTO `table_name` (vote_one_before) VALUES (:vote_one_before)");