gpt4 book ai didi

php - 使用 PHP 将数据插入 MSSQL DB

转载 作者:行者123 更新时间:2023-12-02 21:24:13 24 4
gpt4 key购买 nike

您好,我正在尝试使用 PHP 将数据插入到 MSSQL 中。我已经尝试了很多次来找出问题所在,但我似乎没有找到它。是否有什么地方不对或遗漏?

 <?php
//pull form fields into php variables
$no = $_POST['no'];
$name= $_POST['name'];
$date = $_POST['date'];
$leave = $_POST['leave'];
$days= $_POST['days'];
$empno= $_POST['empno'];

//connect to sql
$dbc = mssql_connect('Server-PC','user','password','database')or die('Error connecting to
the SQL Server database.');

// Input into staff database
$query = "INSERT INTO dbo.[CAGD$Leave Plan] ([No_],[Employee No_],[Employee Name],
[Leave Name], [Start Date],[Leave Days],Satus) VALUES
('$no','$name','$leave','$date','days','empno')";
$r esult = mssql_query($query,$dbc)or die('Error querying MSSQL database');

//close to sql
mssql_close($dbc);

echo $name . 'Your submission has been received<br />';
echo 'If you need change this request please contact your HR Manager<br />';
echo 'Thank you <br />';
echo 'HR Manager';
?>

我收到此错误消息: 警告:mssql_query() [function.mssql-query]:消息:对象名称“dbo.CAGD Plan”无效。
(严重性 16)位于 C:\xampp\htdocs\CAGD\leave_request.php 第 110 行

Warning: mssql_query() [function.mssql-query]: Query failed in C:\xampp\htdocs  
\CAGD\leave_request.php on line 110
Error querying MSSQL database

最佳答案

您可以使用 SQLSRV 驱动程序而不是 MSSQL 驱动程序,然后尝试此操作

<?php 
$serverName = "serverName";
$options = array( "UID" => "sa", "PWD" => "Password", "Database" => "DBname");
$conn = sqlsrv_connect($serverName, $options);

if( $conn === false )
{
echo "Could not connect.\n";
die( print_r( sqlsrv_errors(), true));
}

$no = $_POST['no'];
$name= $_POST['name'];

$query = "INSERT INTO dbo.Test
(No_,FirstName)
VALUES(?, ?)";
$params1 = array($no,$name);
$result = sqlsrv_query($conn,$query,$params1);

sqlsrv_close($conn);
?>

这更有用,您可以在此处了解更多信息:

https://msdn.microsoft.com/en-us/library/cc626305(v=sql.105).aspx

关于php - 使用 PHP 将数据插入 MSSQL DB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25651788/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com