简体   繁体   中英

php SQL insert results empty entries in database.

When I submit the form, I get a success message however the data is missing from the database, the entries are blank..

Basically the problem is with in the $sql string.

The sql string will VALUES echo fine, however entries are blank in the database.

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
$name = (string)$_POST["name"];
$email = (string)$_POST["email"];
$phone = (string)$_POST["phone"];

$sql = "INSERT INTO myTable (name,email,phone)
VALUES ('{$name}','{$email}','{$phone}')";


if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();

I've changed the quotes to literals ' and added id to each form field, I also removed the (string)CAST Now it works.

PHP:

    $name =$_POST['name'];
    $email =$_POST['email'];
    $phone =$_POST['phone'];

HTML:

    <input type="text" id="name" name="name" placeholder="Name:"/><br/>
    <input type="text" id="email" name="email" placeholder="Email:"/><br/>
    <input type="text" id="phone" name="phone" placeholder="Phone Number:"/><br/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM