0

I am trying to insert an array into a database via a dynamic form. The form will insert the first values from the form but not any more. tried a couple of methods and have hit a wall.

Here is the PHP file:

<?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);
?>

Here is a link to the form: http://apollowebtools.com/WAForm/config_form.html

Any help much appreciated, Thanks

2
  • did you get an error ? Commented Jul 17, 2014 at 10:46
  • no, the form runs through, gets to the form submitted. all data apart from the 'added' values are inserted. Its like it doesnt know they are there Commented Jul 17, 2014 at 10:52

3 Answers 3

1

its obvious What following line of codes does is

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

iterate through the array and change the value of $room_types to each successive element of array. So after the foreach ends, $room_types is not an array but the last element of $_POST['room_types'] array

you should go for:

$room = '';
foreach($_POST['room_types'] as $cnt => $room_types)
    $room .= ',' .$room_types;
Sign up to request clarification or add additional context in comments.

Comments

0

I think the mistake is that you have not selected the database.

Add something like following:

<?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());
  }
  
    ?>

more help can be found from this link: mysqli_select_db

I'm not sure but hope this will work.

4 Comments

('host', 'user', 'password', 'database') is what im using during the connection
@Jack_McG ok...so its not a problem...can you try to saperate them?
@Jack_McG try separating mysqli_connect('host', 'user', 'pass') and then selecting databse...If it do not work, error should be something else.
@mebjas sorry but I didn't understand.
0

Check Your query, I think its not right.

Echo $sql and run this query in table manuaaly.

I can see $_POST[hotel_name], it must be $_POST['hotel_name']. You are not adding quotes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.