简体   繁体   中英

How to insert data into SQL Server database using php, jquery and ajax

I'm new to SQL Server. I need to insert data into the database using php, jQuery, ajax and T-SQL. I'm using sqlsrv library, when I'm running this code in mysql it's working properly, but when I'm changing mysql to SQL Server, it's not working properly, and showing some array to string problem. How can I change mysql code to SQL Server and how to retrieve data using sqlsrv ?

Here is my code:

<?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!";
    }
?>
<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM