代码不更新数据库,但print_r显示一切都应该工作?

时间:2011-11-23 18:10:13

标签: php

这是我的代码。由于某种原因,print_r()将显示信息,但是数据库没有添加条目,当我尝试将多个变量添加到数据库时,它实际上并不更新组织表:

 <?php
include "mysql_config.php";
$con = mysql_connect($host, $user, $pass);
$result = mysql_select_db($db);

$orgname = mysql_real_escape_string(html2txt($_POST['orgname']));
$add1 = mysql_real_escape_string(html2txt($_POST['add1']));
$add2 = mysql_real_escape_string(html2txt($_POST['add2']));
$city = mysql_real_escape_string(html2txt($_POST['city']));
$state = mysql_real_escape_string(html2txt($_POST['state']));
$zip = mysql_real_escape_string(html2txt($_POST['zip']));
$url = mysql_real_escape_string(html2txt($_POST['url']));
$email = mysql_real_escape_string(html2txt($_POST['email']));
$phone = mysql_real_escape_string(html2txt($_POST['phone']));
$contact = mysql_real_escape_string(html2txt($_POST['contact']));
$hours = mysql_real_escape_string(html2txt($_POST['hours']));
$culture = array_map('mysql_real_escape_string', array_map('html2txt', $_POST['culture']));
$service = array_map('mysql_real_escape_string', array_map('html2txt', $_POST['service']));
$category = array_map('mysql_real_escape_string', array_map('html2txt', $_POST['category']));
if (isset($_FILES["file"]['name'])  && ($_FILES['file']['name'] !== '')) {
$file = $orgname."/".basename($_FILES['file']['name']);   
mkdir("./".$orgname);
move_uploaded_file($_FILES['file']['tmp_name'], $file);
}
else {
    $file = '';
}

$notes = mysql_real_escape_string(html2txt($_POST['notes']));
$description = mysql_real_escape_string(html2txt($_POST['description']));

print_r($orgname);
print_r($state);
print_r($hours);

mysql_query("INSERT INTO organization (org_name, add_1, add_2, city, state, zip, url, email, phone, contact, hours, file_loc, notes, desc)
             VALUES('".$orgname.", ".$add1.", ".$add2.", ".$city.", ".$state.", ".$zip.", ".$url.", ".$email.", ".$phone.", ".$contact.", ".$hours.", ".$file.", ".$notes.", ".$description."')");

?>

这是错误:

无效查询:列数与第1行的值计数不匹配

2 个答案:

答案 0 :(得分:1)

您没有对查询进行任何错误检查。使用mysql_error()查看问题所在。

在这种特定情况下,查询可能会失败,因为descreserved word

在字段名称周围添加反引号:

`desc` 

或使用其他列名。

答案 1 :(得分:1)

您在此处有错误:

mysql_query("INSERT INTO organization (org_name, add_1, add_2, city, state, zip, url, email, phone, contact, hours, file_loc, notes, desc)
             VALUES('".$orgname.", ".$add1.", ".$add2.", ".$city.", ".$state.", ".$zip.", ".$url.", ".$email.", ".$phone.", ".$contact.", ".$hours.", ".$file.", ".$notes.", ".$description."')");

你没有用引号围绕每一个

mysql_query("INSERT INTO organization (org_name, add_1, add_2, city, state, zip, url, email, phone, contact, hours, file_loc, notes, desc)
             VALUES('".$orgname."', '".$add1."', '".$add2."', '".$city."', '".$state."', '".$zip."', '".$url."'.....)";

我假设他们都在这里测试。

用于调试的

更新

将您的查询保存在变量中,打印出来,然后告诉我们:

$sql = "INSERT INTO organization ....."; // your query

echo $sql; // show us.