使用PHP将数据插入MSSQL DB [英] Insert Data into MSSQL DB using PHP

查看:76
本文介绍了使用PHP将数据插入MSSQL DB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,正在尝试使用PHP将数据插入MSSQL.我已经尝试了很多次以找出问题所在,但是我似乎没有找到它.有什么事情是对的还是错的?

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
//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)在第110行的C:\ xampp \ htdocs \ CAGD \ leave_request.php中

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

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驱动程序,然后尝试执行此操作

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

<?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

这篇关于使用PHP将数据插入MSSQL DB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆