繁体   English   中英

X可编辑的简单数据库更新未更新

[英]X-editable simple database update not updating

我是一名初学者程序员,我试图弄清楚x可编辑字段的工作方式,然后才能尝试在网站中实现它们。 我在网上甚至在这里都看到了几个示例,但是我发现的解决方案不适用于我。 我有一个带有测试页的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周围没有引号。 另外,如果要处理用户提交的数据,请尝试始终使用准备好的语句

<?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);

?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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