我是初学者程序员,我试图弄清楚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'";
?>
我在这里提供的版本就是我所在的版本。我已经尝试了至少十几个不同的东西然后删除它们再试一次。我知道这不是一件困难的事情,我还没有看到它,因为我不完全理解编程语言及其功能,因此我为什么要通过这个来解决它。最终,这将在一个更大的网站项目中实施。在我达到这个目标之前,我会担心安全问题,首先我需要弄清楚是否能让它在第一时间运行。
答案 0 :(得分:0)
在您的代码中,$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);
?>