Sunday 3 November 2019

Send Form Data to mysqli dtbs using php fails (xampp environment)



Here is a my html form code: The problem is that i can't figure out how to succesfuly submit the form to mysql dtbs using xampp. (Data aren't sent to dtbs).




    




My Form


















and now my php code:



        $servername = "localhost";
$username = "username";
$password = "";
$dbname = "mydb";


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

$sql = "INSERT INTO Guests (firstname)
VALUES ('?')";


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

$conn->close();
?>



Data is not sent in the mysql dtbs! i've been trying for 2 days solving this but nothing... please help!



Kind regards, Thanos


Answer



       





My Form














**test.php file**
$servername = "localhost";
$username = "root";

$password = "";
$dbname = "mydb";

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


if(isset($_POST['submitForm'])){
$firstname = $_POST['firstname'];
$sql = "INSERT INTO Guests (firstname)
VALUES ('{$firstname}')";

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


}else{
echo "Are you sure you enter a firstname and the name of your html submit is submitForm";
}

$conn->close();
?>

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...