0

I can't get this code to set the input in form to my database, and I can't figure out why. Is there any that can help me figure you why?

I'm trying to use the form to get input from user.

<form action="" method="post">
    Etternavn:<br>
    <input type="text" name="etternavn" id="etternavn" placeholder="Etternavn"><br>
    Fornavn:<br>
    <input type="text" name="fornavn" id="fornavn" placeholder="Fornavn"><br>
    Klasse:<br>
    <input type="text" name="klasse" id="klasse" placeholder="Klasse"><br>
    Mobil:<br>
    <input type="text" name="mobli" id="mobil" placeholder="Mobil"><br>
    Nettside:<br>
    <input type="text" name="www" id="www" placeholder="Nettside"><br>
    Epost:<br>
    <input type="email" name="epost" id="epost" placeholder="Epost">
    <input type="submit" name="submit" value="Submit">
</form>

Here I'm running the PHP PDO to get hold in the database and try to put the user input in to the database but I can't see why it doesn't work. I don't get any messages that tell me that anything is wrong.

<?php


if (isset($_POST["submit"])){
    $host = "kark.hin.no";
    $dbname = "stud_v16_klemetsen";
    $username = "v16_klemetsen";
    $password = "**********";

    try {
        $dbh = new PDO("mysql:host=$host;dbname=$dbname",$username,$password);
        $dbh->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
        $q = $dbh->prepare("INSERT INTO studenter(etternavn,fornavn,klasse,mobil,www,epost)
        VALUES (:etternavn, :fornavn, :klasse, :mobil, :www, :epost");
        $q->bindParam(':etternavn',$_POST['etternavn'],PDO::PARAM_STR);
        $q->bindParam(':fornavn',$_POST['fornavn'],PDO::PARAM_STR);
        $q->bindParam(':klasse',$_POST['klasse'],PDO::PARAM_STR);
        $q->bindParam(':mobil',$_POST['mobil'],PDO::PARAM_STR);
        $q->bindParam(':adr',$_POST['www'],PDO::PARAM_STR);
        $q->bindParam(':epost',$_POST['epost'],PDO::PARAM_STR);
        $q->execute();

        $q->execute();
        echo "succssfull";

    }
    catch (PDOException $e){
        echo "ERROR" . $e->getMessage();
    }
    $dbh = null;
}
?>
IMSoP
  • 89,526
  • 13
  • 117
  • 169

2 Answers2

1

$q->bindParam(':adr',$_POST['www'],PDO::PARAM_STR);

This line does not match with your SQL

"INSERT INTO studenter(etternavn,fornavn,klasse,mobil,www,epost) VALUES (:etternavn, :fornavn, :klasse, :mobil, :www, :epost"

Your bind parameter must be :www

  • even if i do thise changes i dont get anything added to my database sadly – Ruben Klemetsen Mar 24 '16 at 10:37
  • Yes, sorry I just saw something else. Your SQL Syntax isn't correct, you forgot a `)` : `INSERT INTO studenter(etternavn,fornavn,klasse,mobil,www,epost) VALUES (:etternavn, :fornavn, :klasse, :mobil, :www, :epost)` – Clément Laffitte Mar 24 '16 at 13:29
-1

Also, you're missing an end closing bracket ) in your SQL query, it should be:

$sql = "INSERT INTO studenter(etternavn,fornavn,klasse,mobil,www,epost)
        VALUES (:etternavn, :fornavn, :klasse, :mobil, :www, :epost)";
$q = $dbh->prepare($sql);
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Jaquarh
  • 6,493
  • 7
  • 34
  • 86