I am a beginner programmer and I am trying to figure out how the x-editable fields work before I can attempt to implement them in a website. I've seen a couple of examples around the web and even on here but the solutions I've found haven't worked for me. I have an index.php file with a test page that presents two sample fields (text and drop down selector). I was able to get data from a specific table row to show up in the fields. Here's my index.php code.
我是一名初学程序员,我试图弄清楚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>
I reference a connection.php file:
我引用了一个connection.php文件:
<?php
$dbCon = mysqli_connect("localhost", "#", "#", "#");
if (mysqli_connect_errno()) {
echo "Failed to connect: " . mysqli_connect_error();
}
?>
I am still wrapping my head around the js script and how it gets used. In the "url:" line, I referenced "save.php" which is supposed to update the table data in the database. Here's the js script, which is pretty much taken straight from the example file downloaded from the website:
我仍然围绕着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'
});
});
Everything seems to work just fine. The data from the specific row I'm calling (id=7) is showing up just fine. I can click on the field and it becomes an inline editable field. It'll hold the changes I make, but it doesn't stay that way when I reload the page and it doesn't update in the database table. I'm pretty sure that the issue lies in my save.php file:
一切似乎都很好。我正在调用的特定行(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'";
?>
The version that I have provided here is where I'm at. I've tried at least a dozen different things and then deleted them to try over again. I know this isn't a difficult thing to do, I'm not seeing it yet because I don't fully understand the programming language and its functions, hence why I'm going through this to figure it out. Eventually, this will be implemented in a larger project for a website. I'll worry about the security issues at before I get to that, first I need to figure out to get it working in the first place.
我在这里提供的版本就是我所在的版本。我已经尝试了至少十几个不同的东西然后删除它们再试一次。我知道这不是一件困难的事情,我还没有看到它,因为我不完全理解编程语言及其功能,因此我为什么要通过这个去解决它。最终,这将在一个更大的网站项目中实施。在我达到这个目标之前,我会担心安全问题,首先我需要弄清楚是否能让它在第一时间运行。
1 个解决方案
#1
0
In your code, there's no quote around $username
. Also, try to always use prepared statements if you're handling user submitted data.
在你的代码中,$ 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);
?>
#1
0
In your code, there's no quote around $username
. Also, try to always use prepared statements if you're handling user submitted data.
在你的代码中,$ 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);
?>