PHP MySQL插入不起作用 [英] PHP MySQL insertion not working

查看:172
本文介绍了PHP MySQL插入不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将数据插入到phpmyadmin中。我有点新鲜。我试图用HTML表单插入数据。我有以下HTML代码:

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> 

和PHP:

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


推荐答案

如果您不想将值插入数据库你可以忽略它。
您应该使用例如now()从mySQL db本身输入当前时间或date()函数或默认字段类型。

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());";

占位符:question /:afbeelding必须匹配下面的绑定名称,并且必须绑定所有你的变量占位符:

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();

这篇关于PHP MySQL插入不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆