报错:height列整数值无效,求排查(附PHP/JS相关代码)
Hey there! Let's break down why you're hitting that "invalid integer value '' for column height" error and fix it step by step:
核心问题根源
报错信息:第1行的height列存在无效整数值:''。
This error pops up because your database's height column is set to an integer type, but you're trying to insert an empty string ('') into it. Let's trace where this empty value comes from:
1. JavaScript代码不完整,未正确传递height值
Your provided JS code cuts off mid-implementation:
function height(){ var height = screen.height; document.getElem...
You've correctly grabbed the screen.height value, but didn't finish passing it to the backend. If you're using a form to submit data, you need to assign this value to a form field so PHP can pick it up. Here's a complete example:
// Rename the function to avoid naming conflicts with the variable function setScreenHeight() { const screenHeight = screen.height; // Assign to a hidden input field in your form const heightInput = document.getElementById('screenHeight'); if (heightInput) { heightInput.value = screenHeight; } } // Call this function when the page loads or right before form submission window.addEventListener('load', setScreenHeight);
2. PHP未正确接收/处理height值
In your original PHP code, the $height variable is used but it's unclear how you're retrieving it from the frontend. If the JS didn't send the value at all, $height would default to an empty string. Plus, directly concatenating variables into SQL is a huge security risk (SQL injection!) and can lead to type mismatches.
Fix this with these adjustments:
// Create connection (removed @ to see actual connection errors) $conn = mysqli_connect($server, $user, $pass, $dbname); if (!$conn) { die('Could not connect to database: ' . mysqli_connect_error()); } // Retrieve and sanitize values from the request (use $_GET if you're using GET method) $height = isset($_POST['screenHeight']) ? intval($_POST['screenHeight']) : 0; // Sanitize other string values too to prevent SQL injection $userName = mysqli_real_escape_string($conn, $_POST['userName']); $userAgent = mysqli_real_escape_string($conn, $_POST['userAgent']); $language = mysqli_real_escape_string($conn, $_POST['language']); $javaEnabled = isset($_POST['javaEnabled']) ? intval($_POST['javaEnabled']) : 0; // Use prepared statements to safely insert data and ensure type correctness $stmt = mysqli_prepare($conn, "INSERT INTO test (userName, userAgent, language, javaEnabled, height) VALUES (?, ?, ?, ?, ?)"); mysqli_stmt_bind_param($stmt, "sssii", $userName, $userAgent, $language, $javaEnabled, $height); if (mysqli_stmt_execute($stmt)) { echo "Record inserted successfully"; } else { die("Error inserting record: " . mysqli_stmt_error($stmt)); } // Clean up resources mysqli_stmt_close($stmt); mysqli_close($conn);
3. Optional: Database column adjustment
Double-check your test table's height column definition. If it's set to NOT NULL without a default value, you must provide a valid integer every time. If empty values are acceptable for your use case, you could set the column to allow NULL and adjust the PHP code to insert NULL instead of 0 when no value is provided.
内容的提问来源于stack exchange,提问作者frraj

