我正在尝试创建一个更新数据库中的行的表单。所以我可以为帖子和类似的东西制作标题。
任何人都可以帮助我吗?
答案 0 :(得分:1)
更新:鉴于您提供的代码短,我将对我的答案进行一些小的更新。
您的问题很一般,但我会提供一些代码来帮助您入门。例如,如果您想更新帖子#60的标题。
<form action='update.php' method='POST'>
<label for='title'>Post title:</label>
<input type='text' id='title' name='title' required>
<label for='content'>Post body:</label>
<textarea cols='80' rows='10' id='content' name='content'></textarea>
<input type='hidden' name='postID' value='60'>
<button type='submit'>Update</button>
</form>
我假设每个帖子都有一个名为id
的唯一ID。这又是一个非常简单的例子。为了这个例子,我还假设你的数据库是MySQL。我会在这里使用PDO但是如果你真的需要,可以随意使用mysqli或mysql驱动程序。
<?php
// Define your MySQL credentials here
define('DB_NAME', 'your database name');
define('DB_HOST', 'localhost'); // usually it's localhost
define('DB_PORT', 3306) // usually it's 3306
define('DB_USER' 'username');
define('DB_PASSWORD', 'password');
// Check if your HTTP POST parameters are set
if(!isset($_POST['title']) || !isset($_POST['content']) || !isset($_POST['id'])) {
die('Missing POST parameters');
}
// Create a new PDO instance
$db = new PDO("mysql:dbname=" . DB_NAME . ";host=" . DB_HOST . ";port=" . DB_PORT, DB_USER, DB_PASSWORD);
$sql = 'UPDATE `text` SET `title` = :title, `content` = :content WHERE `id` = :id;';
// Create a new PDOStatement by preparing it
$statement = $db->prepare($sql);
// Bind the values to the prepared statement
// By doing this you don't have to worry about escaping them
$statement->bindValue(':title', $_POST['title'], PDO::PARAM_STR);
$statement->bindValue(':content', $_POST['content'], PDO::PARAM_STR);
$statement->bindValue(':id', $_POST['postID'], PDO::PARAM_INT);
// Execute the prepared statement
$statement->execute();
// Since we're doing an update, it is a good idea to check if the query succeeded.
// rowCount() should return 1 here
if($statement->rowCount()) {
echo 'Post title updated.';
}
?>
有关详细信息,请参阅MySQL documentation for the UPDATE query(或RDBMS的文档)。
希望这会让你开始!
答案 1 :(得分:0)
核心脚本文件
<?
mysql_connect();
mysql_select_db("new");
$table = "test";
if($_SERVER['REQUEST_METHOD']=='POST') { //form handler part:
$name = mysql_real_escape_string($_POST['name']);
if ($id = intval($_POST['id'])) {
$query="UPDATE $table SET name='$name' WHERE id=$id";
} else {
$query="INSERT INTO $table SET name='$name'";
}
mysql_query($query) or trigger_error(mysql_error()." in ".$query);
header("Location: http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']);
exit;
}
if (!isset($_GET['id'])) { //listing part:
$LIST=array();
$query="SELECT * FROM $table";
$res=mysql_query($query);
while($row=mysql_fetch_assoc($res)) $LIST[]=$row;
include 'list.php';
} else { // form displaying part:
if ($id=intval($_GET['id'])) {
$query="SELECT * FROM $table WHERE id=$id";
$res=mysql_query($query);
$row=mysql_fetch_assoc($res);
foreach ($row as $k => $v) $row[$k]=htmlspecialchars($v);
} else {
$row['name']='';
$row['id']=0;
}
include 'form.php';
}
?>
和2个模板文件:form.php
<form method="POST">
<input type="text" name="name" value="<?=$row['name']?>"><br>
<input type="hidden" name="id" value="<?=$row['id']?>">
<input type="submit"><br>
<a href="?">Return to the list</a>
</form>
和list.php
<a href="?id=0">Add item</a>
<? foreach ($LIST as $row): ?>
<li><a href="?id=<?=$row['id']?>"><?=$row['name']?></a>
<? endforeach ?>