动态表单字段不将所有数据插入数据库

时间:2014-07-17 10:41:00

标签: php sql forms mysqli

我试图通过动态表单将数组插入数据库。表单将插入表单中的第一个值,但不再是。尝试了几种方法并且碰壁了。

这是PHP文件:

<?php
$con = new MySQLi(CONNECTION INFORMATION);
if (!$con)
{
    die('Could not connect: ' . mysqli_error());
}


foreach($_POST['room_types'] as $cnt => $room_types);
$room_type = implode(",", $room_types);

foreach($_POST['rooms'] as $cnt => $rooms);
$room = implode(",", $rooms);

foreach($_POST['users'] as $cnt => $users);
$user = implode(",", $users);
foreach($_POST['qty'] as $cnt => $qty);


$sql="INSERT INTO ConfigForm (HotelName,ADD1,ADD2, town, county, postcode, phone, fax, hotel_email, website_url, hotel_contact_name, accounts_contact_name, accounts_email, web_dev_company, web_dev_contact, web_dev_email, tarrif_selection, website_link, payment_select_box, barclays, barclays_store_id, barclays_username, barclays_password, worldpay, worldpay_username, worldpay_password, secpay_username, secpay_password, secpay_template, securetrading, securetrading_site_ref, securetrading_username, securetrading_password, sagepay, sagepay_account, sagepay_password, welcome_secure, welcome_secure_account, no_payment, paypal, paypal_account,roomnum,roomtype) 
VALUES
('$_POST[hotel_name]','$_POST[address_line1]','$_POST[address_line2]','$_POST[town]','$_POST[county]','$_POST[postcode]','$_POST[phone]','$_POST[fax]','$_POST[hotel_email]','$_POST[website_url]','$_POST[hotel_contact_name]','$_POST[accounts_contact_name]','$_POST[accounts_email]','$_POST[web_dev_company]','$_POST[web_dev_contact]','$_POST[web_dev_email]','$_POST[tarru]','$_POST[website_link]','$_POST[payment_select_box]','$_POST[barclays]','$_POST[barclays_store_id]','$_POST[barclays_username]','$_POST[barclays_password]','$_POST[worldpay]','$_POST[worldpay_username]','$_POST[worldpay_password]','$_POST[secpay_username]','$_POST[secpay_password]','$_POST[secpay_template]','$_POST[securetrading]','$_POST[securetrading_site_ref]','$_POST[securetrading_username]','$_POST[securetrading_password]','$_POST[sagepay]','$_POST[sagepay_account]','$_POST[sagepay_password]','$_POST[welcome_secure]','$_POST[welcome_secure_account]','$_POST[no_payment]','$_POST[paypal]','$_POST[paypal_account]','$qty','".$_POST['name'][$cnt]."')";

if (!mysqli_query($con,$sql))
{
    die('Error: ' . mysqli_error($con));
}

echo "<h1>Form Submitted!<h1>";
mysqli_close($con);
?>

以下是表单的链接:http://apollowebtools.com/WAForm/config_form.html

非常感谢任何帮助,谢谢

3 个答案:

答案 0 :(得分:1)

明显的以下代码行是什么

foreach($_POST['room_types'] as $cnt => $room_types);

遍历数组并将$room_types的值更改为数组的每个连续元素。因此,在foreach结束后,$room_types不是数组,而是$_POST['room_types']数组的最后一个元素

你应该去:

$room = '';
foreach($_POST['room_types'] as $cnt => $room_types)
    $room .= ',' .$room_types;

答案 1 :(得分:0)

我认为错误在于您没有选择数据库。

添加以下内容:

<?php
      $con=mysqli_connect('host','user','password');

      if (mysqli_connect_errno()) {
        echo ("conection error : %s\n" . mysqli_connect_error());
        exit();
    }

$db_selected = mysqli_select_db("//database name", $con);

if (!$db_selected)
  {
  die ("Can\'t use this databse : " . mysqli_error());
  }

    ?>

可以从以下链接找到更多帮助:mysqli_select_db

我不确定,但希望这会奏效。

答案 2 :(得分:0)

检查您的查询,我认为不对。

Echo $ sql并在表manuaaly中运行此查询。

我可以看到$ _POST [hotel_name],它必须是$ _POST ['hotel_name']。你没有添加引号。