Php 数据库mysql的输入失败

Php 数据库mysql的输入失败,php,mysql,mysqli,Php,Mysql,Mysqli,我想询问数据库的输入数据 <?php include "koneksi.php"; if(isset($_POST['daftar'])){ $daftar = mysqli_query($conn, "INSERT INTO tb_daftar VALUES ('".$_POST['id']."', '".$_POST['nama']."', '".$_POST['asal_sekolah']."', '".$_POST['

我想询问数据库的输入数据

  <?php
  include "koneksi.php";
  if(isset($_POST['daftar'])){
    $daftar = mysqli_query($conn, "INSERT INTO tb_daftar VALUES
    ('".$_POST['id']."',
      '".$_POST['nama']."',
      '".$_POST['asal_sekolah']."',
      '".$_POST['jenis_kelamin']."',
      '".$_POST['nama_ayah']."',
      '".$_POST['nama_ibu']."',
      '".$_POST['alamat']."',
      '".$_POST['no_hp']."',
      '')");
      if($daftar){
        $pesan1 = "Berhasil daftar";
        echo "<script type='text/javascript'>alert('$pesan1');</script>";
      }else{
        $pesan2 = "Gagal daftar";
        echo "<script type='text/javascript'>alert('$pesan2');</script>";
      }
  }
 ?>

使用mysqli\u error查看mysql查询中的错误

else{
    $pesan2 = mysqli_error($conn);
    echo "<script type='text/javascript'>alert('Error: '+$pesan2);</script>";
  }
else{
$pesan2=mysqli_错误($conn);
回显“警报('错误:'+$pesan2);”;
}

最后一个值后面有一个额外的逗号。您还应该使用准备好的语句来防止SQL注入

if ($dafter = mysqli_prepare($conn, "INSERT INTO tb_dafter VALUES (?, ?, ?, ?, ?, ?, ?, ?)")) {
    mysqli_stmt_bind_param($dafter, "ssssssss", $_POST['id'], $_POST['nama'], $_POST['asal_sekolah'], $_POST['jenis_kelamin'], $_POST['nama_ayah'], $_POST['nama_ibu'], $_POST['alamat'], $_POST['no_hp']);
    mysqli_stmt_execute($dafter);
    $pesan1 = "Berhasil daftar";
    echo "<script type='text/javascript'>alert('$pesan1');</script>";
} else {
    $pesan2 = htmlentities(mysqli_error($conn));
    echo "<script type='text/javascript'>alert('$pesan2');</script>";
}
if($dafter=mysqli\u prepare($conn,“插入tb\u数据值(?,,,,,,,,,,,?))){
mysqli_stmt_bind_param($dafter,$POST['id'],$POST['nama'],$POST['asal_sekolah'],$POST['jenis_kelamin'],$POST['nama_ayah'],$POST['nama_ibu'],$POST['alamat'],$POST['alamat],$POST['no'hp');
mysqli_stmt_execute($dafter);
$pesan1=“Berhasil daftar”;
回显“警报('$pesan1');”;
}否则{
$pesan2=htmlentities(mysqli_错误($conn));
回显“警报('$pesan2');”;
}

您的代码状态不好,您需要从多个方面考虑

  • id
    这样的整数值将不在引号中
  • 如果您没有为列名提供表名,高风险而不提供列名,则序列很重要
  • 您的查询很容易注入
  • 您没有使用
    isset
    检查$\u POST变量值
  • 使用mysqli或pdo来覆盖您的风险

    但我建议使用mysqli或pdo插入。以下是了解mysqli的一些链接:


    删除最后一个逗号“$\u POST['no\u hp']”,
    @SimoneNigro-no他不能。请再次检查,它是“.$\u POST['no\u hp'.]”,“'.”@AmitGupta他是对的。如果OP使用的是事先准备好的声明,那就容易多了。。。使用mysqli_error($con)检查错误我经常做的一件事是列出要插入的列,这有助于调试和检查我是否将正确的值插入正确的列。这是一个解决方案,因为他需要找出数据未插入数据库的原因调试步骤回答了如何解决问题,即使只是第一步,也没有显示任何错误,刚刚在这里刷新的页面我的表单代码:@RafiPriatnaKasbiantoro是原始代码,带有
    if(isset($\u POST['daftar')){
    在同一台计算机上page@JoshKisb是的,我只有2个文件:KokEsisi.PHP和Dex.P.P.考虑移动到CoDeEvIEW.StAccExchange(com)。在这种情况下,这种回答是受欢迎的。但是,在堆栈溢出上,在发布的代码上的一个普遍的默解是“SSSSSS”的主题。?参数类型列表。
    s
    =string,
    i
    =integer。是否尝试阅读文档?保存数据库时缺少冒号信息。(数据库列)应按数据冒号顺序输入。
     $daftar = mysqli_query($conn, "INSERT INTO tb_daftar((database columns)) 
      VALUES
    ('".$_POST['id']."',
      '".$_POST['nama']."',
      '".$_POST['asal_sekolah']."',
      '".$_POST['jenis_kelamin']."',
      '".$_POST['nama_ayah']."',
      '".$_POST['nama_ibu']."',
      '".$_POST['alamat']."',
      '".$_POST['no_hp']."',
      '')");