Insert Data into MSSQL DB using PHP

谁说我不能喝 提交于 2019-11-29 16:40:45

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)
Alireza

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

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

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.

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