php 你能修复第45行的代码吗,因为我不能上传照片[关闭]

4urapxun  于 2023-10-15  发布在  PHP
关注(0)|答案(1)|浏览(137)

已关闭此问题为not reproducible or was caused by typos。它目前不接受回答。

此问题是由打印错误或无法再重现的问题引起的。虽然类似的问题可能是on-topic在这里,这一个是解决的方式不太可能帮助未来的读者。
8天前关闭
Improve this question
我希望这个代码没有错误

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Form Pengaduan</title>
</head>
<body>
    <h1>Form Pengaduan</h1>
    
    <?php
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        $nama = $_POST["nama"];
        $isi_laporan = $_POST["isi_laporan"];
        $status = "PENDING";

        // Validasi data (anda dapat menambahkan validasi lainnya)
        if (empty($nama) || empty($isi_laporan)) {
        } else {
            // Upload file gambar
            $foto = "";
            if ($_FILES["foto"]["error"] == UPLOAD_ERR_OK) {
                $target_dir = "uploads/"; // Direktori penyimpanan gambar
                $target_file = $target_dir . basename($_FILES["foto"]["name"]);

                if (move_uploaded_file($_FILES["foto"]["tmp_name"], $target_file)) {
                    $foto = $target_file;
                } else {
                    echo "<p>Terjadi kesalahan saat mengunggah gambar.</p>";
                }
            }

            // Koneksi ke database
            $mysqli =mysqli_connect("localhost", "root", "", "laporan");

            // Cek koneksi
            if ($mysqli->connect_error) {
                die("Koneksi ke database gagal: " . $mysqli->connect_error);
            }

            // Query untuk menyimpan pengaduan ke database
            $sql = "INSERT INTO pengaduan (nama, isi_laporan, foto, status) VALUES ('$nama', '$isi_laporan', '$foto', '$status')";
            $stmt = $mysqli->prepare($sql);

            // Binding parameter
            $stmt->bind_param("sss", $nama, $isi_laporan, $foto);

            // Eksekusi query
            if ($stmt->execute()) {
                echo "<p>Pengaduan berhasil disimpan!</p>";
                // Reset form
                $nama_pengadu = $isi_laporan = $foto = "";
            } else {
                echo "<p>Terjadi kesalahan saat menyimpan pengaduan: " . $stmt->error . "</p>";
            }

            // Tutup statement dan koneksi
            $stmt->close();
            $mysqli->close();
        }
    }
    ?>

    <form action="pengaduan.php" method="post" enctype="multipart/form-data">
        <label for="nama_pengadu">Nama Pengadu:</label>
        <input type="text" id="" name="nama" required><br>
        
        <label for="isi_laporan">Isi Laporan:</label>
        <textarea id="isi_laporan" name="isi_laporan" rows="4" required></textarea><br>
        
        <label for="foto">Foto:</label>
        <input type="file" id="foto" name="foto" accept="image/*"><br>
        
        <input type="submit" value="Kirim Pengaduan">
    </form>
</body>
</html>
Fatal error: Uncaught ArgumentCountError: The number of variables must match the number of parameters in the prepared statement in C:\xampp\htdocs\aduan\pengaduan.php:45 Stack trace: #0 C:\xampp\htdocs\aduan\pengaduan.php(45): mysqli_stmt->bind_param('sss', 'seyiaa', 'cek broo', 'uploads/bgpolos...') #1 {main} thrown in C:\xampp\htdocs\aduan\pengaduan.php on line 45
kmb7vmvb

kmb7vmvb1#

预准备语句要求查询中有占位符,用问号(?),而不是像上面那样的变量。
这应该是正确的代码:

// Query untuk menyimpan pengaduan ke database
$sql = "INSERT INTO pengaduan (nama, isi_laporan, foto, status) VALUES (?, ?, ?, ?)";
$stmt = $mysqli->prepare($sql);

// Binding parameter
$stmt->bind_param("sss", $nama, $isi_laporan, $foto);

相关问题