Jump to content

Array from form into database


timmah1

Recommended Posts

I followed a tutorial on doing a contact form, works great. But, I want the information to be submitted in to my database to access later.

 

The posted values are put into an array

$fieldss = array('name' => 'First Name', 'surname' => 'Last Name', 'phone' => 'Phone',  'email' => 'Email', 'venue' => 'Venue', 'vname' => 'Contact', 'vaddress1' => 'Address', 'vaddress2' => 'Addresss', 'vcity' => 'City', 'vstate' => 'State', 'vzip' => 'Zip', 'vphone' => 'Phone', 'eventdate' => 'Event Date', 'eventtime' => 'Event Time', 'eventname' => 'Event Name', 'message' => 'Message'); 

Then the values are sent to my email.

 

How can I take these values, and put them into my database?

 

I've tried numerous different things, searched, and followed tutorials, but nothing is working.

 

Here is the code I tried, but nothing gets inserted except my date fieild, but this is the first time something was inserted into the database

if(is_array($fieldss)){
    foreach ($_POST as $keys => $row) {
$book_first = mysql_real_escape_string($fieldss[$row][0]);
$book_last = mysql_real_escape_string($fieldss[$row][1]);
$book_email = mysql_real_escape_string($fieldss[$row][2]);
$query ="INSERT INTO booking (book_first, book_last, book_email) VALUES ( '". $book_first."','".$book_last."','".$book_email."' )";
mysqli_query($conn, $query);
    }
if ($conn->query($query) === TRUE) {
$okMessage = 'Form successfully submitted.<br>';
} else {
$errorMessage = 'There was an error while submitting. ';
}
}
 
Can somebody help me out here?
 
Thank you in advance
Link to comment
Share on other sites

You are off to a really bad start. The code you found out on the Internet is obsolete, insecure and has been completely removed from PHP. You need to use PDO with prepared statements. You are also mixing MySQL with mysqli. You never ever put variables in a query.

Edited by benanamen
Link to comment
Share on other sites

Alright, I noticed mixing the 2, that was a screw up. I fixed that, but still get nothing.

 

So your only solution is using PDO? 

 

I don't want to use PDO, But thank you for your suggestion.

 

Now, would anybody else be able to help me out?

Link to comment
Share on other sites

Well, what do you want? Right now, all I see is a random collection of half-understood features from three(!) different APIs: the old MySQL extension, the procedural mysqli API and then the object-oriented mysqli API.

 

If you want to use mysqli, you have to actually learn it from the ground up using the PHP manual. It's complicated, counter-intuitive and not very well understood in the PHP community, so following tutorials isn't going to help. You're also likely to spend more time (re)learning mysqli than simply switchting to PDO.

 

In any case, there is no magical fix for your code, if that's what you expected. You need to make a decision, learn the basics and then come back with halfway sane code and more information (like the HTML form).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.