Android端SQLite数据经JSON传PHP接口写入MySQL失败问题
Hey there! Let's figure out why your Android app isn't throwing errors but your PHP interface isn't writing data to MySQL. Here's a step-by-step breakdown of the most likely issues and fixes:
1. Fix PDO Connection Issues & Enable Error Reporting
First up, I notice a potential variable mismatch in your code snippet: you defined $mot_passe='' but tried to use $m... (I assume that's a typo for $mot_passe) when initializing PDO. Even if that's not the case, PDO defaults to silent failures, so you won't see errors unless you explicitly enable them.
Update your connection code like this:
$hote='mysql:host=localhost;port=3306;dbname=gedimat'; $utilisateur='root'; $mot_passe=''; try { $connexion = new PDO($hote, $utilisateur, $mot_passe); // Enable exception mode to catch connection/SQL errors $connexion->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch(PDOException $e) { die("Connection failed: " . $e->getMessage()); }
This will immediately tell you if there's a problem connecting to your database.
2. Verify JSON Data is Being Received Correctly
Just because your Android app doesn't throw errors doesn't mean the data is reaching your PHP interface correctly. Add debug code to check what's actually being sent:
If Android sends data as application/json (raw JSON):
// Capture raw JSON input $rawJson = file_get_contents('php://input'); // Log the data to a file for debugging file_put_contents('json_debug.log', $rawJson . PHP_EOL, FILE_APPEND); // Decode the JSON $data = json_decode($rawJson, true); // Check if decoding failed if(json_last_error() !== JSON_ERROR_NONE) { die("Invalid JSON: " . json_last_error_msg()); }
If Android uses application/x-www-form-urlencoded:
// Log POST data file_put_contents('post_debug.log', print_r($_POST, true) . PHP_EOL, FILE_APPEND); // Check if the expected keys exist in $_POST if(!isset($_POST['your_expected_key'])) { die("Missing required data"); }
3. Validate Your SQL Insert Logic
You didn't share your SQL execution code, but this is a common failure point. Always use prepared statements to avoid syntax errors and SQL injection:
// Replace with your actual table and column names $stmt = $connexion->prepare("INSERT INTO your_table (column1, column2) VALUES (:val1, :val2)"); // Bind values from your decoded JSON/POST data $stmt->bindParam(':val1', $data['key1']); // Or $_POST['key1'] if using form data $stmt->bindParam(':val2', $data['key2']); try { $stmt->execute(); // Check how many rows were inserted echo "Rows inserted: " . $stmt->rowCount(); } catch(PDOException $e) { die("SQL Error: " . $e->getMessage()); }
Make sure your column names match exactly what's in your MySQL table, and that all required fields are included.
4. Check PHP Error Logs
Enable full error reporting in PHP to catch hidden issues:
error_reporting(E_ALL); ini_set('display_errors', 1);
You can also check your server's PHP error log (usually in /var/log/apache2/ or /var/log/nginx/ depending on your web server) for any uncaught errors.
5. Confirm Android Request Details
- IP Address: If your PHP interface is running on your local machine, the Android emulator can't use
localhost—use10.0.2.2instead to access your host machine's loopback address. - Request Method: Ensure your Android app is using the correct HTTP method (POST, most likely) and that the content type matches how your PHP code is reading the data.
内容的提问来源于stack exchange,提问作者Timothée

