简体   繁体   English

在更新时预先填写php表单

[英]Pre-filling php form on update

I am currently using one form only for both updating and inserting, it works well. 我目前仅使用一种表格进行更新和插入,效果很好。 But I would like to have it prefilled when updating only. 但是我只想在更新时预先填充它。 I am not exactly sure how to do this. 我不确定如何执行此操作。 If I set the value with the php variable I get an error in the log that states it is not defined. 如果使用php变量设置该值,则会在日志中显示一条错误,指出未定义。 Any help is appreciated. 任何帮助表示赞赏。 Thank you. 谢谢。

Here is the form: 形式如下:

<?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>

and here is my actions file: 这是我的动作文件:

<?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);
 }

?>

and my crud file 和我的粗锉

       <?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');  
    }               
}


}




 ?>

You need to get the data from the record that someone is editing on the form page. 您需要从某人正在表单页面上编辑的记录中获取数据。

if (isset($_GET['id']) && $_GET['id'] > 0) {
  echo "Update for record id#:" . $_GET['id'];
  // GET row from database, something like $obj->read($_GET['id']);
}

For this you need to add some read-function to your CRUD class which selects a line where id= some input integer. 为此,您需要在CRUD类中添加一些读取功能,以选择id =一些输入整数的行。

If you have the data, fetch it to an object or array and use these values in the form: 如果有数据,则将其提取到对象或数组,并以以下形式使用这些值:

<input type="text" value="<?php if (isset($data)) echo $data['author']; ?>">

(of course the select box needs a bit more work) (当然,选择框需要做更多的工作)

You've already got your ID and conditions set up. 您已经设置了ID和条件。 Now you need to initialize the vars you will need: 现在,您需要初始化所需的变量:

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'=>'');
}

Now in your form it is safe to use: 现在以您的形式可以安全使用:

<input type="text" name="author" value="$formValues['author']"/>

Prefilling a form depends either on embedding the prefill contents in the HTML with attributes such as the value= attribute for text boxes or setting selected="selected" on an option in a <select> , or on having client-side javascript do that based on data either retrieved via AJAX or, again, embedded in the HTML. 预填充表单取决于使用属性(例如,文本框的value=属性)在HTML中嵌入预填充内容,还是在<select>option上设置selected="selected" ,或者取决于客户端javascript这样做通过AJAX检索或再次嵌入HTML的数据。

Your Title field in the form php is an example of what I'm talking about -- you've got this: php形式的Title字段就是我正在谈论的一个示例-您已经知道了:

value="<?php echo $title?>"

and that's what you'd need for all the rest of the fields (and selected="selected" for the <select> fields). 这就是其余所有字段所需要的(对于<select>字段,则为selected="selected" )。

Also, see this question 另外,看这个问题

changing the php to 将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";
}
?>

and input value to 并输入值到

     value="<?php echo $title; ?>"

Works! 作品!

Thanks everyone for all your help and input! 感谢大家的帮助和投入! :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM