0

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).

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>My Form</title>
    <meta name="description" content="An interactive form">
    </head>

    <body>

    <form action="test.php" method="post" id="Personalinfo">

    <label for="fname">Όνομα:</label>
    <input type="text" id="fname" name="firstname" placeholder="Όνομα 
    Πελάτη..">

    <input type="submit" value="Submit">

    </body>
    </html>

and now my php code:

    <?php
    $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 . "<br>" . $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

thanos_zach
  • 156
  • 4
  • 20

2 Answers2

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

'?' is to substitute in an integer, string, double or blob value.

You placed the '?', but forgot to prepare it using bind_param. More importantly, you have to pass $firstname value into $stmt->bind_param("s", $firstname);

Updated Code

$firstname = $_POST['firstname'];
$sql = $conn->prepare("INSERT INTO Guests (firstname) VALUES (?)");
$sql->bind_param("s", $firstname);

if ($sql->execute() === TRUE) {

Read

  1. Prepared Statements in MySQLi

  2. how to insert into mysql using Prepared Statement with php

Nana Partykar
  • 10,556
  • 10
  • 48
  • 77
0
       <!DOCTYPE html>
            <html>
            <head>
            <meta charset="utf-8">
            <meta http-equiv="X-UA-Compatible" content="IE=edge">
            <title>My Form</title>
            <meta name="description" content="An interactive form">
            </head>

            <body>

            <form action="test.php" method="post" id="Personalinfo">

            <label for="fname">Όνομα:</label>
            <input type="text" id="fname" name="firstname" placeholder="Όνομα 
            Πελάτη..">

            <input type="submit" name="submitForm" value="Submit">

            </body>
            </html> 
    **test.php file**
    <?php
        $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 . "<br>" . $conn->error;
       }

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

       $conn->close();
       ?>
Lekens
  • 1,823
  • 17
  • 31
  • Thank you very much for trying to help! Unfortunately i came up with this message when submiting: "Warning: mysqli::__construct(): (HY000/1044): Access denied for user ''@'localhost' to database 'mydb' in C:\xampp\htdocs\NutriWebSolutions\test.php on line 8 Connection failed: Access denied for user ''@'localhost' to database 'mydb'". – thanos_zach Jul 31 '17 at 10:06
  • oh.., $username is root – Lekens Jul 31 '17 at 10:10
  • I have edit my code, change the database username to root, i didn't notice you miss that before. Access will be granted now – Lekens Jul 31 '17 at 10:12
  • thanks again for your help! I didn't vote negative someone else must... Now i submit my form and a blaank page comes up with no errors but data aren't still transfered to dtbs :( – thanos_zach Jul 31 '17 at 10:24
  • Good, now that means the submit block of code is not executed, let's try a else statement....check the code again..i just add an update – Lekens Jul 31 '17 at 10:28
  • the name is submitForm in that way, that is why i wrote if(isset($_POST['submitForm '])) before that block can be executed. – Lekens Jul 31 '17 at 10:32
  • Ok, now the echo message shows up but still no data to mysql dtbs! – thanos_zach Jul 31 '17 at 10:35
  • You need to copy the html form as i did it,
    – Lekens Jul 31 '17 at 10:37
  • aa ok i have to change my html code too! let me try! – thanos_zach Jul 31 '17 at 10:38
  • if this is what is echoed "Are you sure you enter a firstname and the name of your html submit is submitForm" then it is because this name="submitForm" is not found in your code – Lekens Jul 31 '17 at 10:38
  • Hello again my friend! i'm working on my form and i wanted to ask if there is a way to dislpay the "New record created succesfuly" text next to submit button after submiting and not move to another page! Is there any way for that? – thanos_zach Aug 18 '17 at 16:58
  • Awesome! Thanks! – thanos_zach Aug 20 '17 at 11:16