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); ?>

  • 4
    in your query `$name` must be `$first_name` – Nitin Pund Jun 01 '18 at 11:27
  • form tag is not closed in html – Suresh Kamrushi Jun 01 '18 at 11:28
  • I missed the form tag off when coping in. – Georgeehall Jun 01 '18 at 11:30
  • if you say that the form use to work, then you have definitely changed more than just the style. If you read @NitinP's comment, you can see why the above code wouldn't work. – M. Eriksson Jun 01 '18 at 11:30
  • Yes, thanks for pointing it out - This has not solved the issue. – Georgeehall Jun 01 '18 at 11:34
  • You should also make sure that you have error reporting turned on and, preferably, have display_errors turned on while developing. Then you would have caught the "undefined variable" issue. Here's a good post about it: [How do I get PHP errors to display?](https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display) – M. Eriksson Jun 01 '18 at 11:36

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,583
  • 1
  • 12
  • 21