-1

As my first PHP project, I have created the connection.php that I suppose it is everything right:

<?php

$host = "localhost";
$user = "root";
$password = "";
$db_name = "php_store";

$connection = mysqli_connect($host, $user, $password, $db_name);
?>

Then I have created a file.php called list where shows my stored products from the database:

<?php
    include 'connection.php';

    $get_products = "SELECT * FROM tb_products ";
    $query_products = mysqli_query($connection, $get_products);
?>

<!doctype html>
<html lang="en">
  <head>
    <title>Products</title>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="wpriceth=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
  </head>
  <body>
    <!-- getting products to be shown on the html page -->
    <table class="table">
        <thead>
            <tr>
                <th>SKU</th>
                <th>DELETE</th>
                <th>name</th>
                <th>price</th>
                <th>attributes</th>
            </tr>
        </thead>
        <tbody>
            <td></td>
            <td> <!-- DELETING values -->
                <form action="delete.php" method="POST">
                    <input type="hidden" name="id" value="<?php echo $id?>">
                    <input type="submit" value="DELETE" class="btn btn-danger">
                </form>
            </td>
            <td></td>
            <td></td>
            <!-- creating while instructions -->
            <?php
                while($getting_products = mysqli_fetch_array($query_products)) {
                    $id = $getting_products['id'];
                    $name = $getting_products['name'];
                    $price = '$: ' . $getting_products['price'];
                    $attributes = $getting_products['attributes'];
            ?>
            <tr>
                <td scope="row"><?php echo $id?></td>
                <td>  <!-- DELETE CHECKBOX -->
                    <form action="delete.php" method="POST">
                    <input type="hidden" name="id" value="<?php echo $id?>">
                        <input  class="btn btn-danger"
                                name="checkbox[]"
                                type="checkbox"
                                value="<?php echo $id; ?>"
                        ><!-- not working -->
                    </form>
                </td>
                <td><?php echo $name?></td>
                <td><?php echo $price?></td>
                <td><?php echo $attributes?></td>
            </tr>

            <?php }; ?> <!-- closing while -->

            <!-- creating new values on the database -->
            <form action="create.php" method="POST">
                <td></td>
                <tr>
                    <th>name</th>
                    <th>price</th>
                    <th>attributes (Mb or Kg or HxWxL)</th>
                </tr>
                <td><input type="text" name="name"></td>
                <td><input type="real" name="price"></td>
                <td><input type="text" name="attributes"></td>
                <td><input class="btn btn-primary" type="submit" value="Add Product"></td>
            </form>            
        </tbody>
    </table>
    <!-- Sendint to edit page --> <!--  -->
    <div class="float-right">
        <form action="http://localhost/edit.php">
            <button class="btn btn-warning text-light"
                    onclick="edit.php" type="submit"
            >
            Go to EDIT page
        </button>
        </form>
    </div>
      
    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
  </body>
</html>

And it works as expected, but creating an edit.php where shows a similar page as the list, but the products should be edited, and they're being, I get Warnings in the browser saying my lines 6, 7, and 8 are undefined:

1      <?php
2      // getting connection
3      include 'connection.php';
4    
5      // receives typed data
6      $id = $_POST['id'];
7      $name = $_POST['name'];
8      $price = $_POST['price'];
9    
10     // insert data on products table
11     $receives_products = "UPDATE tb_products
12     SET name = '$name', price = '$price'
19     WHERE id = '$id' ";
14    
15     // validates connection
16     $query_products = mysqli_query($connection, $receives_products) or die(mysqli_error($connection));
17     ?>
18    
19     <?php
20         $get_products = "SELECT * FROM tb_products ";
21         $query_products = mysqli_query($connection, $get_products);
22     ?>
23     
24     <!doctype html>
25     <html lang="en">
26       <head>
27         <title>EDIT</title>
28         <!-- Required meta tags -->
29         <meta charset="utf-8">
30         <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
31    
32         <!-- Bootstrap CSS -->
33         <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
34       </head>
35       <body>
36           
37         <!-- getting products to be shown on the html page -->
38         <?php
39    
40             while($getting_products = mysqli_fetch_array($query_products)) {
41                 $name = $getting_products['name'];
42                 $price = '$: ' . $getting_products['price'];
43             ?>
44             <tr>
45                 <td scope="row"><?php echo $id?></td>
46                 <td><?php echo $name?></td>
47                 <td><?php echo $price?></td>
48                 <td> <!-- EDITING values -->
49                     <form action="edit.php" method="POST">
50                         <input type="hidden" name="id" value="<?php echo $getting_products['id']; ?>">
51    
52                         <input type="text" name="name" value="<?php echo $getting_products['name'];?>">
53                         <input type="text" name="price" value="<?php echo $getting_products['price'];?>">
54                         <input type="submit" value="EDIT" class="btn btn-warning">
55                    </form>
56                 </td>
57    
58         <?php }; ?> <!-- closing while -->
59    
60         <form action="http://localhost/list.php">
61             <button class="btn btn-success" onclick="list.php" type="submit">Back</button>
62         </form>
63         <!-- Optional JavaScript -->
64         <!-- jQuery first, then Popper.js, then Bootstrap JS -->
65         <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
66         <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
67         <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
68       </body>
69     </html>

The warnings when I visit the edit.php page are:

Warning: Undefined array key "id" in C:\xampp\htdocs\edit.php on line 6

Warning: Undefined array key "name" in C:\xampp\htdocs\edit.php on line 7

Warning: Undefined array key "price" in C:\xampp\htdocs\edit.php on line 8

I've no idea why it's happening once that I'm calling the connection.php in the file, so I hope someone can help me with that, and as I said, everything is working as expected, I just don't know why I'm getting these warning.

Your Common Sense
  • 154,967
  • 38
  • 205
  • 325
GNeto
  • 89
  • 5
  • you've been given the answer already – Your Common Sense Jun 04 '22 at 13:27
  • You need to add the [POST method](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form#attr-method) to your form. The default value of HTML method attribute is GET. – KIKO Software Jun 04 '22 at 13:29
  • `onclick="edit.php"` looks like wishful thinking, remove it – mplungjan Jun 04 '22 at 13:40
  • When you say, I have to add the method post to my form, are you talking about the form inside the edit.php line 50? I have added the method="POST on line 85, but the warnings are still there. – GNeto Jun 04 '22 at 21:37
  • I created another page called editPage.php where I have used code from the edit.php, but I used only from the line 19 to 69 changing the id value on the line 45 to $getting_products['id'] and before the html I used – GNeto Jun 04 '22 at 23:48

0 Answers0