douluan8828 2019-02-19 18:46
浏览 66

将PHP 7变量插入SQL Server

I am sure this question has been asked. I have searched for the answer but I can't seem to find it. I have this code to connect then INSERT data into a MS Sql Database from PHP . It isn't working.

 <!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<?php
//Session Start
session_start();
include '/includes/dbconn.mssql.php';

ini_set('display_errors','On');
?>
<html>
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
    <div>

    </div>
</body>
<?php        

        $sql = "INSERT INTO 
agents(fname,lname,date,dl1,ul1,lat1,jit1,dl2,ul2,lat2,jit2,dl3,ul3,lat3,jit3,UCN,ISP,IP,mmake,mmodel,[user],pass,email,winver,modem,mon1mk,mon1mdl,mon2mk,mon2mdl,headset,hmake,hmodel,webcam,prospect,[current],planul,plandl,station) "
                    . "VALUES('Janice','Smith','2/15/2019','10','10','1','1','10','10','1','1','10','10','1','1','333689','Xfinity','10.0.0.1','Ubee','100','admin','password','jane.smith@companyinc.com','null','null','Acer','100','AOC','5','1','Plantronic','54','1','0','1','150','50','28')";
            //      $sql = "INSERT INTO agents(fname,lname,date,dl1,ul1,lat1,jit1,dl2,ul2,lat2,jit2,dl3,ul3,lat3,jit3,UCN,ISP,IP,mmake,mmodel,user,pass,email,winver,modem,mon1mk,mon1mdl,mon2mk,mon2mdl,workspace,monitor1,monitor2,headset,hmake,hmodel,webcam,prospect,current,planul,plandl,station) VALUES('$fname','$lname','$date','$dl1','$ul1','$lat1','$jit1','$dl2','$ul2','$lat2','$jit2','$dl3','$ul3','$lat3','$jit3','$UCN','$ISP','$IP','$mmake','$mmodel','$user','$pass','$email','$winver','$modem','$mon1mk','$mon1mdl','$mon2mk','$mon2mdl','$workspace','$monitor1','$monitor2','$headset','$hmake','$hmodel','$webcam','$prospect','$current','$planul','$plandl','$station')";
            $result = sqlsrv_query($conn,$sql);
            $echo = sqlsrv_rows_affected;
            if(!$result) {
    echo 'Your code failed. $echo';
}
else {
    echo 'Success! $echo'; 
}
?>
</html>

When i run the code in Edge it says

Fatal error: Call to undefined function sqlsrv_query() in .... line 34

Line 34 being

$result = sqlsrv_query($conn,$sql);

What am i doing wrong?

  • 写回答

1条回答 默认 最新

  • dongxiaoshe0737 2019-02-26 18:52
    关注

    I got it to insert data into the cells with data and with variables.

         <?php
    
       $uploadDir = 'pics/';
    
       if(isset($_POST['upload'])){ 
    
           $fileName = $_FILES['monitor1']['name'];
           $tmpName  = $_FILES['monitor1']['tmp_name'];
           $fileSize = $_FILES['monitor1']['size'];
           $fileType = $_FILES['monitor1']['type'];
    
           $filePath = $uploadDir . $fileName;
    
           $result = move_uploaded_file($tmpName, $filePath);
    
           if (!$result) {
               echo "Error uploading <strong>file</strong>";
               exit;
    }
    
    
           $sql =  "INSERT INTO agents(fname,lname,employee_status,dl1,ul1,lat1,jit1,monitor1) "
                   . "  VALUES('".$_POST['fname']."',
                            '" .$_POST['lname']."',
                            '" .$_POST['status']."',
                            '" .$_POST['dl1']."',
                            '" .$_POST['ul1']."',
                            '" .$_POST['lat1']."',
                            '" .$_POST['jit1']."',
                            '" .$_FILES['monitor1']."')";                            
    
                  $result = sqlsrv_query($conn,$sql);
    
                  if( $result === false ) {
     die( print_r( sqlsrv_errors(), true));
    }
    
       }
    
    ?>
    
    评论

报告相同问题?