无法将PHP数据传入MySQL:表只读错误排查求助
First, let's zero in on the exact error you're hitting—it's telling us exactly where the problem lies:
Fatal error: Uncaught exception 'mysqli_sql_exception' with message 'Table 'daftarpelanggan' is read only'
Your database connection works (since you aren't getting a "connection failed" error), and you've confirmed the database/table names are correct. The issue boils down to the table not allowing write operations (like your INSERT query). Here are the most common fixes to resolve this:
1. Check and Restore MySQL User Permissions
Even though you're using the root user (which normally has full access), it's possible the permissions for the daftarpelanggan table were restricted accidentally. To fix this:
- Log into your MySQL server via command line or a tool like phpMyAdmin:
mysql -u root -p - Run these commands to grant explicit write permissions to your user for the table:
USE pelanggan; GRANT INSERT, UPDATE, DELETE ON daftarpelanggan TO 'root'@'localhost'; FLUSH PRIVILEGES;
2. Fix System File Permissions for MySQL Data Files
On Linux/macOS, MySQL stores table files (like .frm or .ibd for InnoDB tables) in its data directory (usually /var/lib/mysql/pelanggan/ for your pelanggan database). If these files don't have write access for the MySQL process user (typically mysql), the table will be locked to read-only.
- Open a terminal and navigate to your database's data folder:
cd /var/lib/mysql/pelanggan/ - Check the owner and permissions of the table files:
ls -l - If the owner isn't
mysql, correct it with:sudo chown -R mysql:mysql /var/lib/mysql/pelanggan/ - Ensure the files have write permissions for the owner:
sudo chmod 660 /var/lib/mysql/pelanggan/daftarpelanggan.*
3. Disable Read-Only Mode on the Table
It's possible the table was explicitly marked as read-only via a SQL command. To check and revert this:
- Log into MySQL and run:
SHOW TABLE STATUS LIKE 'daftarpelanggan'; - Look for the
Read_onlycolumn. If it showsYES, turn off read-only mode with:ALTER TABLE daftarpelanggan READ ONLY = OFF;
Bonus: Fix Critical SQL Injection Risk
While unrelated to your current error, your insert code has a major security flaw. Never directly plug user input (like $_POST values) into SQL queries—use prepared statements instead to protect against SQL injection:
// Replace your existing insert logic with this if (isset($_POST['nama'], $_POST['telp'], $_POST['paket'])) { $nama = $_POST['nama']; $telp = $_POST['telp']; $paket = $_POST['paket']; if (empty($nama) || empty($telp) || empty($paket)) { echo '*FIELDS MUST BE FILLED'; } else { $stmt = mysqli_prepare($conn, "INSERT INTO daftarpelanggan (nama,telp,paket) VALUES (?,?,?)"); mysqli_stmt_bind_param($stmt, "sss", $nama, $telp, $paket); if(mysqli_stmt_execute($stmt)){ echo '1 record has been added successfully'; } else { die('Error: ' . mysqli_stmt_error($stmt)); } } }
内容的提问来源于stack exchange,提问作者hendry

