-1

The update feature that im working on is confusing me with the undefined index that I got. I had a button from a page called model.php and it links to the update_model.php page. The error that I got is Undefined index: idx when i press the confirm the update in my update_model page. My record does not get updated in the database as well but the success message does appear. I have already declared the idx variable using get method, but why does it still say that it is an undefined index? Is it not getting anything?

update_model.php:

     <?php
                $idd = $_GET['idx'];
                $query = $conn->query("SELECT * FROM model WHERE model_id='$idd' limit 0,1");
                $row = $query->fetch_assoc();
                if(isset($_POST['updateBtn'])){
                    $id = $row['model_id'];
                    $model = $_POST['model'];
                    $result = $conn->query("UPDATE model SET model='".$model."' WHERE model_id='".$id."'");    
                     if($result){//success update message
            ?>
                <div class="alert alert-success alert dismissible" role = "alert">
                    <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <strong id="alert">Success!</strong>Your data has been updated.
                </div>
            <?php    
                }
            }
?>

    <form action="update_model.php" method="post">
                        <table>
                            <tr>
                                <td> Model </td>
                                <td>        
                                <input type="text" name="model" placeholder="<?php echo $row['model'] ?>" required>
                                </td>
                                <td>
                                <button name="updateBtn" type="submit" class="btn-success btn"> Update </button>
                                </td>

                            </tr >
                        </table>
            </form>

button in model.php:

<button class="btn-success btn"> <a href="update_model.php?idx=<?php echo $rows['model_id']; ?>" class="text-white"> Update </a> </button>
heh
  • 69
  • 8
  • You form input name as `model`, and also you submit for in `post` method then how can you use `$_GET['idx']`? – Siva Feb 12 '19 at 15:10
  • debug with: var_dump($_GET); and var_dump($_POST); – Andrew Feb 12 '19 at 15:13
  • **Warning:** Your code is vulnerable to SQL Injection attacks. You should use parameterised queries and prepared statements to help prevent attackers from compromising your database by using malicious input values. http://bobby-tables.com gives an explanation of the risks, as well as some examples of how to write your queries safely using PHP / mysqli. **Never** insert unsanitised data directly into your SQL. The way your code is written now, someone could easily steal, incorrectly change, or even delete your data. – ADyson Feb 12 '19 at 15:13
  • The issue is because when you submit the form in update_model, there's no "idx" field in the form, and certainly not one passed via GET (especially since the form uses post). The value is not persisted from the previous request. If you need it again after the form is submitted then you need to add it to your form's action method as per [this answer below](https://stackoverflow.com/a/54653180/5947043) so that it gets sent back to the server again when the form is submitted. (N.B. This all occurs because HTTP requests are _stateless_) – ADyson Feb 12 '19 at 15:17

1 Answers1

1

Change you code here from

<form action="update_model.php" method="post">

to

<form action="update_model.php?idx=<?=$idd;?>" method="post">