如何将多个复选框答案插入数据库表?

时间:2011-12-07 01:56:24

标签: php mysql sql forms webforms

我的数据库存在很大问题。它正确连接,并与正确字段中插入表$_POST的表单company_info查询中的信息连接。

现在,我不知道我在这里做错了什么,但我不断得到

的模具错误
"Error querying database".

数据库版本为:phpMyAdmin 2.6.4-pl3

MySQL:5.0

有什么想法吗?如果需要,我可以为您提供其余的代码。

$dbc = mysql_connect('db390590179.db.1and1.com', 'dbo390590179', '*********')
or die('Error connecting to MySQL server.');

mysql_select_db("db390590179", $dbc);


$query = "INSERT INTO company_info (company_name, company_phone,  company_contact, company_address, " .
"company_city, company_state, company_zip, " .
"state_living, vehicles, position, " .
"experience, training, hazmat, " .
"require_hazmat, load_nyc, take_truck_home, " .
"have_rider, have_pet, choose_route, " .
"fuel, cash_advance, days_before_home, " .
"log_system, slip_seat, pre_pass, " .
"ez_pass, health_insurance, retirement_plan, " .
"payment_plan, calculate_pay, freight, " .
"loads, home_on_time, idle_time, " .
"equipment_condition, canada)" .

"VALUES ('$company_name', $company_phone', '$company_contact', '$company_address', '$company_city', " .
"'$company_state', '$company_zip', " .
"'$state_living', '$vehicles', '$position', " .
"'$experience', '$training', '$hazmat', " .
"'$require_hazmat', '$load_nyc', '$take_truck_home', " .
"'$have_rider', '$have_pet', '$choose_route', " .
"'$fuel', '$cash_advance', '$days_before_home', " .
"'$log_system', '$slip_seat', '$pre_pass', " .
"'$ez_pass', '$health_insurance', '$retirement_plan', " .
"'$payment_plan', '$calculate_pay', '$freight', " .
"'$loads', '$home_on_time', '$idle_time', " .
"'$equipment_condition', '$canada')";

$result = mysql_query($query, $dbc)
or die('Error querying database.');

mysql_close($dbc);

4 个答案:

答案 0 :(得分:1)

我认为这是因为它在INSERT语句中的变量$company_phone之前缺少引号。

答案 1 :(得分:0)

for (int i = 0; i < CheckBoxList1.Items.Count - 1; i++)
{
  String str = "";
  if (CheckBoxList1.Items[i].Selected)
  {
    str = CheckBoxList1.Items[i].Text;
    con.Open();
    string sql =
      "Insert into dbtable(Category,BookTitle,Feature,SubCategory)values('" +
      DDLCategory.SelectedItem.Text + "','" + TxtBooktitle.Text + "','" +
      CheckBoxList1.Items[i].Text + "','" + DDLSubcategory.SelectedItem.Text +
      "')";
    SqlCommand cmd = new SqlCommand(sql, con);
  }
}

只需使用DEBUGGER,看看情况如何,你应该能够轻松解决这些问题。

答案 2 :(得分:0)

只需在单个引号中组合不同的值即可。

例如,“'$ company_state,$ company_zip,'”。“'$ state_living,$ vehicles,$ position,'”。“'$ experience,$ training,$ hazmat,'” ....

这将完美地运作,并且还必须包含 * $ company_phone * 开头的缺失引用。

答案 3 :(得分:0)

您可以从每行中删除双引号并合并它们。我删除了语法错误。你可以确保结果是否成功。试试这段代码。

$dbc = mysql_connect('db390590179.db.1and1.com', 'dbo390590179', '*********')
        or die('Error connecting to MySQL server.');

mysql_select_db("db390590179", $dbc);


$query = "INSERT INTO company_info (company_name, company_phone,  company_contact, company_address,
        company_city, company_state, company_zip,
        state_living, vehicles, position,
       experience, training, hazmat,
       require_hazmat, load_nyc, take_truck_home,
        have_rider, have_pet, choose_route,
       fuel, cash_advance, days_before_home,
        log_system, slip_seat, pre_pass,
        ez_pass, health_insurance, retirement_plan,
        payment_plan, calculate_pay, freight,
        loads, home_on_time, idle_time,
        equipment_condition, canada)

        VALUES ('$company_name', '$company_phone', '$company_contact', '$company_address', '$company_city',
       '$company_state', '$company_zip',
        '$state_living', '$vehicles', '$position',
        '$experience', '$training', '$hazmat', 
        '$require_hazmat', '$load_nyc', '$take_truck_home',
        '$have_rider', '$have_pet', '$choose_route',
        '$fuel', '$cash_advance', '$days_before_home',
        '$log_system', '$slip_seat', '$pre_pass',
        '$ez_pass', '$health_insurance', '$retirement_plan',
        '$payment_plan', '$calculate_pay', '$freight',
        '$loads', '$home_on_time', '$idle_time',
        '$equipment_condition', '$canada')";

$result = mysql_query($query, $dbc)
        or die('Error querying database.');

mysql_close($dbc);