我试图在 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 不通过 html 表单保存在 mysql 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27053354/