MYSQL database wont connect to php form.

Not sure whats happening… Heres my code.

<?php $link = mysqli_connect("localhost", "Caast", "CENSOREDPASSWORD", "Data") or die("Error " . mysqli_error($link)); // Check connection if($link === false){ die("ERROR: Could not connect. " . mysqli_connect_error()); } // Escape user inputs for security $first_name = mysqli_real_escape_string($link, $_POST['firstname']); $last_name = mysqli_real_escape_string($link, $_POST['lastname']); $email_address = mysqli_real_escape_string($link, $_POST['email']); // attempt insert query execution $sql = "INSERT INTO user (first_name, last_name, email_address) VALUES ('$first_name', '$last_name', '$email_address')"; if(mysqli_query($link, $sql)){ echo "Records added successfully."; } else{ echo "ERROR: Could not able to execute $sql. " . mysqli_error($link); } // close connection mysqli_close($link); ?>

___________________HTML

Add Records Form

First Name:

Last Name:

Email Address:

Well, Larry, what is happening? You showed us some code, but did not bother to tell us the error you
are getting or what is being displayed. Is all of this on the same page? Is the posted page that you
named “insert.php” this same file? If not, you have no HTML code in it.

In other words, let’s explain PHP. PHP is server-side only. It is processed and the OUTPUTS of the code
is then sent to the client-side only browser. For a simple page like this you can have it post to itself or to
a second file. The second file would be the PHP code you showed us, but would also have the HTML code
to display the results. Just echoing ‘successful’ or ‘failed’ might not show due to no html code in the data.
You would have to add in the rest of the html code. In general terms like this:

your php code here Then, the php code would be stuck inside of an HTML page and the data should be displayed. Not be best way to handle it, but, it will work.

Since you did not tell us what happens when you run your code, I will guess that is the issue…

Sponsor our Newsletter | Privacy Policy | Terms of Service