我有一个脚本,将数组中的所有数据插入到MYSQL。但是,当数组中只有一个单词时,脚本不会给出错误,而当有多个单词时,它会给出一个
Column count doesn't match value count at row 1以下是我的代码
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST') {
include("connect.php");
$counter = 0;
$counters = 0;
$string = mysql_real_escape_string($_POST['words']);
$arr = explode(" ", $string);
mysql_query("SET charset utf8");
$sql = mysql_query("SELECT `word` FROM unicode WHERE word IN ('".implode("', '", $arr) . "')") or die (mysql_error());
$dupes = array();
while($r = mysql_fetch_assoc($sql)) {
$dupes[] = $r['word'];
}
$newwords = array_diff($arr, $dupes);
if(count($newwords)) {
$word = implode("'),('", $newwords);
$md5 = md5($word);
$sqli = mysql_query("INSERT INTO unicode (word, word_hash) VALUES ('$word', '$md5')") or die (mysql_error());
}
}
?>请帮帮忙..。
发布于 2013-07-17 17:58:20
通常,当我在使用SQL时遇到问题时,我会执行以下操作来跟踪问题。
SQL
最后,我不像上面建议的那样对内爆等问题发表评论,但我也会补充以下内容。查看您的查询,看起来您正在执行我在第1点中提到的操作。
$sqli = mysql_query("INSERT INTO unicode (word, word_hash) VALUES ('$word', '$md5')") or die (mysql_error());$word和$md5两边的单引号表示将$word和$md5直接传递到DB中。在使用双引号“...”中的变量时您不需要在它们周围放置任何东西,只需按原样使用它们即可。或者,如果您想使用单引号,可以连接查询字符串。
$sqli = mysql_query('INSERT INTO unicode (word, word_hash) VALUES ( ' . $word . ', ' . $md5 . ')') or die...再次回显您需要确认的查询(不使用mysqli_query函数)。
希望这能有所帮助。
S
发布于 2013-07-17 17:34:50
您的INSERT查询中的列参数数量超过了2,但是您只提供了2个值。
$word = implode("'),('", $newwords);这里的这句话是罪魁祸首。当您内爆$newwords数组时,您可能会得到超过2个值的。当插入到MySQL查询中时,它将与您提供的VALUES数量不匹配。这就是导致错误的原因。
发布于 2013-07-17 17:38:32
您正在内爆$newwords,因此结果查询将如下所示:
...VALUES ('word1'),('word2'),('word3', 'md5 string')向implode()添加$md5
$md5 = 'md5 string';
$word = implode("', '$md5'),('", array('word1', 'word2', 'word3'));输出:
...VALUES ('word1', 'md5 string'),('word2', 'md5 string'),('word3', 'md5 string')https://stackoverflow.com/questions/17696090
复制相似问题