Insert Data into MSSQL DB using PHP

只谈情不闲聊 提交于 2019-11-28 10:36:53

问题


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:\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

回答1:


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)



回答2:


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




回答3:


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" .




回答4:


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 $Leave isn't treated as a variable.



来源:https://stackoverflow.com/questions/25651788/insert-data-into-mssql-db-using-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!