PHP $_POST 不通过 h​​tml 表单保存在 mysql 上

标签 php html mysql

我试图在 mysql 上保存一些数据,但它没有保存。

注意:mysql 数据库在直接控制台代码下工作正常。

这是我的 HTML 代码:

<form method="post" action="prueba1.php" >
    Nombre: <input type="text" name="nombre" id="nombre" /></br>
    Email: <input type="text" name="email" id="email" /></br>
    Comentarios: <input type="text" name="comentarios" id="comentarios" / ></br>
    <input type="submit" name="submit" value="Registrar" />
    </form>

这是我的 PHP 代码:

if(!empty($_POST))
    {
        try
            {
                $nombre = $_POST ['nombre'];
                $email = $_POST ['correo'];
                $comentarios = $_POST ['comentarios'];
                $sql_insert = "
                INSERT INTO usuario (nombre, correo, comentarios)
                VALUES (?,?,?) ";
                $stmt = $con->prepare($sql_insert);
                $stmt->bindValue(1, $nombre);
                $stmt->bindValue(2, $email);
                $stmt->bindValue(3, $comentarios);
                $stmt->execute();
                echo "<h3>Tu comentario se a registrado con éxito</h3>";
                            }
        catch (Exception $e)
        {
                echo "<h3>No se pudo registrar tu comentario</h3>";
                die(var_dump($e));
        }

注意:当我按下带有值“Registrar”的按钮时,告诉我保存已成功。但数据库中不存在。

我们将不胜感激。

最好的问候。

最佳答案

说明,是不是正确的使用方式

 $stmt->bindValue(1, $nombre);
 $stmt->bindValue(2, $email);
 $stmt->bindValue(3, $comentarios);

因为我只用过bind_param

$stmt->bind_param('is',$value1,$value2); // where i is integer and s is string

尝试使用

 $stmt->bind_param('sss',$nombre,$email,$comentarios); // hope all three are strings

然后检查数据库中的条目,抱歉,如果我错了...

关于PHP $_POST 不通过 h​​tml 表单保存在 mysql 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27053354/

相关文章:

Php验证和注册

php - mysql 每个日期只更新 50 条记录

php - 将查询中的值放入数组

java - 连接到 MySQL Web 数据库并使用 Pictures Android 列出结果

php - 不能使用 CodeIgniter 的 LIKE()

javascript - 文件上传: Check for file format and then invoke srvlet

html - CSS 响应式 : mobile version

html - 防止文字在小宽度的图片上

java - 使用 JOOQ 从表生成枚举类

php - 在docker镜像中安装mysql客户端