Home > Net >  I'm trying to get a single value (an email address) from a html input to mysql using php
I'm trying to get a single value (an email address) from a html input to mysql using php

Time:07-07

$host = "localhost";
$dbname = "";
$username = "";
$password = "";
$db = mysql_connect($host, $username, $password);
        
  if (mysql_error() > "") print mysql_error() . "<br>";
  mysql_select_db($dbname, $db);
  if (mysql_error() > "") print mysql_error() . "<br>";

  $email = $_POST["email"];
  // Inserting these values into the MySQL table
  // we created above
  $query = "INSERT INTO Emails-prelaunch (Email) VALUES (" . $email . "')";
  $result = mysql_query($query);
  // mysql_query() is a PHP function for executing
  // MySQL queries
  echo "<p>Thank you for entering an email!</p>";

This is what've done so far but it gives giving me HTTPs 500 error or a syntax error in insert into value line

CodePudding user response:

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}

// prepare and bind
$stmt = $conn->prepare("INSERT INTO prelaunch (Email) VALUES (?)");
$stmt->bind_param("s", $email);

// set parameters and execute
$email = "[email protected]";

$stmt->execute();

If you don't want to use prepared statement (even if you should), there is only a missing ' character because $email is a String :

INSERT INTO Emails-prelaunch (Email) VALUES ('" . $email . "')"

CodePudding user response:

During the test phases, you can add the following code to the beginning of the file and find out the reason for the errors.

ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
  • Related