我目前只使用一种形式进行更新和插入,效果很好。但我想在更新时预先填写。我不确定如何做到这一点。如果我使用php变量设置值,我会在日志中收到错误,指出它未定义。任何帮助表示赞赏。谢谢。
以下是表格:
<?php
include_once('Crud_class.php');
$obj = new Crud("loocalhost","root","password","mydb");
$id = $_GET['id'];
if (isset($_GET['id']) && $_GET['id'] > 0) {
echo "Update for record id#:" . $_GET['id'];
} else {
echo "Insert new book";
}
?>
<html>
<head>
<title>Add New Product</title>
</head>
<body>
<form method="post" action="actions.php">
<ul class="form">
<li><label for"title">Title:</label>
<input type="text" name="title" value="<?php echo $title?>" /></li>
<li><label for="author">Author:</label>
<input type="text" name="author"/></li>
<li><label for="category">Category:</label>
<select name="category">
<option value="General">General</option>
<option value="HTML/CSS">HTML/CSS</option>
<option value="Javascript">Javascript</option>
<option value="PHP">PHP</option>
<option value="Other">Other</option>
</select></li>
<li><label for"description">Description:</label>
<textarea name="description"></textarea></li>
<li><label for="img_path">Image Path:</label>
<input type="text" name="img_path"/></li>
<input type="hidden" name="id" value="<?php if ($id > 0) { echo $id;} else {echo 0;} ?>"/>
<li><input class="submit" type="submit" name="submit" value="Submit"/></li>
</ul>
</form>
</body>
</html>
这是我的行动档案:
<?php
include('Crud_class.php');
$obj=new Crud("localhost","root","password","mydb");
if (isset($_POST['id']) && $_POST['id'] > 0) {
//update
extract($_REQUEST);
$obj->update($id,$title,$author,$category,$description,$img_path);
} else {
// insert
extract($_REQUEST);
$obj->insert($title,$author,$category,$description,$img_path);
}
?>
和我的crud文件
<?php
class Crud{
public $mysqli;
public $data;
public function __construct($host,$username,$password,$db_name){
$this->mysqli = new mysqli('localhost', 'root', 'password', 'mydb');
}
// BOOKS Table
//READ
public function readAll(){
$query="SELECT * FROM books";
$result= $this->mysqli->query($query);
$num_result=$result->num_rows;
if($num_result>0){
while($rows=$result->fetch_assoc()){
$this->data[]=$rows;
//print_r($rows);
}
return $this->data;
}
}
//INSERT
public function insert($title,$author,$category,$description,$img_path){
$query="INSERT INTO books SET title='$title', author='$author', category='$category', description='$description', img_path='$img_path'";
$result= $this->mysqli->query($query) or die(mysqli_connect_errno()."Product Failed to Insert");
if($result){
header('location:read.php?insert_status=success');
}
}
//UPDATE
public function update($id,$title,$author,$category,$description,$img_path){
$query="UPDATE books SET title='$title', author='$author', category='$category', description='$description', img_path='$img_path' WHERE id='$id'";
$result= $this->mysqli->query($query) or die(mysqli_connect_errno()."Cannot update");
if($result){
header('location:read.php?update_status=success');
}
}
//Delete
public function delete($id){
$query="DELETE FROM books WHERE id='$id'";
$result= $this->mysqli->query($query) or die(mysqli_connect_errno()."Failed to Delete");
if($result){
header('location:read.php?delete_status=success');
}
}
}
?>
答案 0 :(得分:2)
您需要从表单页面上正在编辑的记录中获取数据。
if (isset($_GET['id']) && $_GET['id'] > 0) {
echo "Update for record id#:" . $_GET['id'];
// GET row from database, something like $obj->read($_GET['id']);
}
为此你需要在你的CRUD类中添加一些读取函数,它选择一行id = some input integer。
如果您有数据,请将其提取到对象或数组,并使用以下格式的值:
<input type="text" value="<?php if (isset($data)) echo $data['author']; ?>">
(当然选择框需要更多工作)
答案 1 :(得分:1)
您已经设置了自己的ID和条件。现在您需要初始化您需要的变量:
if (isset($_GET['id']) && $_GET['id'] > 0) {
echo "Update for record id#:" . $_GET['id'];
$formValues = readRecordByID($_GET['id']); // you will need to create this crud select function
} else {
echo "Insert new book";
//initialize blank values:
$formValues = array(
'author'=>'',
'category'=>'',
'description'=>'',
'img_path'=>'');
}
现在,您可以安全地使用:
<input type="text" name="author" value="$formValues['author']"/>
答案 2 :(得分:0)
预填表单取决于在HTML中嵌入预填充内容,其中包含文本框的value=
属性或selected="selected"
option
中的<select>
等Title
客户端javascript基于通过AJAX检索的数据或者再次嵌入HTML中的数据来实现。
php中的value="<?php echo $title?>"
字段就是我所说的一个例子 - 你有这个:
selected="selected"
这就是所有其他字段所需的内容(以及<select>
字段{{1}})。
另请参阅this question
答案 3 :(得分:0)
将php更改为
<?php
include_once('Crud_class.php');
$obj = new Crud("loocalhost","root","password","mydb");
$id = $_GET['id'];
if (isset($_GET['id']) && $_GET['id'] > 0) {
echo "Update for record id#:" . $_GET['id'];
}
if(isset($_REQUEST['id'])){ // this is added
$id=$_REQUEST['id'];
$result=$obj->mysqli->query("SELECT * FROM books WHERE id='$id'");
$rows=$result->fetch_assoc();
extract($rows);
} else {
echo "Insert new book";
}
?>
并输入值
value="<?php echo $title; ?>"
作品!
感谢大家的帮助和意见! :)