简体   繁体   English

按ID提取MYSQL行,并在表单中进行编辑和更新?

[英]Fetch MYSQL row by id and edit and update in form?

I have a MYSQL table with edit link and delete button on each row. 我在每行上都有一个带有编辑链接和删除按钮的MYSQL表。 The edit link goes to edit_movie.php which has a form. 编辑链接转到具有表单的edit_movie.php。 My problem is that the form is not showing any data to be updated/edited. 我的问题是该表单未显示任何要更新/编辑的数据。 How do I fetch the id from the row clicked on or what function to write to pick it up. 我该如何从单击的行中获取ID或要编写的函数来提取它。 The form is just blank, no fields are filled out. 表单只是空白,没有填写任何字段。

Could someone help out I´d be very happy! 有人可以帮我很开心! Been sitting with this problem for 3 days now and just can´t get it to work. 已经有3天的时间坐在这个问题上了,无法工作。 I'm a PHP beginner... 我是PHP初学者...

I know I'm using mysql functions which are outdated and I need to lookover SQL injection which I will once I get this working. 我知道我正在使用已经过时的mysql函数,我需要查看SQL注入,一旦完成此工作,我将进行查找。

Table info: 表信息:

Table 1 name: Movies 表1名称:电影
Fields: id (primary key, AI), title, release_year, genre_id, director 字段:id(主键,AI),标题,release_year,genre_id,director

Table 2 name: Categories 表2名称:类别
Fields: genre_id (primary key, AI), genre 字段:genre_id(主键,AI),类型

They have a foreign relation between genre_id. 它们在genre_id之间有异物。

Here is the code in index.php file 这是index.php文件中的代码

    <?php

require('movie.inc.php');

if ( isset($_GET['delete']) && isset($_GET['id']) ){
   if ( delete_movie_by_id($_GET['id']) ){ //it's 100% safe
       die('Movie has been removed. Refresh the page now'); // or the like
   } else {
      echo 'Sorry movie could not be deleted'; // could not - handle here
   }
}
    include 'add_movie.php';
?>
<!DOCTYPE html>
<html>
<head>

<title>My movie library</title>
<meta charset="utf-8" />
<link rel="stylesheet" href="mall.css" />

</head>
<body>

<table>
 <tr>
  <th>Title</th>
  <th>Release year</th>
  <th>Genre</th><th>Director</th>
  <th>Update</th>
  <th>Delete</th>
 </tr>

  <?php  foreach (get_all_movies() as $index => $row) : ?>
   <tr>
     <td><?php echo $row['title'];?></td>
     <td><?php echo $row['release_year']; ?></td>
     <td><?php echo $row['genre'];?></td>
     <td><?php echo $row['director'];?></td>
     <td><a href='<?php printf('edit_movie.php?edit=%s', $row['id']);?>'>Edit</a></td>
     <td>
      <form action="index.php" method="GET">
              <input type="hidden" name="delete" value="yes" />
              <input type="hidden" name="id" value="<?php echo $row['id'];?>" /> 
              <input type="submit" value="Delete" />
      </form>
      </td>
    </tr>
    <?php endforeach; ?>
    </table>

</body>
</html>

Here is the edit_movie.php code: 这是edit_movie.php代码:

 <?php

    require 'connect.inc.php';
    require_once('movie.inc.php');

    ?>

    <!DOCTYPE html>

    <html>

    <head>

    <title>My movie library</title>
    <meta charset="utf-8" />
    <link rel="stylesheet" href="mall.css" />

    </head>

    <body>

    <h1>Edit movie</h1>
    <div id="form_column">
    <form action="edit_movie.php" method="post">
    <input type="hidden" name="id" value="<?php if (isset($row["id"])) ?>" /> <br>
    Title:<br> <input type="text" name="title" value="<?php if (isset($row["title"])) { echo $row["title"];} ?>" /> <br>
    Release Year:<br> <input type="text" name="release_year" value="<?php if (isset($row["release_year"])) { echo $row["release_year"];} ?>" /> <br>
    Director:<br> <input type="text" name="director" value="<?php if (isset($row["director"])) { echo $row["director"];} ?>" /> <br><br>
    Select genre:
    <br>
    <br> <input type="radio" name="genre_id" value="1" checked />Action<br>
    <br> <input type="radio" name="genre_id" value="2" />Comedy<br>
    <br> <input type="radio" name="genre_id" value="3" />Drama<br>
    <br> <input type="radio" name="genre_id" value="4" />Horror<br>
    <br> <input type="radio" name="genre_id" value="5" />Romance<br>
    <br> <input type="radio" name="genre_id" value="6" />Thriller<br><br>
    <input type="submit" value="Update movie" />
    </form>
    </div>



    </body>

    </html>

And here is the movie.inc.php file 这是movie.inc.php文件

<?php

require_once('connect.inc.php');


function get_all_movies(){

   $query = "SELECT * FROM movies m INNER JOIN categories c ON m.genre_id = c.genre_id";

   $result = mysql_query($query);

   if ( ! $result ){ 
     return false;
   } else {
     $return = array();

     while ($row = mysql_fetch_assoc($result)){

        $return[] = array('director' => $row['director'], 'genre' => $row['genre'], 'release_year' => $row['release_year'], 'title' => $row['title'], 'id' => $row['id']); 
     }
       return $return;
   }
}


function delete_movie_by_id($id){
   return mysql_unbuffered_query(sprintf("DELETE FROM `movies` WHERE id='%s' LIMIT 1", mysql_real_escape_string($id)));
}

if ( isset($_POST['delete'], $_POST['id']) ){

   delete_movie_by_id($_POST['id']); 
}


?>

In edit_movie.php it doesn't look like you're actually getting the movie passed view the form. edit_movie.php ,看起来好像实际上不是在通过表单查看电影。 You need something like this in your movie.inc.php : 您需要在movie.inc.php

function get_movie_to_edit($id) {

     $query = "SELECT * FROM movies m INNER JOIN categories c ON m.genre_id = c.genre_id WHERE id = $id";

     $result = mysql_query($query);

     if ( ! $result ){ 
         return false;
     } else {

         while ($row = mysql_fetch_assoc($result)){

                $return = array('director' => $row['director'], 'genre' => $row['genre'], 'release_year' => $row['release_year'], 'title' => $row['title'], 'id' => $row['id']); 
         }
             return $return;
     }
}

And then in edit_movie.php you need to call this function. 然后在edit_movie.php中,您需要调用此函数。 Something like... 就像是...

if(isset($_GET['edit'])) {
    $movie = get_movie_to_edit($_GET['edit']);
}

Of course, as you mentioned, you need to clean up a lot of the code to prevent against injection and check for a scenario where, say, you get to edit_movie.php and the ID ro edit doesn't exist, but this is the basic gist. 当然,正如您所提到的,您需要清理很多代码以防止注入,并检查是否存在edit_movie.php进入edit_movie.php且ID ro编辑不存在的情况,但这是基本要点。

You would also then edit the values in the form in edit_movie.php to reflect the new array like: 然后,您还可以在edit_movie.php中的表单中编辑值,以反映新的数组,例如:

<form action="edit_movie.php" method="post">
<input type="hidden" name="id" value="<?php echo $movie['id']; ?>" /> <br>
Title:<br> <input type="text" name="title" value="<?php echo $movie['title']; ?>" /> <br>
.... the rest of your form inputs
</form>

Updated For the genre radio buttons... 已更新对于类型单选按钮...

<input type="radio" name="genre_id" value="1"<?php if($movie['genre'] == 1) { echo ' checked'; } ?> />Action<br>
<input type="radio" name="genre_id" value="2"<?php if($movie['genre'] == 2) { echo ' checked'; } ?> />Comedy<br>
.... and so on

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

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