0

I am working with php and mysql for the first time. The goal is to have a table that store email addresses to form a mailing list for a newsletter. my table Emails has 2 columns ID (INT auto increment) and email (varchar, 255)

I can connect to the database but I cannot write to it. I think my problem is in the syntax of my INSERT INTO statement. I have seen many examples and they seem to use different syntax specifically around the values. form code:

    <form method="post" action="email.php" class="form-container">
    <div class="form-title"><h2>Sign up for my newsletter!</h2></div>
    <div class="form-title">Email Address</div>
    <input class="form-field" required="required" placeholder="[email protected]" type="text" name="newEmail" /><br />
    <div class="submit-container">
    <input class="submit-button" type="submit" value="Submit" /></div>
  </form>

php code:

    <?php
    $dbHost = "localhost";
    $dbUser = "input";
    $dbPass = "input";
    $dbName = "MailingList";


$conn= mysqli_connect ($dbHost, $dbUser, $dbPass, $dbName);

if(mysqli_connect_errno()) {
die("FAIL:". mysqli_connect_error() . "(" . mysqli_connect_errno() . ")");
}

$addEmail = "mysqli_real_escape_string($_POST['newEmail'])";

$query ="INSERT INTO Emails (email) VALUES ('$addEmail')"

mysqli_close($conn)
?>
2
  • 1
    Given the answers below, you're also missing a closing semi-colon $query ="INSERT INTO Emails (email) VALUES ('$addEmail')" you can use two of those answers to piece everything together. Commented Feb 18, 2014 at 18:20
  • Thanks for the help! Working on it now. Commented Feb 18, 2014 at 18:36

4 Answers 4

2

You have missed to add the $conn i.e database link to the mysqli_real_escape_string and also, you have wrapped the mysqli_real_escape_string() inside the ", so it consider as string. So remove the " and use it. Try this,

$addEmail = mysqli_real_escape_string($conn,$_POST['newEmail']);
                                 ......^
$query ="INSERT INTO Emails (email) VALUES ('$addEmail')";

instead of

$addEmail = "mysqli_real_escape_string($_POST['newEmail'])";
Sign up to request clarification or add additional context in comments.

1 Comment

You're welcome Krish. I deleted my comment after noticing it.
1

You need to execute the query, not just write it.

$query ="INSERT INTO Emails (email) VALUES ('$addEmail')";
mysqli_query($conn, $query);

If you use a prepared statement, you can save yourself the trouble of escaping:

$stmt = mysqli_prepare($conn, "INSERT INTO Emails (email) VALUES (?)");
mysqli_stmt_bind_param($stmt, "s", $_POST['newEmail']);
mysqli_stmt_execute($stmt);

If you want non-procedural style (aka oop), this would look like the following

$stmt = $conn->prepare("INSERT INTO Emails (email) VALUES (?)");
$stmt->bind_param("s", $_POST['newEmail']);
$stmt->execute();

3 Comments

I edited my code and now it is creating records, but still is not actually writing the email to the record. Any idea why?
So you have empty rows? As I said, you need to execute the query - then be sure to check for errors as well.
just to be clear in php this line is how you execute the query correct? mysqli_query($conn, $query); i will have to work on this more later, but thank you for your answer.
1

Get rid of the quotes around your escape function. This turns it into a string instead of actually escaping the value:

$addEmail = mysqli_real_escape_string($conn,$_POST['newEmail']);

1 Comment

$addEmail = mysqli_real_escape_string($conn,$_POST['newEmail']); ;-) since it's mysqli_*
1
$addEmail = mysqli_real_escape_string($conn,$_POST['newEmail']);

https://www.php.net/mysqli_real_escape_string

string mysqli_real_escape_string ( mysqli $link , string $escapestr )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.