0

I have a form, below, that was working for a while, however for some reason the code has now stopped inserting what the user would input via the form. Once the form was working, the only things I changed were the style, and nothing on the database site, or with the insertion of the data.

I get zero errors when I hit the Submit button, and it takes me to the page that I want the form to take the user too.

<form action="insert.php" method="post">
<p>
    <label for="firstName">First Name:</label>
    <input type="text" name="name" id="Name">
</p>
<p>
    <label for="emailAddress">Email Address:</label>
    <input type="text" name="email" id="email">
</p>
<input type="submit" value="Submit">

and my PHP code is:

<?php
$link = mysqli_connect("localhost", "root", "root", "travelsite");
// 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, $_REQUEST['name']);
$email = mysqli_real_escape_string($link, $_REQUEST['email']);

// attempt insert query execution
$sql = "INSERT INTO subscribe (name, email) VALUES ('$name', '$email')";
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); ?>

1 Answers1

0

Try using $_POST['name'] and $_POST['email'].

Also as mentioned on the comments: you got your variables mixed up. (thx @NitinP)

Try also using a prepare statement like:

/* create a prepared statement */
if ($stmt = mysqli_prepare($link, "INSERT INTO subscribe (name, email) VALUES (?, ?)")) {

    /* bind parameters for markers */
    mysqli_stmt_bind_param($stmt, "ss", $name, $email); /*or $first_name*/

    /* execute query */
    mysqli_stmt_execute($stmt);

    printf("%d Row inserted.\n", mysqli_stmt_affected_rows($stmt));

    /* close statement */
    mysqli_stmt_close($stmt);
}

For more information see the link under Procedural style

Domenik Reitzner
  • 1,449
  • 1
  • 11
  • 20