Home > Back-end >  Form to post into database table
Form to post into database table

Time:11-17

I'm trying to make a form that posts the index.php input to my database table using index.php and connection.php. Also I'm trying to specify everything else to be in letter format except the phone number (puhelinnumero) in numeric format using bind_param, but it gives me this error:

enter image description here

Here is the index.php.

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <link rel="stylesheet" href="style.css">
    </head>
    <body>
        <div class="lomake-container">
            <form action="connection.php" method="POST">
                <h2>Ilmoittautumis lomake</h2>

                <div class="lomake-block">
                    <label for ="nimi">Etunimi</label>
                    <input type="text" name="etunimi" id="nimi" placeholder="Etunimi">
                </div>

                <div class="lomake-block">
                    <label for ="sukunimi">Sukunimi</label>
                    <input type="text" name="sukunimi" placeholder="Sukunimi">
                </div>

                <div class="lomake-block">
                    <label for="male">Mies</label>
                    <input type="radio" id="male" name="sukupuoli">
                </div>

                <div class="lomake-block">
                    <label for="female">Nainen</label>
                    <input type="radio" id="female" name="sukupuoli">
                </div>

                <div class="lomake-block">
                    <label for="other">Muu</label>
                    <input type="radio" id="other" name="sukupuoli">
                </div>

                <div class="lomake-block">
                    <label for ="sähköposti">Sähköposti</label>
                    <input type="text" name="sähköposti" id="sähköposti" placeholder="Sähköposti">
                </div>

                <div class="lomake-block">
                    <label for ="salasana">Salasana</label>
                    <input type="text" name="salasana" id="salasana" placeholder="Salasana">
                </div>

                <div>
                    <label for ="puhelinnumero">Puhelin numero</label>
                    <input type="text" name="puhelinnumero" id="puhelinnumero" placeholder="Puhelin num.">
                </div>

                <input type="submit" value="Lähetä">

            </form>
        </div>
    </body>
    
</html>

Here is the connection.php

<?php

$etunimi = $_POST["etunimi"];
$sukunimi = $_POST["sukunimi"];
$sukupuoli = $_POST['sukupuoli'];
$sähköposti = $_POST['sähköposti'];
$salasana = $_POST['salasana'];
$puhelinnumero = $_POST['puhelinnumero'];

$servername = "localhost";
$username = "root";
$password = '';
$database = 'palvelu';


$conn = new mysqli($servername, $username, $password, $database);

// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}else {
    echo "Yhteys onnistui";
    $stmt = $conn->prepare("insert into lomake($etunimi, $sukunimi, $sukupuoli, $sähköposti, $salasana, $puhelinnumero)
    values(?, ?, ?, ?, ?, ?)");
}
$stmt->bind_param("sssssi",$etunimi, $sukunimi, $sukupuoli, $sähköposti, $salasana);
echo "onnistui jea";
$stmt->execute();
$stmt->close();
$conn->close();




?>

Here is the table:

enter image description here

CodePudding user response:

You can't parameterise column names, but anyway I'm pretty sure that's not actually your intention, and you've possibly slightly misunderstood how to build an INSERT query. You need to specify the column names you want to insert into. The variable values you're currently trying to use in place of column names will be automatically assimilated into the query via the ? placeholders when MySQL receives the query.

Also you forgot to put the last value into the bind_param command.

Lastly your logic is a tiny bit flawed - if the connection fails, then your code will die. There's no need for the else. If it doesn't die, just carry on.

Try this instead:

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

echo "Yhteys onnistui";
$stmt = $conn->prepare("insert into lomake(`nimi`, `sukunimi`, `gender`, `email`, `password`, `number`) values(?, ?, ?, ?, ?, ?)");
$stmt->bind_param("sssssi",$etunimi, $sukunimi, $sukupuoli, $sähköposti, $salasana, $puhelinnumero);
echo "onnistui jea";
$stmt->execute();
$stmt->close();
$conn->close();

P.S.

Here is the MySQL documentation reference for INSERT: https://dev.mysql.com/doc/refman/8.0/en/insert.html

  • Related