Click here to Skip to main content
15,894,720 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
HI all

I want to insert my html form data to my database

this is my form:

XML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Add Records Form</title>
</head>
<body>
<form action="insert.php" method="post">
    <p>
        <label for="firstName">First Name:</label>
        <input type="text" name="firstname" id="firstName">
    </p>
    <p>
        <label for="lastName">Last Name:</label>
        <input type="text" name="lastname" id="lastName">
    </p>
    <p>
        <label for="emailAddress">Email Address:</label>
        <input type="text" name="email" id="emailAddress">
    </p>
    <input type="submit" value="Add Records">
</form>
</body>
</html>



and this is the insert.php :
PHP
<?php
$link = mysqli_connect("127.0.0.1", "root","", "person");
 
// 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, $_POST['firstname']);
$last_name = mysqli_real_escape_string($link, $_POST['lastname']);
$email_address = mysqli_real_escape_string($link, $_POST['email']);

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





but I have this error when I click the form button :
ERROR: Could not able to execute


please help!
Posted

Is table the name of your table?
If yes - which is a baaaaad idea, as it is a reserved word, you need to escape it.
If ANSI MODE is ON, than put it between double quotes, else use back tick (`) escaping.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 12-Feb-15 15:21pm    
5ed... :-)
—SA
amir.nazarizadeh 12-Feb-15 15:22pm    
thanks and thanks :)
fixed :)
As an addition to solution 1, instead of placing the variables inside to SQL statement, use the bind_param, see 3.10.4 mysqli_stmt::bind_param, mysqli_stmt_bind_param[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900