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

Unity通过PHP向Azure SQL Server插入数据失败求助

Troubleshooting Azure SQL Insert Failure from PHP

Great news that your database connection is working and you’re able to read the GET parameters (the "Test84" output confirms that). Let’s break down the most likely reasons your insert isn’t hitting the table, with actionable fixes:

1. You forgot to execute the INSERT query

It’s super easy to prepare a query but skip the step to run it. For example, if you’re using the sqlsrv extension (the standard for Azure SQL in PHP):

// ❌ Wrong: Prepares but doesn't execute the query
$sql = "INSERT INTO your_table (col1, col2) VALUES (?, ?)";
$params = array($_GET['one'], $_GET['two']);
$stmt = sqlsrv_prepare($conn, $sql, $params);
// Missing sqlsrv_execute($stmt)!

// ✅ Correct: Add execution + error checking
$sql = "INSERT INTO your_table (col1, col2) VALUES (?, ?)";
$params = array($_GET['one'], $_GET['two']);
$stmt = sqlsrv_prepare($conn, $sql, $params);
if (sqlsrv_execute($stmt)) {
    echo "<br>Insert succeeded!";
} else {
    echo "<br>Insert failed: ";
    print_r(sqlsrv_errors(), true);
}

2. You’re not checking for errors

Right now, your script only outputs "Test84" but gives no feedback if the insert fails. Add error handling to see exactly what Azure SQL is complaining about—this will tell you if there’s a syntax typo, data type mismatch, or permission issue.

For sqlsrv, use sqlsrv_errors() to get detailed error messages. For PDO, use $stmt->errorInfo().

3. SQL syntax or data type mismatch

Double-check your INSERT statement for:

  • Typos in table/column names (even small typos like col1 vs col_1 will break the insert)
  • Correct data types: If your columns are integers, don’t wrap values in quotes; if they’re strings, make sure to use parameterized queries (never concatenate user input directly into the query—this is unsafe and causes syntax issues)
  • Missing commas or parentheses: For example, INSERT INTO table (col1 col2) VALUES (1,2) is invalid (missing comma between columns)

4. Autocommit is disabled (transaction not committed)

If you turned off autocommit for your connection, you need to explicitly commit the transaction after the insert:

// For sqlsrv
sqlsrv_begin_transaction($conn);
// ... run insert query ...
sqlsrv_commit($conn);

// For PDO
$pdo->beginTransaction();
// ... run insert query ...
$pdo->commit();

Without committing, the changes won’t be saved to the database.

5. Using the wrong PHP extension (common mistake!)

Azure SQL Server requires the sqlsrv or pdo_sqlsrv extension. If you’re using mysqli_connect() or PDO with a MySQL driver, that’s why the insert isn’t working—those tools are for MySQL databases, not Microsoft SQL Server.

Check your connection code:

  • ✅ Correct for Azure SQL: sqlsrv_connect($serverName, $connectionOptions)
  • ❌ Wrong: mysqli_connect(...) (this is for MySQL only)

6. Permissions (unlikely but worth ruling out)

Even if you can run other SQL commands, confirm your database user has INSERT permissions on the specific table. You can grant this via Azure Portal or SSMS with:

GRANT INSERT ON your_table TO your_database_user;

Quick Debug Script

Modify your PHP file to include error checking—this will immediately show you the problem:

<?php
$serverName = "your-server.database.windows.net";
$connectionOptions = array(
    "Database" => "your-db-name",
    "Uid" => "your-username",
    "PWD" => "your-password"
);

// Establish connection
$conn = sqlsrv_connect($serverName, $connectionOptions);
if ($conn === false) {
    die("Connection failed: " . print_r(sqlsrv_errors(), true));
}

// Read GET params
$one = $_GET['one'];
$two = $_GET['two'];
echo "Test" . $two . $one; // Your existing output

// Attempt insert with error handling
$sql = "INSERT INTO your_table_name (column_one, column_two) VALUES (?, ?)";
$params = array($one, $two);
$stmt = sqlsrv_prepare($conn, $sql, $params);

if (sqlsrv_execute($stmt)) {
    echo "<br>Success! Row inserted.";
} else {
    echo "<br>Insert failed: ";
    print_r(sqlsrv_errors(), true);
}

// Clean up
sqlsrv_free_stmt($stmt);
sqlsrv_close($conn);
?>

Run your test URL again, and the error message will point you directly to the issue.


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 04:12:49