javascript - X-可编辑简单数据库更新不更新

标签 javascript php mysql sql x-editable

我是一名初学者程序员,在尝试在网站中实现 x-editable 字段之前,我试图弄清楚它们是如何工作的。我在网上甚至在这里看到了一些示例,但我找到的解决方案对我不起作用。我有一个 index.php 文件,其中包含一个测试页面,其中显示两个示例字段(文本和下拉选择器)。我能够从特定的表行获取数据以显示在字段中。这是我的index.php 代码。

<?php
	$dbCon = mysqli_connect("localhost", "#", "#", "#");
// Check connection
if (mysqli_connect_errno())
{
	echo "Failed to connect: " . mysqli_connect_error();
}
	$sql="SELECT * FROM user WHERE id ='7' ";
	$records=mysqli_query($dbCon,$sql);
	$user=mysqli_fetch_assoc($records)
?>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>X-editable Test Page</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- bootstrap -->
    <link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css" rel="stylesheet">
    <script src="http://code.jquery.com/jquery-2.0.3.min.js"></script> 
    <script src="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script>  
    <!-- x-editable (bootstrap version) -->
    <link href="//cdnjs.cloudflare.com/ajax/libs/x-editable/1.4.6/bootstrap-editable/css/bootstrap-editable.css" rel="stylesheet"/>
    <script src="//cdnjs.cloudflare.com/ajax/libs/x-editable/1.4.6/bootstrap-editable/js/bootstrap-editable.min.js"></script>
    <!-- main.js -->
    <script src="main.js"></script>
  </head>
  <body>
    <div class="container">
      <h1>Sample Editing Page</h1>
      <div>
        <span>Username:</span>
        <a href="#" id="username" data-type="text" data-placement="right" data-url="save.php" data-name="username" data-title="Enter username"><?php echo $user['username'];?> </a>
      </div>
      <div>
        <span>Country:</span>
        <a href="#" id="country" data-type="select" data-placement="right" data-url="save.php" data-name="country" data-title="Select country"><?php echo $user['country'];?></a>
      </div>
    </div>
  </body>
</html>

我引用了一个connection.php文件:

<?php
    $dbCon = mysqli_connect("localhost", "#", "#", "#");

    if (mysqli_connect_errno()) {
        echo "Failed to connect: " . mysqli_connect_error();
    }
?>

我仍在思考 js 脚本及其使用方式。在“url:”行中,我引用了“save.php”,它应该更新数据库中的表数据。这是 js 脚本,它几乎直接取自从网站下载的示例文件:

$(document).ready(function() {
    //toggle `popup` / `inline` mode
    $.fn.editable.defaults.mode = 'inline';  
	 
    
    //make username editable
    $('#username').editable({
		type: 'text',
		url: 'save.php',
		pk:7
		 
    }	
    );
    
    //make status editable
    $('#country').editable({
        type: 'select',
        title: 'Select status',
        placement: 'right',
        value: 1,
        source: [
            {value: 1, text: 'USA'},
            {value: 2, text: 'Australia'},
            {value: 3, text: 'Other'}
        ]
        /*
        //uncomment these lines to send data on server
		*/
        ,pk: 7
        ,url: 'save.php'
    });
});

一切似乎都进展顺利。我调用的特定行(id=7)中的数据显示得很好。我可以单击该字段,它就会变成内联可编辑字段。它会保留我所做的更改,但当我重新加载页面时它不会保持这种状态,并且不会在数据库表中更新。我很确定问题出在我的 save.php 文件中:

<?php
include("connection.php");

if(isset($_POST['username'])){
	$username=$_POST['username'];
	$country=$_POST['country'];
}

$mysql_query= "UPDATE user SET username=$username WHERE id='7'";

?>

我在这里提供的版本就是我所在的位置。我已经尝试了至少十几种不同的东西,然后删除它们再试一次。我知道这不是一件困难的事情,我还没有看到它,因为我不完全理解编程语言及其功能,因此为什么我要通过这个来弄清楚它。最终,这将在一个更大的网站项目中实现。在开始之前我会担心安全问题,首先我需要弄清楚如何让它正常工作。

最佳答案

在您的代码中,$username 周围没有引号。另外,尝试始终使用 prepared statements如果您正在处理用户提交的数据。

<?php
include("connection.php");

if(isset($_POST['username'])){
    $username=$_POST['username'];
}

$stmt = mysqli_prepare($dbCon, "UPDATE user SET username=? WHERE id=?");

//'si' stands for String and Integer, respectively for $username and id 7
mysqli_stmt_bind_param($stmt, 'si', $username, 7);

mysqli_stmt_execute($stmt);

?>

关于javascript - X-可编辑简单数据库更新不更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38359080/

相关文章:

javascript - Upsert 性能随着集合(文档数量)的增加而降低

javascript - 为什么即使prototype被覆盖, `obj instanceof ConstructorFunc`仍然返回true?

javascript - 如何停止在没有换行符的字符串中进行正则表达式捕获但仍全局捕获?

mysql - 如何确保 sql 更新中的某个字段大于其他字段

mysql - 通过列 View 而不是 mysql 中的行 View 返回结果

java - 数据库更新时实时重新加载

标签之间的 JavaScript RegEx 无法使用 console.log 工作

php - 来自mysql的分页数据表,自动刷新

php - 使用 PHP PEAR MAIL 发送多个 CC 和 BCC

php - 在 Mysql 中运行第二次提交的结果是什么?