PHP MySQL insertion not working

2019-08-08 02:27发布

I am trying to insert data into phpmyadmin. I am kinda new to this. I am trying to insert data with an HTML form. I have the following HTML code:

<form id = "vraagStellen" action = "shoutbox.php" method = "post" class = "col-lg-12 col-md-12 col-sm-12 form-inline" role="form">
    <div class = "selectAfbeelding form-group">
        <label for "selectAfb">Naam:</label><br />
        <input type = "text" id = "selectAfb" name = "selectAfb" class = "form-control" maxlength="30"><br />
    </div>
    <div class = "selectVraag form-group">
        <label for "vraag">Bericht:</label><br />
        <input type = "text" id = "vraag" name = "vraag" class = "form-control" maxlength="100">
    </div>
    <br />
        <input type="reset" name = "reset" value = "Opnieuw" id = "reset">
        <input type="submit" name = "verzend" value = "Verzenden" id = "verzenden">
</form> 

And the PHP:

try {
    $conn = new PDO("mysql:host=".localhost.";dbname=".dbname, dbuser, dbpass);
} catch(PDOException $e) {
    echo $e->getMessage();
    die();
}
$afbeelding = $_POST['selectAfb'];
$question = $_POST['vraag'];

$sql3 = "INSERT INTO `dbi286018`.`vraagstellen` (`id`, `Afbeelding`, `Vraag`, `date`) VALUES (NULL, :afbeelding, :question, CURRENT_TIMESTAMP);";
//Prepare your query
$stmt = $conn->prepare($sql3);
//Execute your query binding variables
$stmt->execute(array(':id'=>NULL, ':Afbeelding'=>$afbeelding, ':Vraag'=>$question));

2条回答
Animai°情兽
2楼-- · 2019-08-08 03:02

In addition to my comment, you are mixing vraag and question in your query (see the last line).

$sql3 = "INSERT INTO `dbi286018`.`vraagstellen` (`id`, `Afbeelding`, `Vraag`, `date`) VALUES (NULL, :afbeelding, :question, CURRENT_TIMESTAMP);";
//Prepare your query
$stmt = $conn->prepare($sql3);
//Execute your query binding variables
$stmt->execute(array(':id'=>NULL, ':Afbeelding'=>$afbeelding, ':question'=>$question));
查看更多
时光不老,我们不散
3楼-- · 2019-08-08 03:07

If you don`t want to insert value into db you can just omit it. You should use for example now() to enter current time, or date() function or default field type from mySQL db itself.

    $sql3 = "INSERT INTO `dbi286018`.`vraagstellen`
 (`Afbeelding`, `Vraag`, `date`) VALUES (:afbeelding, :question, now());";

The placeholder :question / :afbeelding, have to match binding names below, and you have to bind all your variables to placeholders:

  //Execute your query binding variables
    $stmt->execute(array(':Afbeelding'=>$afbeelding, ':question'=>$question));

I always prefer to build make queries like below, it`s easier for read:

$sth = $dbh->prepare("INSERT INTO `dbi286018`.`vraagstellen`
   (`Afbeelding`, `Vraag`, `date`) VALUES (:afbeelding, :question, now()");

$sth->bindParam(':afbeelding', $afbeelding);
$sth->bindParam(':question', $question);
$sth->execute();
查看更多
登录 后发表回答