我有一個問題,試圖用表單更新數據庫。MYSQL和PHP數據庫更新不會更新數據庫
所以,我的問題是,當我點擊更新什麼都沒有發生。數據庫不會更新。
另一個問題是,當我在我的表單和SQL查詢中使用POST。它不會將信息拉到編輯頁面,它是空白的。
對不起,如果這難以閱讀等,但這是我第一次發佈。另外,我知道在我的GET/POST查詢中存在安全漏洞,我只是試圖在開始使用準備好的語句或任何它們所調用的內容之前使它正常工作。如果我回應查詢並定義一個if/else語句,我可以看到它不起作用,但我只是不知道爲什麼。我已經花了3天時間,並使用我在互聯網上找到的示例多次更改代碼。
的index.php
<?php
$servername = "localhost";
$username = "***";
$password = "****";
$dbname = "****";
$link = new mysqli("$servername", "$username", "$password", "$dbname");
if ($link->connect_error) {
die("Connection failed: " . $link->connect_error);
}
echo "Connected successfully";
mysqli_select_db($link,"jamesrph_myphp");
$sql = "SELECT * FROM article";
$result = mysqli_query($link,$sql);
$id = 'id';
$title = 'title';
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css"/>
<title>PHP </title>
</head>
<body>
<h1> My title </h1>
<table>
<tr>
<?php
while($row=mysqli_fetch_array($result)){
?>
<td><?php echo $row["title"]; ?> </td>
<td><a href="article_detail.php?id=<?php echo $row["id"]; ?>">Read More</a></td>
<td><a href="edit.php?id=<?php echo $row["id"]; ?>">Edit</a></td>
</tr>
<?php
}
?>
</table>
</body>
</html>
edit.php
<?php
$link = mysqli_connect("localhost","******","******", "*****");
$query = "SELECT * FROM article WHERE id=".mysqli_real_escape_string($link, $_GET['id'])." LIMIT 1";
$result = mysqli_query($link,$query);
$row = mysqli_fetch_array($result);
$title = $row['title'];
$content = $row['content'];
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>
<p> Edit Article </p>
<form method="get" action="processarticle.php">
<input type="hidden" name="id" value="<?php echo $row["id"]; ?>" />
<input id="titlearea" type="text" name="title" value="<?php echo $row["title"]; ?>"/>
<textarea id="contentarea" name="content" rows="10" cols="40"><?php echo $row["content"];?></textarea>
<input type="submit" name="submit" id="update_article"/>
</form>
</body>
</html>
processarticle.php
<<?php
//Database Connection
include 'connection.php';
//Get ID from Database
if(isset($_GET['edit_id'])){
$sql = "SELECT * FROM article WHERE id =" .$_GET['edit_id'];
$result = mysqli_query($link, $sql);
$row = mysqli_fetch_array($result);
}
//Update Information
if(isset($_POST['btn-update'])){
$title = $_POST['title'];
$content = $_POST['content'];
$id = $_POST['id'];
$update = "UPDATE article SET title=?, content=? WHERE id=?";
$up = mysqli_query($link, $update);
if($stmt = $mysqli->prepare($update)){
$stmt->bind_param("ssi" ,$title ,$content ,$id);
$stmt->excute();
}
header("location: disp.php");
}
?>
嘗試使用連接(帶的連接字符串。)在UPDATE查詢的$ title和$內容以及。 –
對不起在我的鏈接意味着什麼? –
找到某些工作沒有任何價值,然後**修復安全漏洞。無論如何,你必須學習使用準備好的語句來修復它。爲什麼不節省時間並首先學習安全方法,而不是學習如何製作不安全的代碼,然後學習如何保護它? –