我的代码无法将数据发送到mysqli数据库,但它不会产生错误

bpzcxfmw  于 2021-06-15  发布在  Mysql
关注(0)|答案(1)|浏览(307)

我想写的这一页有很多问题。我读了很多帖子,但仍然没有找到我需要的答案。如果有人能帮助我,我将不胜感激。此表单的数据不会发送到数据库,但不会产生任何错误。
以前版本的代码出现访问错误。然后我修复了它,现在它不会产生错误。
但它仍然不发送数据。我将把代码粘贴到这里。
提前谢谢。

<form method="POST" action="#">
    Nome:
    <input type="CHAR" name="nome">
    <br><br>E-Mail:
    <input type="text" name= "email">
    <br><br>Mensagem:<br>
    <textarea name="comentario"></textarea>
    <br><br>
    <input type="submit" value="Enviar">
    <input type="reset" value="Limpar">
</form>
<hr>

<?php
$date = date_default_timezone_set('America/Sao_Paulo');
$con=mysqli_connect("localhost","root","","bdcomentarios");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
        {
$insert = mysqli_query($con,"INSERT INTO tbcomentarios('nome')");
        }
{
$insert = mysqli_query($con,"INSERT INTO tbcomentarios('email')");
    }
    {
$insert = mysqli_query($con,"INSERT INTO tbcomentarios('comentario')");
    }
    {
$insert = mysqli_query($con,"INSERT INTO             
tbcomentarios('date_default_timezone_get('Y/m/d')'");
    }
$sql = "SELECT * FROM tbcomentarios ORDER BY id desc";
$executar=mysqli_query($con, $sql);
while( $exibir = mysqli_fetch_array($executar)){
    echo $exibir['data'];
    echo "</br>";
    echo $exibir['nome'];
    echo "</br>";
    echo $exibir['email'];
    echo "</br>";
    echo $exibir['comentario'];
    echo "</br><hr>";
}
?>
zu0ti5jz

zu0ti5jz1#

在将post方法传递的变量输入数据库之前,必须获取这些变量。请尝试以下操作:
1) 这让php表单提交自己。

<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
Nome:
<input type="CHAR" name="nome">
<br><br>E-Mail:
<input type="text" name= "email">
<br><br>Mensagem:<br>
<textarea name="comentario"></textarea>
<br><br>
<input type="submit" value="Enviar">
<input type="reset" value="Limpar">
</form>

这部分保留变量并将它们插入db中(假设您在tbcomentarios表中有data、nome、email、comentario列)

<?php
$nome = "";
$email = "";
$comentario = "";

//keep the variables
if(isset($_POST["nome"]))
     $nome = $_POST["nome"];
if(isset($_POST["email"]))
     $email = $_POST["email"];
if(isset($_POST["comentario"]))
     $comentario = $_POST["comentario"];

//current date
$data = date("Y/m/d");

$con=mysqli_connect("localhost","root","","bdcomentarios");
// Check connection
if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$sql_insert="INSERT INTO tbcomentarios (data, nome, email, comentario) 
VALUES('$data', '$nome', '$email', '$comentario')";

//check the insert into DB
if (mysqli_query($con,$sql_insert)) {
     echo "New record created successfully";
} else {
     echo "Error: " . $sql . "<br>" . mysqli_error($con);
}

$sql = "SELECT * FROM tbcomentarios ORDER BY id desc";
$executar=mysqli_query($con, $sql);
while( $exibir = mysqli_fetch_array($executar)){
    echo $exibir['data'];
    echo "</br>";
    echo $exibir['nome'];
    echo "</br>";
    echo $exibir['email'];
    echo "</br>";
    echo $exibir['comentario'];
    echo "</br><hr>";
}

相关问题