使用 PHP 将数据插入 MSSQL DB

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25651788/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:55:58  来源:igfitidea点击:

Insert Data into MSSQL DB using PHP

phpsql-serverinsert-into

提问by user3315848

Hello there am trying to insert data into MSSQL using PHP. I have tried many times to figure out what the problem might be but i seem not to find it. Is there something am not getting right or missing?

您好,我正在尝试使用 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';
?>

I get this error message: Warning: mssql_query() [function.mssql-query]: message: Invalid object name 'dbo.CAGD Plan'.
(severity 16) in C:\xampp\htdocs\CAGD\leave_request.php on line 110

我收到此错误消息:警告:mssql_query() [function.mssql-query]:消息:对象名称“dbo.CAGD 计划”无效。
(严重性 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

回答by Jayakarthik Appasamy

First Specify your database Connection...

首先指定您的数据库连接...

mssql_connect('Server-PC','user','password','database')
like -> "localhost","root","XXXX", "DBNAME"

then query like

然后像这样查询

 $query = "INSERT INTO TABLENAME  (id,name) VALUES   
('$id','$name')";
$result = mssql_query($query,$dbc)

回答by Alireza

You can use SQLSRV Driver instead of MSSQL Driver and then try this

您可以使用 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);
?>

This is more useful, and you can learn more here:

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

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

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

回答by Cosmin Hutanu

Hmm, it seems to me that you have 7 fields in the table but only 6 values submitted - you are missing the value for the first column, [No_]. Besides, the last column satus(i suppose it should be 'status') does not have de []delimiters. The error returned tells you that the name of the table is wrong. And yes variable names are case sensitive in PHP, it should be $leave - best to exit the string and concatenate - something like "bla bla".$leave."anything here with spaces or not".

嗯,在我看来,您在表中有 7 个字段,但只提交了 6 个值 - 您缺少第一列的值[No_]. 此外,最后一列satus(我想它应该是“状态”)没有[]分隔符。返回的错误告诉您表的名称是错误的。是的,变量名在 PHP 中区分大小写,它应该是 $leave - 最好退出字符串并连接 - 类似于"bla bla".$leave."anything here with spaces or not".

回答by Marc B

Is this supposed to be a variable?

这应该是一个变量吗?

$query = "INSERT INTO dbo.[CAGD$Leave Plan] ([No_],[Employee No
                               ^^^^^^

If so, then it's apparently undefined in your code, and the generated query string will contain dbo.[CAGD Plan], and not whatever value was supposed to be in that variable. If the $is literally in your table name, then it should be CAGD\$Leave, so that $Leaveisn't treated as a variable.

如果是这样,那么它显然在您的代码中未定义,并且生成的查询字符串将包含dbo.[CAGD Plan],而不是该变量中应该包含的任何值。如果$您的表名中字面上是 ,则它应该是CAGD\$Leave,因此$Leave不会将其视为变量。