Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在PHP或Jquery中从前端更改数据库值?_Php_Jquery_Mysql - Fatal编程技术网

如何在PHP或Jquery中从前端更改数据库值?

如何在PHP或Jquery中从前端更改数据库值?,php,jquery,mysql,Php,Jquery,Mysql,我有php代码,它显示一个表,其中的内容显示在数据库中的一个表中。其中一个字段是状态字段。有一个“更改”按钮,可以将“非活动”状态更改为“活动”,反之亦然 我需要知道如何着手编写“更改”按钮的代码。它是应该用jquery编写,还是也可以用php编写 代码如下: <!doctype html> <html> <head> <meta charset="utf-8"> <link rel="stylesheet" type="text/css"

我有php代码,它显示一个表,其中的内容显示在数据库中的一个表中。其中一个字段是状态字段。有一个“更改”按钮,可以将“非活动”状态更改为“活动”,反之亦然

我需要知道如何着手编写“更改”按钮的代码。它是应该用jquery编写,还是也可以用php编写

代码如下:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="indexStyle.css">
<title>User List</title>
</head>
<body>
<table border="2" bgcolor="#50D5CE">
<tr>
    <td>SL. No</td>
    <td>Name</td>
    <td>Mobile Number</td>
    <td>Status</td>    
 </tr>
<?php include ('header.php');
include ('includes/connect.php');
$dbconn =  new connect();
$fields = "first_name,last_name,mobile_number,status";
$user_details = $dbconn->select($fields,"user_details","1");
$c=1;
for($i = 0; $i < count($user_details);$i++){
?>
<tr>
<td><?php echo $c ?></td>
<td><?php echo $user_details[$i][0]." ".$user_details[$i][1]; ?></td>
<td><?php echo $user_details[$i][2] ?></td>
<td><?php if($user_details[$i][3] == 0) {
echo "Inactive"; } 
else echo "Active"; ?>
<input type="button" name="change_status" id ="change_status" value="Change"/></td>
</tr>
<?php $c++;
} ?>
</table>

</body>
</html>

用户列表
序号
名称
手机号码
地位

jQuery是Javascript,在用户计算机上运行,而PHP在服务器上运行。问题是,用户必须触发数据库更新,服务器必须执行它。因此,您需要使用jQuery发送AJAX请求,服务器端必须执行更新。

您可以使用
PHP
代码或
jQuery和PHP
代码对其进行更改

这是因为您需要一个服务器端语言(在您的例子中是PHP)来访问数据库。因此需要服务器端脚本

要运行服务器端脚本,您可以直接调用php页面,也可以从AJAX请求(在您的例子中是jQuery)调用它。

更改输入:↓

“更改”按钮必须触发对PHP代码的AJAX调用(或排序),比如“更新ID”(其中ID是数字),PHP代码必须连接到db并将ID的值更新为您想要的任何值。1。如果我们使用的是jquery脚本,那么为什么我们需要在输入标记中使用onClick()事件呢?2.没有使用表单,我仍然可以通过ajax传递数据吗?
  change input: ↓
 <input type="button" name="change_status" id ="change_status" value="Change" onclick="update_status(<?php echo $user_details[$i][2]; ?>,<?php echo $user_details[$i][3]; ?>)"/>

  <script>
           function update_status(mob_no,sta)
           {

                  $.ajax({
                        type: "POST",
                        url: "update_status.php",
                        data: {mobno:mob_no,status:sta},
                        success: function(data){
                            // whatever you do here after success
                        }
           }
  </script>
     <?php
           if(isset($_POST['mobno']) && isset($_POST['status']))
           {
                 $mono=  $_POST['mobno'];
                 $temp_status = 0;
                 if($_POST['status']==0)
                     $temp_status=1;

                mysql_query("update user_details set status='$temp_status' where mobile_number ='$mono'");

           }
     ?>