我试图将数据从PHP表单插入到远程SQL Server数据库中。
当我单击按钮提交时出现错误:
警告:mysqli_query()期望参数1为mysqli,在第30行的C:\ wamp64 \ www \ connection \ connection.php中给出的资源
和一个错误:无法建立连接。数组([0] => 数组([0] => 01000 [SQLSTATE] => 01000 [1] => 5701 [code] => 5701 [2] => [Microsoft] [用于SQL Server的ODBC驱动程序13] [SQL Server]已更改 数据库上下文到“工作流”。 [消息] => [Microsoft] [ODBC驱动程序 [SQL Server] [SQL Server] 13已更改数据库上下文为“工作流”。 )[1] =>数组([0] => 01000 [SQLSTATE] => 01000 [1] => 5703 [代码] => 5703 [2] => [Microsoft] [SQL Server的ODBC驱动程序13] [SQL Server]将语言设置更改为us_english。 [消息] => [Microsoft] [用于SQL Server的ODBC驱动程序13] [SQL Server]更改的语言 设置为us_english。 ))
我已经安装了ODBC驱动程序。
Connection.php:
<?php
$servername = "server_ip";
$connectionInfo=array( "Database"=>"Workflow", "UID"=>"admin",
"PWD"=>"pass");
$conn=sqlsrv_connect($servername, $connectionInfo);
$ID = $_POST['id'];
$Date = $_POST['date'];
$sql = "INSERT INTO T1 (ID,Date) VALUES ('$ID','$Date')";
if(mysqli_query($conn,$sql)) {
echo "Connection established.<br />";
}else{
echo "Connection could not be established.<br/>";
die(print_r(sqlsrv_errors(), true));
}
if(!mysqli_query($conn,$sql)) {
echo 'Data not inserted';
} else {
echo 'Data inserted';
}
?>
Index.html:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
<title>Form</title>
</head>
<body>
<form action="connection.php" method="post">
<div class="container">
ID: <input type="text" name="id">
Date: <input type="text" name="date">
</div>
<input type="submit" value="insert">
</form>
</body>
</html>
答案 0 :(得分:1)
您使用来自两个不同PHP扩展的功能-sqlsrv_
和mysqli_
。要连接到MS SQL Server,请使用sqlsrv_
函数,它们是PHP Driver for SQL Server的一部分。
另外,请考虑以下事项:
[]
包围列名称(如果它们是保留关键字)尝试使用此connection.php
:
<?php
# Connection
$servername = "server_ip";
$connectionInfo = array(
"Database"=>"Workflow",
"UID"=>"admin",
"PWD"=>"pass"
);
$conn = sqlsrv_connect($servername, $connectionInfo);
if( $conn === false ) {
echo "Error (sqlsrv_connect): ".print_r(sqlsrv_errors(), true);
exit;
}
# Parameters
$id = $_POST['id'];
$date = $_POST['date'];
$params = array(&$id, &$date);
# Statement
$sql = "INSERT INTO T1 (ID, [Date]) VALUES (?, ?)";
$stmt = sqlsrv_prepare($conn, $sql, $params);
if ($stmt === false) {
echo "Error (sqlsrv_prepare): ".print_r(sqlsrv_errors(), true);
exit;
}
if (sqlsrv_execute($stmt)) {
echo "Statement executed.\n";
} else {
echo "Error (sqlsrv_execute): ".print_r(sqlsrv_errors(), true);
}
# End
sqlsrv_free_stmt($stmt);
sqlsrv_close($conn);
?>