Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
926 views
in Technique[技术] by (71.8m points)

sql server - Insert Data into MSSQL DB using PHP

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

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

Warning: mssql_query() [function.mssql-query]: Query failed in C:xampphtdocs  
CAGDleave_request.php on line 110
Error querying MSSQL database
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...