Insert Data into MSSQL DB using PHP

后端 未结 4 1020
轻奢々
轻奢々 2020-12-22 03:37

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 n

相关标签:
4条回答
  • 2020-12-22 04:11

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

    0 讨论(0)
  • 2020-12-22 04:26

    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.

    0 讨论(0)
  • 2020-12-22 04:30

    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)
    
    0 讨论(0)
  • 2020-12-22 04:31

    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

    0 讨论(0)
提交回复
热议问题