Hello I am trying to use phpmyadmin and connect to my database. I did once before but I have never applied styles to the text boxes so I don't know if its right. For some reason I get an error message I told it to say if it fails to connect. I can't figure out why its not connecting. I'm using MAMP server Port: PHP 5.6.21
Here is my PHP code:
<?php
if($_POST['formSubmit'] == "Submit") {
$errorMessage = "";
if(empty($_POST['formName'])) {
$errorMessage .= "<li>You need to enter your name</li>";
}
if(empty($_POST['formEmail'])) {
$errorMessage .= "<li>You need to enter your email</li>";
}
if(empty($_POST['formSubject'])) {
$errorMessage .= "<li>Please enter a subject.</li>";
}
if(empty($_POST['formComment'])) {
$errorMessage .= "<li>Please enter your question.</li>";
}
$varname = $_POST['formName'];
$varemail = $_POST['formEmail'];
$varsubject = $_POST['formSubject'];
$varcomment = $_POST['formComment'];
if(empty($errorMessage)) {
$db = mysql_connect("localhost","root","root");
if(!$db) die("Error connecting to MySQL database.");
mysql_select_db("three_cats_database" ,$db);
$sql = "INSERT INTO contact_form (name, email, subject, comment) VALUES (".
PrepSQL($varname) . ", " .
PrepSQL($varemail) . ", " .
PrepSQL($varsubject) . "," .
PrepSQL($varcomment) . ") ";
mysql_query($sql);
header("Location: thankyou.php");
exit();
}
}
// function: PrepSQL()
// use stripslashes and mysql_real_escape_string PHP functions
// to sanitize a string for use in an SQL query
//
// also puts single quotes around the string
//
function PrepSQL($value)
{
// Stripslashes
if(get_magic_quotes_gpc()) {
$value = stripslashes($value);
}
// Quote
$value = "'" . mysql_real_escape_string($value) . "'";
return($value);
}
?>
Here is my HTML:
<p>Please fill out this form completely to contact us with any concerns or suggestions.</p><br>
<div class="imgbg"><div class="img">
<!-- FORM IS HERE -->
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<div class="contact-form margin-top">
<label for='formName'><span>Name:</span>
<input type="text" class="input_text" name="formName" id="name" maxlength="50" value="<?=$varname;?>"/>
</label>
<label for='formEmail'><span>Email:</span>
<input type="text" class="input_text" name="formEmail" id="email" maxlength="50" value="<?=$varemail;?>"/>
</label>
<label for='formSubject'><span>Subject:</span>
<input type="text" class="input_text" name="formSubject" id="subject" maxlength="50" value="<?=$varsubject;?>"/>
</label>
<label><span>Comment</span>
<textarea class="message" name="formComment" id="feedback"><?php echo htmlspecialchars($varcomment);?></textarea>
</label>
<input type="submit" class="button" name="formSubmit" value="Submit" />
</label>
</div>
</form>
Edit this line to get more informations on your error :
if(!$db) die("Error connecting to MySQL database.");
can become :
if(!$db) die("Error connecting to MySQL database : ".mysql_error($db));
You will see more information when crashing :)
Note : create a user for your table, it's better to never write root password in your php files