[英]Retrieve data from Mysql database, display on a form, and allow user to update using PHP
我對 PHP 很陌生。 我有一個頁面,我希望用戶能夠輸入引文,然后將該信息傳遞給一個 php 腳本,該腳本查詢數據庫,以表單形式返回信息,然后允許用戶更新任何字段以這種形式返回。
我有三個問題:
1) 當數據返回時,只返回字段中的第一個字。 許多字段包含多個單詞。
2)當用戶更改字段中的數據時,數據庫不會更新。
3)我似乎不知道如何讓表單字段像我輸入數據一樣顯示。
以下是查詢和返回數據以供用戶在必要時查看和更新的代碼:
<?php
mysql_connect("***************", "*********", "****") or die(mysql_error());
mysql_select_db("***********") or die(mysql_error());
$searchterm= $_POST['searchterm'];
$query = "SELECT Citation, Category, Overview, Facts, Decision, Keywords, Link FROM cases WHERE citation = '$searchterm'";
$result = mysql_query($query);
while ($row = mysql_fetch_assoc($result))
{
echo "<form action=".$_SERVER['PHP_SELF']." method=post>" .
"Case Citation: <input type=text name=Citation value={$row['Citation'] }><br>" .
"Category: <input type=text name=Category value={$row['Category'] }><br>" .
"Overview: <input type=text name=Overview value={$row['Overview'] }><br>" .
"Case Facts: <input type=text name=Facts value={$row['Facts'] }><br>" .
"Decision: <input type=text name=decision value={$row['Decision'] }><br>" .
"Keywords: <input type=text name=Keywords value={$row['Keywords'] }><br>" .
"Weblink: <input type=text name=Link value={$row['Link'] }><br>" .
"<input type=submit name=submit value=Update>" .
"</form>";
}
//when they click submit
if (isset($_POST['submit'])) {
$Citation=$_POST['Citation'];
$Category=$_POST['Category'];
$Overview=$_POST['Overview'];
$Facts=$_POST['Facts'];
$Decision=$_POST['Decision'];
$Keywords=$_POST['Keywords'];
$Link=$_POST['Link'];
$update = "UPDATE IGNORE cases SET citation='$citation', category='$category', overview='$overview', facts='$facts', decision='$decision', keywords='$keywords', link='$link' WHERE citation = '%$searchterm%'";
$add = mysql_query($update);
}
?>
這是我用來添加數據的表單:
<form action="process.php" method="post">
Case Citation: <input type="text" name="citation" size=128><br>
Category: <input type="text" name = "category" size=56><br>
Overview: <textarea class="textarea" cols="96" row="8" name = "overview"> </textarea><br>
Case Facts: <textarea class="textarea" cols="96" row="8" name = "facts"></textarea><br>
Decision: <input type="text" name = "decision" size=56><br>
Keywords: <textarea class="textarea" cols="96" row="8" name = "keywords"></textarea><br>
Web Link: <input type="text" name = "link" size=128><br>
<input type="submit" value="Submit">
</form>
這是將信息保存到數據庫的代碼:
<?
$citation=$_POST['citation'];
$category=$_POST['category'];
$overview=$_POST['overview'];
$facts=$_POST['facts'];
$decision=$_POST['decision'];
$keywords=$_POST['keywords'];
$link=$_POST['link'];
mysql_connect("*************", "************", "*********") or die(mysql_error());
mysql_select_db("************") or die(mysql_error());
mysql_query("INSERT INTO `cases` VALUES ('$citation', '$category', '$overview', '$facts', '$decision', '$keywords', '$link')");
Print "Your information has been successfully added to the database. Add case page will automatically reload.";
?>
您的第一個(可能還有其余的……)問題是由您構建表單的方式引起的:
echo "<form action=".$_SERVER['PHP_SELF']." method=post>" .
"Case Citation: <input type=text name=Citation value={$row['Citation'] }><br>" .
"Category: <input type=text name=Category value={$row['Category'] }><br>" .
"Overview: <input type=text name=Overview value={$row['Overview'] }><br>" .
"Case Facts: <input type=text name=Facts value={$row['Facts'] }><br>" .
"Decision: <input type=text name=decision value={$row['Decision'] }><br>" .
"Keywords: <input type=text name=Keywords value={$row['Keywords'] }><br>" .
"Weblink: <input type=text name=Link value={$row['Link'] }><br>" .
"<input type=submit name=submit value=Update>" .
"</form>";
請注意,所有屬性的值都未加引號,因此在 html 中,您的輸入之一可能如下所示:
Decision: <input type=text name=decision value=this is some text from that field><br>
這不是有效的 html。
您應該引用所有值並准備/轉義它們以在 html 中使用:
'Decision: <input type=text name=decision value="' . htmlspecialchars($row['Decision']) . '"><br>' .
etc.
除此之外,您還有一個 sql 注入問題,您應該通過切換到 PDO(或 mysqli)和帶有綁定變量的准備好的語句來解決該問題。
請注意,sql 注入問題不僅會使您處於危險之中,而且如果您的值之一包含例如'
字符,它也很容易使您的 sql 無效。
在 while 循環中檢查 value={$row['Citation'] }。
你的數據庫字段名第一個字母是大寫嗎?
再次檢查數據庫字段名稱。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.