2016-11-24 59 views
0

我是SQL Server的新手。我需要使用php,jQuery,ajax和T-SQL将数据插入数据库。我正在使用sqlsrv库,当我在mysql中运行此代码时它正常工作,但是当我将mysql更改为SQL Server时,它无法正常工作,并显示一些数组为字符串问题。如何将mysql代码更改为SQL Server以及如何使用sqlsrv检索数据?如何使用php,jquery和ajax插入数据到SQL Server数据库

这里是我的代码:

<?php 
    if(isset($_POST['first_name']) && isset($_POST['last_name']) && isset($_POST['email']) && isset($_POST['event1'])) 
    { 
     // include Database connection file 
     include("db_connection.php"); 

     // get values 
     $first_name = $_POST['first_name']; 
     $last_name = $_POST['last_name']; 
     $email = $_POST['email']; 
     $event1 = $_POST['event1']; 

     $query = "INSERT INTO demo(first_name, last_name, email, event1) VALUES('$first_name', '$last_name', '$email', '$event1')"; 
     if (!$result = sqlsrv_query($conn, $query)) 
     { 
      exit(sqlsrv_errors()); 
     } 
     echo "1 Record Added!"; 
    } 
?> 
+1

这看起来像一个很好的地方让你开始http://php.net/manual/en/book.sqlsrv.php – RiggsFolly

+1

'sqlsrv_errors() '返回一个数组,'exit()'接受一个字符串参数......这可能是你的*数组到字符串*错误来自;意味着您的查询失败。将其更改为'exit(print_r(sqlsrv_errors(),true));'这应该会提供一些关于查询无法正常工作的信息。 – ImClarky

+0

你可以用'db_connection.php'文件内容更新问题吗? –

回答

-2
<html> 
<title>h26t5hr5ge8dxbf6dit034hd8rt4fd</title> 
<body> 
<p> 
    function save_data() 
    { 
     var name=$("#name").val(); 
     var email=$("#email").val(); 
     var phone=$("#phone").val(); 
     var user_id=$("#user_id").val(); 
     var passwd=$("#passwd").val(); 

      $.ajax({ 
      url :'faculty-db.php', 
      type:'POST', 
      dataType:'html', 
      data:{ 
       'action':'save', 
       'name':name, 
       'email':email, 
       'phone':phone, 
       'user_id':user_id, 
       'passwd':passwd 
      }, 
      beforeSend:function(){ 
       $('#my_data').html('<img src="images/ajax-loader.gif" 

alt="Loading...">'); 
       //$('#btn_save').prop("disabled", true); 
      }, 
      async: false, 
      success :function(data){ 
       $('#my_data').html(data); 
       $('#myform')[0].reset(); 
       //$('#btn_save').prop("disabled", false); 
       show_data(); 
      } 
     }).responseText; 
    } 

    function show_data() 
    { 
     $.ajax({ 
      url :'faculty-db.php', 
      type:'POST', 
      dataType:'html', 
      data :{'action':'show'}, 
      beforeSend:function(){ 
       $('#my_data').html('<img src="images/ajax-loader.gif" 

alt="Loading...">'); 
      }, 
      async: false, 
      success :function(data){ 
       $('#my_data').html(data); 
      } 
     }).responseText; 
    } 

    function edit_data(faculty_id) 
    { 
     myWindow = window.open("faculty-edit.php? 

faculty_id="+faculty_id, "myWindow", "width=870,height=530"); 
     //alert(subject_id); 
    } 

    function delete_data(faculty_id) 
    { 
     $.ajax({ 
      url :'faculty-db.php', 
      type:'POST', 
      dataType:'html', 
      data :{ 
       'action':'del', 
       'faculty_id':faculty_id 
      }, 
      beforeSend:function(){ 
       $('#my_data').html('<img src="images/ajax-loader.gif" 

alt="Loading...">'); 
      }, 
      async: false, 
      success :function(data){ 
       $('#my_data').html(data); 

       show_data(); 
      } 
     }).responseText; 
    } 

    $(document).ready(function() { 
     // show_data(); 
    }); 
</p> 

<p> 
include_once("connection-pdo.php"); 

$action=$_REQUEST['action']; 
switch($action) { 

    case 'save': 
     $name = $_REQUEST['name']; 
     $email = $_REQUEST['email']; 
     $phone = $_REQUEST['phone']; 
     $user_id = $_REQUEST['user_id']; 
     $passwd = $_REQUEST['passwd']; 

     echo $sql = "INSERT INTO `faculty_master` 

(`name`,`email`,`phone`,`user_id`,`password`) VALUE 

('$name','$email','$phone','$user_id','$passwd')"; 
     $query = $pdoconn->prepare($sql); 
     $query->execute(); 

     break; 

    case 'show': 
     $html=''; 
     $html.='<'table> 
      <'tr> 
       <'th>ID</th> 
       <'th>NAME</th> 
       <'th>EMAIL</th> 
       <'th>PHONE</th> 
       <'th>USER ID</th> 
       <'th>PASSWORD</th> 
       <'th>EDIT</th> 
       <'th>DELETE</th>'; 


     $sql = "SELECT 

faculty_id,`name`,`email`,`phone`,`user_id`,`password` FROM 

`faculty_master` WHERE del_flag=0"; 
     $query = $pdoconn->prepare($sql); 
     $query->execute(); 
     $arr_faculty = $query->fetchAll(PDO::FETCH_ASSOC); 
     $slno=1; 
     foreach($arr_faculty as $val) { 
      $name = $val['name']; 
      $email = $val['email']; 
      $phone = $val['phone']; 
      $user_id = $val['user_id']; 
      $passwd = $val['password']; 


      $html .= '<'/tr> 
         <'tr> 
          <'td>'.$slno.'</td> 
          <'td>'.$name.'</td> 
          <'td>'.$email.'</td> 
          <'td>'.$phone.'</td> 
          <'td>'.$user_id.'</td> 
          <'td>'.$passwd.'</td> 
          <'td> <img src="images/edit.png" 

style="cursor: pointer" onclick="edit_data('.$val 

['faculty_id'].')"></td> 
          <'td> <img src="images/delete.png" 

style="cursor: pointer" onclick="delete_data('.$val 

['faculty_id'].')"></td> 
         <'/tr>'; 

      $slno++; 

     } 

     $html.='<'/table>'; 
     echo $html; 
     break; 

    case 'del': 
     $faculty_id=$_REQUEST['faculty_id']; 
     $sql ="UPDATE `faculty_master` SET `del_flag`=1 WHERE 

`faculty_id`=$faculty_id"; 
     $query = $pdoconn->prepare($sql); 
     $query->execute(); 
     break; 

    case 'update': 

     $faculty_id=$_REQUEST['faculty_id']; 
     $name=$_REQUEST['name']; 
     $email=$_REQUEST['email']; 
     $phone=$_REQUEST['phone']; 
     $user_id=$_REQUEST['user_id']; 
     $passwd=$_REQUEST['passwd']; 

     $sql ="UPDATE `faculty_master` SET 

`name`='$name',`email`='$email',`phone`='$phone',`user_id`='$user_id',` 

password`='$passwd' WHERE `faculty_id`=$faculty_id"; 
     $query = $pdoconn->prepare($sql); 
     $query->execute(); 
     if($query) 
      echo 'EDITED SUCCESSFULLY'; 
     else 
      echo 'ERROR WHILE EDITING...'; 
     break; 

} 
?> 
</p> 

<p> 
include_once("connection-pdo.php"); 
$faculty_id=$_REQUEST['faculty_id']; 
$sql = "SELECT `name`,`email`,`phone`,`user_id`,`password` FROM 

`faculty_master` WHERE del_flag=0 AND faculty_id=$faculty_id"; 
$query = $pdoconn->prepare($sql); 
$query->execute(); 
$arr_faculty = $query->fetchAll(PDO::FETCH_ASSOC); 
$name = $arr_faculty[0]['name']; 
$email = $arr_faculty[0]['email']; 
$phone = $arr_faculty[0]['phone']; 
$user_id = $arr_faculty[0]['user_id'd]; 
$passwd = $arr_faculty[0]['password']; 
</p> 
</body> 
</html> 
+2

添加描述此代码的工作原理。我认为你必须添加脚本和PHP标签 –

相关问题