增删改表单提示添加成功但数据未写入MySQL问题排查
Let's walk through why your form's "Add" action is showing a success message but not actually storing data in your user table, plus address your MySQLi vs. MySQL concern.
First: Critical Missing Piece – Where is add.php?
Your add.html form submits to add.php, but you didn't include this file in your code snippet. This is almost certainly the biggest issue right now! If add.php doesn't exist or isn't properly handling the INSERT logic, the success message you're seeing is either hardcoded somewhere (without actual database interaction) or coming from a broken script.
Example Working add.php (With Error Handling)
Create this file if it doesn't exist, and adjust it to match your setup:
<?php include_once("config.php"); if(isset($_POST['Submit'])) { // Grab form data $steamUE = $_POST['steamUE']; $steamPW = $_POST['steamPW']; $steamGC = $_POST['steamGC']; // Basic empty field check if(empty($steamUE) || empty($steamPW) || empty($steamGC)) { echo "<font color='red'>All fields are required!</font><br>"; echo "<a href='add.html'>Go back</a>"; exit(); } // Insert query $insertQuery = "INSERT INTO `user` (steamUE, steamPW, steamGC) VALUES ('$steamUE', '$steamPW', '$steamGC')"; $result = mysqli_query($mysqli, $insertQuery); // Check if insert succeeded if($result) { echo "Data added successfully."; // Redirect to homepage after 2 seconds header("Refresh: 2; url=index.php"); exit(); } else { // Show actual error to debug echo "Failed to add data: " . mysqli_error($mysqli); } } ?>
Check If Your Host Supports MySQLi
To confirm your host's support for MySQLi (and rule out your initial suspicion), create a simple test file named mysqli-test.php with this code:
<?php if (extension_loaded('mysqli') && function_exists('mysqli_connect')) { echo "✅ MySQLi is supported on your host!"; } else { echo "❌ Only the deprecated MySQL extension is available (not recommended for use)."; } ?>
Upload it to your host and visit it in your browser. If MySQLi is supported, move on to other checks. If not, you'd need to replace all mysqli_* functions with their old mysql_* equivalents (though this is strongly discouraged, as MySQL is deprecated and insecure).
Other Common Issues to Debug
Verify Database Connection (
config.php)
Double-check that yourconfig.phpusesmysqli_connect(notmysql_connect) and has the correct database credentials:<?php $mysqli = mysqli_connect("localhost", "your_username", "your_password", "your_database"); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); exit(); } ?>Confirm Table & Field Names Match
Make sure yourusertable actually has the fieldssteamUE,steamPW, andsteamGC(typos here are super common!). You can verify this directly in phpMyAdmin by checking the table structure.Check Database User Permissions
Ensure the database user you're using hasINSERT,SELECT,UPDATE, andDELETEpermissions for theusertable. You can adjust this in phpMyAdmin under the "Privileges" tab.Fix Typo in
edit.php
You have a duplicate label in your edit form – change the second "Steam Username" to "Steam Password":<tr> <td>Steam Password</td> <!-- Fixed from "Steam Username" --> <td><input type="text" name="steamPW" value="<?php echo $steamPW;?>"></td> </tr>
Final Steps
- Add the
add.phpscript above (or fix your existing one) with proper error handling. - Run the MySQLi support test to confirm your host's capabilities.
- Refresh phpMyAdmin after testing the "Add" action (sometimes cached views don't show new data immediately).
内容的提问来源于stack exchange,提问作者unnamedUser

