如何使用php、jquery和ajax将数据插入SQL Server数据库
我是SQL Server的新手。我需要使用php、jQuery、ajax和T-SQL将数据插入数据库。我使用的是如何使用php、jquery和ajax将数据插入SQL Server数据库,php,jquery,sql,sql-server,ajax,Php,Jquery,Sql,Sql Server,Ajax,我是SQL Server的新手。我需要使用php、jQuery、ajax和T-SQL将数据插入数据库。我使用的是sqlsrvlibrary,当我在mysql中运行此代码时,它工作正常,但当我将mysql更改为SQL Server时,它工作不正常,并显示一些数组到字符串的问题。如何将mysql代码更改为SQL Server,以及如何使用sqlsrv检索数据 这是我的密码: <?php if(isset($_POST['first_name']) && isset($
sqlsrv
library,当我在mysql中运行此代码时,它工作正常,但当我将mysql更改为SQL Server时,它工作不正常,并显示一些数组到字符串的问题。如何将mysql代码更改为SQL Server,以及如何使用sqlsrv
检索数据
这是我的密码:
<?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!";
}
?>
h26t5hr5ge8dxbf6dit034hd8rt4fd
函数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',
类型:'POST',
数据类型:'html',
数据:{
“操作”:“保存”,
“名称”:名称,
“电子邮件”:电子邮件,
“电话”:电话,
“用户id”:用户id,
“passwd”:passwd
},
beforeSend:function(){
$('my#u data').html('');
//$('btn_save').prop(“disabled”,true);
},
async:false,
成功:功能(数据){
$('my#u data').html(数据);
$('#myform')[0].reset();
//$('btn_save').prop(“disabled”,false);
显示_数据();
}
}).responseText;
}
函数show_data()
{
$.ajax({
url:'faculty-db.php',
类型:'POST',
数据类型:'html',
数据:{'action':'show'},
beforeSend:function(){
$('my#u data').html('');
},
async:false,
成功:功能(数据){
$('my#u data').html(数据);
}
}).responseText;
}
功能编辑数据(教员id)
{
myWindow=window.open(“faculty-edit.php?
教员id=“+教员id,”我的窗户”,“宽度=870,高度=530”);
//警报(受试者id);
}
函数删除\u数据(教员id)
{
$.ajax({
url:'faculty-db.php',
类型:'POST',
数据类型:'html',
数据:{
'动作':'del',
“教员id”:教员id
},
beforeSend:function(){
$('my#u data').html('');
},
async:false,
成功:功能(数据){
$('my#u data').html(数据);
显示_数据();
}
}).responseText;
}
$(文档).ready(函数(){
//显示_数据();
});
包括_once(“connection pdo.php”);
$action=$_请求['action'];
开关($动作){
“保存”案例:
$name=$_请求['name'];
$email=$_请求['email'];
$phone=$_请求['phone'];
$user\u id=$\u请求['user\u id'];
$passwd=$_请求['passwd'];
echo$sql=“插入到“教员”中`
(`name`、`email`、`phone`、`user\u id`、`password`)值
(“$name”、“$email”、“$phone”、“$user_id”、“$passwd”)”;
$query=$pdoconn->prepare($sql);
$query->execute();
打破
案例“展示”:
$html='';
$html.='
名称
电话
暗语
删除';
$sql=“选择
教员id、`name`、`email`、`phone`、`user\u id`、`password`FROM
`教员/硕士`其中del_标志=0”;
$query=$pdoconn->prepare($sql);
$query->execute();
$arr_faculty=$query->fetchAll(PDO::FETCH_ASSOC);
$slno=1;
foreach($val){
$name=$val['name'];
$email=$val['email'];
$phone=$val['phone'];
$user_id=$val['user_id'];
$passwd=$val['password'];
$html.='
“.$name。”
“.$phone。”
“.$passwd。”
';
echo$html;
打破
案例“del”:
$faculty\u id=$\u请求['faculty\u id'];
$sql=“UPDATE`faculty\u master`SET`del\u flag`=1,其中
`教员id`=$教员id”;
$query=$pdoconn->prepare($sql);
$query->execute();
打破
案例“更新”:
$faculty\u id=$\u请求['faculty\u id'];
$name=$_请求['name'];
$email=$_请求['email'];
$phone=$_请求['phone'];
$user\u id=$\u请求['user\u id'];
$passwd=$_请求['passwd'];
$sql=“更新`教员\教师`设置
`name`='$name'、`email`='$email'、`phone`='$phone'、`user\u id`='$user\u id'、`
密码“='$passwd”,其中“教员id”=$教员id”;
$query=$pdoconn->prepare($sql);
$query->execute();
如果($query)
echo“编辑成功”;
其他的
回显“编辑时出错…”;
打破
}
?>
包括_once(“connection pdo.php”);
$faculty\u id=$\u请求['faculty\u id'];
$sql=“从中选择'name'、'email'、'phone'、'user\u id'、'password'
`教员导师`其中del_flag=0,教员id=$教员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\u id=$arr\u faculty[0]['user\u id'd];
$passwd=$arr_[0]['password'];
这看起来是一个很好的开始sqlsrv\u errors()
返回一个数组,exit()
接受一个字符串参数…这可能就是数组到字符串错误的来源;这意味着查询失败。将其更改为exit(print\r(sqlsrv\u errors(),true))
这应该会为您提供一些关于查询不起作用的原因的信息。您能用db\u connection.php
文件内容更新问题吗?请添加此代码的工作原理的描述。I t
<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>