You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

将JavaScript变量score插入MySQL失败求助:曾测试可用现失效

Hey Jonas, let's break down why your score variable isn't making it to the MySQL database even though your functions are firing. Since it worked before but now fails, let's walk through common troubleshooting steps:

1. Uncover silent PHP errors

PHP often suppresses errors by default, which can hide the root cause. Add these lines at the top of Scoreadder.php to force error display:

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// Rest of your database code
?>

Then, add error handling to your JavaScript request (assuming you're using AJAX/fetch) to catch server-side issues:

// Example with fetch API
fetch('Scoreadder.php', {
  method: 'POST',
  body: JSON.stringify({ score: score }),
  headers: { 'Content-Type': 'application/json' }
})
.then(response => {
  if (!response.ok) throw new Error(`Server error: ${response.status}`);
  return response.text();
})
.then(data => console.log('Server response:', data))
.catch(err => console.error('Request failed:', err));

This will show you if there's a database connection failure, SQL syntax error, or permission issue.

2. Confirm the score value is reaching the server

Add a debug output at the start of Scoreadder.php to verify the variable is being received:

<?php
// Adjust based on how you're sending the data (POST/GET/JSON)
$score = $_POST['score'] ?? $_GET['score'] ?? json_decode(file_get_contents('php://input'), true)['score'] ?? 'No score received';
echo "Received score: " . $score;
// Proceed with database logic after checking this output
?>

Check your browser console for this output—if it says "No score received", you've got a mismatch between the parameter name in your JS and PHP (e.g., sending points but expecting score).

3. Validate your database connection & permissions

A working connection before doesn't mean it's still valid—credentials might have changed, or your database user lost permissions. Add explicit connection checks:

$conn = mysqli_connect('localhost', 'your_username', 'your_password', 'your_db_name');
if (!$conn) {
  die("Connection failed: " . mysqli_connect_error());
}
// Also verify the user has INSERT/UPDATE permissions for your table

4. Fix your SQL query (use prepared statements!)

Hardcoding variables into SQL is risky and prone to syntax errors (e.g., missing quotes for string values). Switch to prepared statements to avoid both:

// Example for inserting a numeric score
$stmt = mysqli_prepare($conn, "INSERT INTO your_table_name (score_column) VALUES (?)");
mysqli_stmt_bind_param($stmt, "i", $score); // "i" = integer, use "s" for strings
if (mysqli_stmt_execute($stmt)) {
  echo "Score inserted successfully";
} else {
  echo "Error: " . mysqli_stmt_error($stmt);
}

This eliminates syntax issues from variable values and prevents SQL injection.

5. Check for request interruptions

If you're using a form submit, make sure you're preventing the default page reload in your JS:

document.querySelector('#your-form').addEventListener('submit', (e) => {
  e.preventDefault(); // Stop the page from reloading
  // Your code to send the score here
});

A page reload can abort the AJAX request before it completes, making it look like nothing happened.

6. Check server logs

If none of the above works, check your web server's error logs (e.g., Apache's error.log or Nginx's error.log). These logs often contain fatal PHP errors or database connection issues that aren't displayed in the browser.

Try these steps one by one—chances are one of them will reveal the problem. If you find specific error messages, feel free to share them for more targeted help!

内容的提问来源于stack exchange,提问作者Jonas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 07:54:12