我想为自己创建一个小CMS,因此我可以在受保护的页面上编辑我的文本内容。我已经有一个页面,我可以在textarea(test.php)中看到我的文本,但是当我点击编辑按钮时它不会工作。
谁可以帮助我? :)ps,为了安全起见,已删除数据库配置...
// test.php的
<code>
<?php
// CONNECT TO THE DATABASE
$DB_NAME = '';
$DB_HOST = '';
$DB_USER = '';
$DB_PASS = '';
$mysqli = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
echo'<h1> All users</h1><br>';
// A QUICK QUERY ON A FAKE USER TABLE
$query = "SELECT * FROM `categorien` WHERE `categorie_id`";
$result = $mysqli->query($query) or die($mysqli->error.__LINE__);
// GOING THROUGH THE DATA
if($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo'
<p>
<form method="post" action="edit.php">
<b>Title:</b> '.$row['categorie_naam']. '</td><br>
<textarea name="categorie_uitleg">'.$row['categorie_uitleg'].'</textarea><br />
<INPUT type="hidden" name="id" value='.$row['categorie_id'].'>
<INPUT type="submit" name="edit" value="edit">
</form>
</p>
';
}
}
else {
echo 'NO RESULTS';
}
// CLOSE CONNECTION
mysqli_close($mysqli);
?>
</code>
// edit.php
<?php
session_start();
$servername = "";
$username = "";
$password = "";
$dbname = "";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "UPDATE `categorien` SET `categorie_uitleg` = '". $_POST['categorie_uitleg']) ."' WHERE 1";
if ($conn->query($sql) === TRUE) {
header("Location: test.php");
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
$conn->close();
?>
答案 0 :(得分:0)
您收到错误消息吗?
我注意到categorie_uitleg
列没有获得在textarea中编辑的新值,并且表名在引号(&#39;)内而不是ticks(`):< / p>
$query = "UPDATE 'categorien' SET `categorie_uitleg` WHERE `1`";
它应该是这样的:
$query = "UPDATE `categorien` SET `categorie_uitleg` = '". $_POST['categorie_uitleg']) ."' WHERE 1";
注意:此查询将更新categorien
表中的所有行!
根据评论编辑
<textarea>'.$row['categorie_uitleg'].'</textarea><br />
name-attribute应添加到<textarea>
<textarea name="categorie_uitleg">'.$row['categorie_uitleg'].'</textarea><br />