簡體   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