PHP表单注册错误

时间:2018-02-03 02:41:07

标签: php mysql forms post mysqli

此处我的错误

  

警告:mysqli_affected_rows()期望参数1为mysqli,   给出的布尔值   第19行的C:\ xampp \ htdocs \ elearningthamescenter \ prosesdaftar.php数据   gagal disimpan ke dalam数据库

这里我的代码请帮忙谢谢

if (isset($_POST["daftar"])) {
  $id = $_POST["id"];
  $username = $_POST["username"];
  $password = $_POST["password"];
  $level = $_POST["level"];
  $grup = $_POST["grup"];
  $email = $_POST["email"];
  $nama = $_POST["nama"];

  $conn = mysqli_connect("localhost", "thames",
                         "goahead123", "elearningthamescenter");
  if (mysqli_connect_errno()) {
    echo "Koneksi ke server gagal";
    exit();
  }

  $sql = mysqli_query($conn, "INSERT INTO `user` (`id`, `username`, `password`, `level`, `grup`, `email`, `nama`) VALUES ('$id', '$username', '$password', '$level', '$grup', '$email', '$nama');");
  $num = mysqli_affected_rows($sql);

  if ($num > 0) {
    echo "Data yang Anda masukkan sudah disimpan.";
  } else {
    echo "Data gagal disimpan ke dalam database";
  }
}

1 个答案:

答案 0 :(得分:1)

mysqli_affected_rows() 函数需要连接到SQL,而不是语句变量。传递 "$conn" 而不是 "$sql" ,它应该可以正常工作。

请查看mysqli Affected-rows了解详情。