简体   繁体   中英

Cannot update row in mysql using PHP

here is the index.php

 <body>
        <?php require_once 'process.php';?>

         <?php
            if (isset($_SESSION['message'])):?>

            <div class="alert alert-<?$_SESSION['msg_type']?>">

                <?php
                    echo $_SESSION['message'];
                    unset($_SESSION['message']);
                ?>
             </div>
         <?php endif ?>
        <div class = "container">
        <?php 
            $result=mysqli_query($conn,"SELECT * FROM book ");
        ?>

            <div class= "row justify-content-center">
                <table class="table">
                    <thead>
                        <tr>
                            <th>Title</th>
                            <th>Author</th>
                            <th>Year</th>
                            <th>Donor's Name</th>
                            <th>Call Number</th>
                            <th>Date Received</th>
                            <th>Handled By</th>
                            <th colspan="2">Action</th>
                        </tr>
                    </thead>
                <?php
                    while ($row = mysqli_fetch_assoc($result)){
                ?>
                        <tr>
                            <td><?php echo $row['title'];?></td>
                            <td><?php echo $row['author'];?></td>
                            <td><?php echo $row['year'];?></td>
                            <td><?php echo $row['donorname'];?></td>
                            <td><?php echo $row['callnum'];?></td>
                            <td><?php echo $row['datereceived'];?></td>
                            <td><?php echo $row['handledby'];?></td>
                            <td>
                                <a href="index.php?edit=<?php echo $row["id"];?>" class="btn btn-info">Update</a>
                            </td>

                            <td>
                                <a href="process.php?delete=<?php echo $row["id"];?>" class="btn btn-danger">Delete</a>
                            </td>

                        </tr>
                        <?php } ?>
                </table>
                </div>
                <?php 
            function pre_r($array){
                echo '<prev>';
                print_r($array);
                echo '</prev>';
            }
            ?>
    <form action="process.php" method ="POST">
                <input type="hidden" name="id" value="<?php echo $id;?>">

                <div class="form-group">
                <label>Title</label>
                <input type ="text" name="title" value="<?php echo $title;?>">
                </div>
                <div class="form-group">            
                <label>Author</label>
                <input type ="text" name="author" value="<?php echo $author;?>">
                </div>
                <div class="form-group">
                <label>Year</label>
                <input type ="text" name="year" value="<?php echo $year;?>">
                </div>
                <div class="form-group">            
                <label>Donor's Name</label>
                <input type ="text" name="donorname" value="<?php echo $donorname;?>">
                </div>
                <div class="form-group">
                <label>Call Number</label>
                <input type ="text" name="callnum" value="<?php echo $callnum;?>">
                </div>
                <div class="form-group">            
                <label>Date Received</label>
                <input type ="Date" name="datereceived" value="<?php echo $datereceived;?>">
                </div>
                <div class="form-group">
                <label>Handled By</label>
                <input type ="text" name="handledby" value="<?php echo $handledby;?>">
                </div>

                <div class="form-group">    
                <?php
                    if ($update ==true):
                ?>      
               <button type="submit" name="update" class="btn btn-primary">Update</button>
                <?php else: ?>
                <button type="submit" name="save" class="btn btn-primary">save</button>
            <?php endif; ?>
                </div>
            </form>

process.php page

<?php

  session_start();
  $conn = mysqli_connect('localhost','root','');

  if(!$conn)
  {
    die('Could not connect : ' . mysqli_error());
  }
  mysqli_select_db($conn,"admin");


   $id =0;
   $update = false;  
   $title='';   
   $author='';
   $year='';   
   $donorname='';
   $callnum='';   
   $datereceived='';
   $handledby='';  

if (isset($_POST['update'])){
  $id = $_POST['id'];
  $title = $_POST['title'];
  $author=$_POST['author'];
    $year = $_POST['year'];
    $donorname=$_POST['donorname'];
    $callnum = $_POST['callnum'];
    $datereceived=$_POST['datereceived'];
    $handledby = $_POST['handledby'];

    $update=mysqli_query($conn,"UPDATE book SET `title`='$title', author ='$author', year='$year',     donorname='$donorname', callnum='$callnum', datereceived='$datereceived' , handledby='$handledby' WHERE `id`='$_POST[id]'");

      $_SESSION['message'] ="Record has been updated!";
      $_SESSION['msg_type'] ="warning";

  header("location:index.php");

}

?>

The problem is now, I can only update the last row. I want to update each row that I selected but only the last row can be updated.I couldn't figure out which part did I do wrong. It seems like the id is not pass, however on the url when I click on the update button, the id is already passed. I already tried this code using different database, and it seems to work just fine.

First you need to get the right 'id' of the selected row, based on your code you are using a link to determine the selected 'id' from the "edit=<?php echo $row["id"];?>" . Now what you need to do is get the id from the link:

if(isset($_GET['edit'])) $selected_id=$_GET['edit'];
else $selected_id=0;

Then set the value of the hidden input:

<input type="hidden" name="id" value="<?php echo $selected_id;?>">

And in your update query change this WHERE id='$_POST[id]' to WHERE id=$id

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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