Reputation: 328
Okay, I've looked everywhere. To begin with I have a form, a newsletter, that users enter their email into. Once they hit submit it checks a php form that takes that email and inputs it into an SQL database that stores the email. The problem is I get this error when submitting the form
Error: INSERT INTO emails (email) VALUES ([email protected])
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '@yahoo.com)' at line 2
No idea why it wont put it into the database. The form code is
<section>
<div class="mockup-content">
<div class="morph-button morph-button-inflow morph-button-inflow-1">
<button type="button"><span>Subscribe to our Newsletter</span></button>
<div class="morph-content">
<div>
<div class="content-style-form content-style-form-4">
<h2 class="morph-clone">Subscribe to our Newsletter</h2>
<form action="scripts/mailer.php" method="post">
<p><input type="text" class="button" id="email" name="email" placeholder="[email protected]">
<div>We promise, we won't send you any spam. Just love.</div>
<input type="submit" class="button" id="submit" value="SIGN UP"></p>
</form>
</div>
</div>
</div>
</div><!-- morph-button -->
</div>
</section>
This is just the html data for the form, the php data is
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "mysql";
$email = ($_POST["email"]);
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO emails (email)
VALUES ($email)";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
Can anyone explaine why this is?
Upvotes: 1
Views: 122
Reputation: 1745
The email is in varchar datatype . you need to put quotes on it
$sql = "INSERT INTO emails (email) VALUES ('".$email."')";
Upvotes: 0
Reputation: 5991
You should use a tick ('
) when binding variables in a query:
$sql = "INSERT INTO emails (email) VALUES ('$email')";
It is okay not to use tick when the value of your variable you are trying to bind is an int
.
Upvotes: 1