Form to Database

I’m still trying to get this code to work by retrieving data from a previous page sent from a form using POST

This is the code now:

[php]<?php

$con=mysqli_connect("*************************");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

mysqli_query($con,“INSERT INTO emails (emailad)
VALUES (’$_POST[emailad]’)”);
mysqli_close($con);
[/php]

The data in the database row is ‘Array’ when i remove the [emailad} but when this is present with the $_POST i still get no data in the database, its just empty.

How do i get it so data from the previous form is upped to the database?

could you use print_r($_POST) and print_r($_GET) in your script to make sure that the data is transmitted correctly. cause except maybe for security issues i cant see anything wrong with the script.

Try this one.
[php]<?php

$con = mysqli_connect("*************************");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$emailad = $_POST['emailad'];

$query = "INSERT INTO emails (emailad) VALUES ('{$emailad}')";

mysqli_query($con, $query);

mysqli_close($con);

?>[/php]

No fix so far.

As suggested i’ve tried echo POST and GET, but all i’m getting from this is ‘Array Array’

So i’ll post my form code and see if there’s anything I’m doing wrong.

[php]
Email:

</form[/php]

Also if possible, could i have some tips on the security of the script and how I could improve it (the previous PHP one)

Okay now from using print_r($_POST) the page is showing:

Array ( [emailad] => [email protected] )

any ideas how i just use the end email part instead of just getting array in the database row?

UPDATE:
I have no idea what i did but it’s now working?

i’d still like to know how i can make this more secure as someone mentioned

When you try to echo any Array i.e. GET, POST etc. you will see Array in the browser instead of its contents.

Use var_dump($_POST) or print_r ($_GET) and you will see all its contents. have you closed the form tag properly because I’m seeing an incomplete form closing tag. You are using “EMAIL” as your input field’s name in the form but referring to it as emailad in the php script. It should be same.

Try this one and then tell me if it worked or not?

<form action="connectdatabase.php?" method="post">
Email: <input name="email" type="email" />
<input value="Join List" type="submit" />
</form>

[php]

<?php $con = mysqli_connect("*************************"); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } $email = $_POST['email']; $query = "INSERT INTO emails (emailad) VALUES ('{$email}')"; mysqli_query($con, $query); mysqli_close($con); ?>

[/php]

The name of your column should also be emailad in the database table.

Sponsor our Newsletter | Privacy Policy | Terms of Service