Learn computer science
Edit Item - Printable Version

+- Learn computer science (https://discuss.computersciencewiki.org)
+-- Forum: Questions and Answers (https://discuss.computersciencewiki.org/forum-1.html)
+--- Forum: Questions and answers (https://discuss.computersciencewiki.org/forum-2.html)
+--- Thread: Edit Item (/thread-51.html)



Edit Item - Koala123 - 01-14-2021

Mr. Mackenty when I try to edit an item it doesn't pop up as it does on the inventory. I think that it's coded to be able to connect to the database.inc but yet it does not work. I have tried multiple things, but nothing seems to make a difference. Could you please take a look at my code and see if there is anything wrong with it? Thank you so much. 
PHP Code:
<!doctype html>
<!-- 
this file should be named recipe_edit_item.php -->
<
html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
  </head>
<
body>
    <!-- =========================================== -->
    <!-- PLEASE DO NOT EDIT ANYTHING ABOVE THIS LINE -->
    <!-- =========================================== -->
    <?php include('recipe_navbar.php'); ?>
<div class="container mt-5">

<table class="table table-hover table-bordered table-striped">
  <thead>
    <tr>
      <th scope="col">Id</th>
      <th scope="col">Name</th>
      <th scope="col">Link_to_image</th>
      <th scope="col">Recipe</th>
      <th scope="col">Meal_type</th>
      <th scope="col">Gluten_free</th>
      <th scope="col">Vegeterian</th>
      <th scope="col">Vegan</th>
      <th scope="col">Lactose_free</th>
    </tr>
  </thead>
<tbody>
  
  
  <?php

include('database_inc.php');

$result mysqli_query($connect,
"SELECT * FROM items;");

while (
$row mysqli_fetch_array($result))
{
?>
    <tr>
      <th scope="row"><?php echo $row['id']; ?></th>
      <td><?php echo $row['name']; ?></td>
      <td><?php echo $row['link_to_image']; ?></td>
      <td><?php echo $row['recipe']; ?></td>
      <td><?php echo $row['meal_type']; ?></td>
      <td><?php echo $row['gluten_free']; ?></td>
      <td><?php echo $row['vegeterian']; ?></td>
      <td><?php echo $row['vegan']; ?></td>
      <td><?php echo $row['lactose_free']; ?></td>
      <td><a href="recipe_edit_item_process.php?id=<?php echo $row['id']; ?>">Edit this item</a></td>
    </tr>
  <?php
}
?>
  </tbody>
</table>
</div> <!-- close the container -->
    
    <!-- =========================================== -->
    <!-- PLEASE DO NOT EDIT ANYTHING BELOW THIS LINE -->
    <!-- =========================================== -->
    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js" integrity="sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous"></script>
  </body>
</html> 
This is the process page that could also be the source of the problem;
PHP Code:
<!doctype html>
<!-- 
this file should be named recipe_edit_item_process.php -->
<
html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
  </head>
<
body>
    <!-- =========================================== -->
    <!-- PLEASE DO NOT EDIT ANYTHING ABOVE THIS LINE -->
    <!-- =========================================== -->
    <?php 

        
// the line below gets the id from the url. There are all sorts of security issues here
        // can you think of some security problems? 

        $item_to_edit $_GET['id'];

        // the line below includes a file that allows connection to our database.

        include('database_inc.php');

    ?>
<?php 
include('recipe_navbar.php'); ?>
<div class="container mt-5">

<?php 

$result 
mysqli_query($connect"SELECT * from items WHERE id = '$item_to_edit';");


while (
$row mysqli_fetch_array($result))
{
    ?>
<form action = "recipe_update_edited_item.php" method="POST">

  <div class="form-group">
    <label for="Id">Name</label>
    <input 
    name="name" 
    type="text" 
    class="form-control" 
    id="name"  
    placeholder="Enter name"
    value = "<?php echo $row['name']; ?>"
    >
  </div>

  <div class="form-group">
    <label for="ink_to_image">Link_to_image</label>
    <input 
    name="ink_to_image" 
    type="text" 
    class="form-control" 
    id="ink_to_image"  
    placeholder="Enter ink_to_image"
    value = "<?php echo $row['ink_to_image']; ?>">
  </div>

  <div class="form-group">
    <label for="recipe"> Recipe</label>
    <input 
    name="recipe" 
    type="text" 
    class="form-control" 
    id="recipe"  
    placeholder="Enter recipe"
    value = "<?php echo $row['recipe']; ?>">    
  
  </div>

  <div class="form-group">
    <label for="meal_type">Meal_type</label>
    <input 
    name="meal_type" 
    type="text" 
    class="form-control" 
    id="meal_type"  
    placeholder="Enter meal_type"
    value = "<?php echo $row['meal_type']; ?>">
  </div>

  <div class="form-group">
    <label for="gluten_free">Gluten_free</label>
    <input 
    name="gluten_free" 
    type="text" 
    class="form-control" 
    id="gluten_free"  
    placeholder="Enter gluten_free"
    value = "<?php echo $row['gluten_free']; ?>">
  </div>

  <div class="form-group">
    <label for="vegeterian">Vegeterian</label>
    <input 
    name="vegeterian" 
    type="text" 
    class="form-control" 
    id="vegeterian"  
    placeholder="Enter vegeterian"
    value = "<?php echo $row['vegeterian']; ?>">
  </div>

  <div class="form-group">
    <label for="vegan">Vegan</label>
    <input 
    name="vegan" 
    type="text" 
    class="form-control" 
    id="vegan"  
    placeholder="Enter vegan"
    value = "<?php echo $row['vegan']; ?>">
  </div>

  <div class="form-group">
    <label for="lactose_free">Lactose_free</label>
    <input 
    name="lactose_free" 
    type="text" 
    class="form-control" 
    id="lactose_free"  
    placeholder="Enter lactose_free"
    value = "<?php echo $row['lactose_free']; ?>">
  </div>

<input type="hidden" name="item_to_edit" value="<?php echo $item_to_edit?>">

  <button type="submit" class="btn btn-primary">Edit this item</button>
</form>

<?php
}
?>

</div> <!-- close the container -->
    
    <!-- =========================================== -->
    <!-- PLEASE DO NOT EDIT ANYTHING BELOW THIS LINE -->
    <!-- =========================================== -->
    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js" integrity="sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous"></script>
  </body>
</html> 



RE: Edit Item - bmackenty - 01-15-2021

Can you please paste in the source code for this page:

recipe_edit_item_process.php


RE: Edit Item - Koala123 - 01-19-2021

(01-15-2021, 10:15 AM)bmackenty Wrote: Can you please paste in the source code for this page:

recipe_edit_item_process.php

PHP Code:
<!doctype html>
<!-- 
this file should be named recipe_edit_item_process.php -->
<
html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
  </head>
<
body>
    <!-- =========================================== -->
    <!-- PLEASE DO NOT EDIT ANYTHING ABOVE THIS LINE -->
    <!-- =========================================== -->
    <?php 

        
// the line below gets the id from the url. There are all sorts of security issues here
        // can you think of some security problems? 

        $item_to_edit $_GET['id'];

        // the line below includes a file that allows connection to our database.

        include('database_inc.php');

    ?>
<?php 
include('recipe_navbar.php'); ?>
<div class="container mt-5">

<?php 

$result 
mysqli_query($connect"SELECT * from items WHERE id = '$item_to_edit';");


while (
$row mysqli_fetch_array($result))
{
    ?>
<form action = "recipe_update_edited_item.php" method="POST">

  <div class="form-group">
    <label for="Id">Name</label>
    <input 
    name="name" 
    type="text" 
    class="form-control" 
    id="name"  
    placeholder="Enter name"
    value = "<?php echo $row['name']; ?>"
    >
  </div>

  <div class="form-group">
    <label for="ink_to_image">Link_to_image</label>
    <input 
    name="ink_to_image" 
    type="text" 
    class="form-control" 
    id="ink_to_image"  
    placeholder="Enter ink_to_image"
    value = "<?php echo $row['ink_to_image']; ?>">
  </div>

  <div class="form-group">
    <label for="recipe"> Recipe</label>
    <input 
    name="recipe" 
    type="text" 
    class="form-control" 
    id="recipe"  
    placeholder="Enter recipe"
    value = "<?php echo $row['recipe']; ?>">    
  
  </div>

  <div class="form-group">
    <label for="meal_type">Meal_type</label>
    <input 
    name="meal_type" 
    type="text" 
    class="form-control" 
    id="meal_type"  
    placeholder="Enter meal_type"
    value = "<?php echo $row['meal_type']; ?>">
  </div>

  <div class="form-group">
    <label for="gluten_free">Gluten_free</label>
    <input 
    name="gluten_free" 
    type="text" 
    class="form-control" 
    id="gluten_free"  
    placeholder="Enter gluten_free"
    value = "<?php echo $row['gluten_free']; ?>">
  </div>

  <div class="form-group">
    <label for="vegeterian">Vegeterian</label>
    <input 
    name="vegeterian" 
    type="text" 
    class="form-control" 
    id="vegeterian"  
    placeholder="Enter vegeterian"
    value = "<?php echo $row['vegeterian']; ?>">
  </div>

  <div class="form-group">
    <label for="vegan">Vegan</label>
    <input 
    name="vegan" 
    type="text" 
    class="form-control" 
    id="vegan"  
    placeholder="Enter vegan"
    value = "<?php echo $row['vegan']; ?>">
  </div>

  <div class="form-group">
    <label for="lactose_free">Lactose_free</label>
    <input 
    name="lactose_free" 
    type="text" 
    class="form-control" 
    id="lactose_free"  
    placeholder="Enter lactose_free"
    value = "<?php echo $row['lactose_free']; ?>">
  </div>

<input type="hidden" name="item_to_edit" value="<?php echo $item_to_edit?>">

  <button type="submit" class="btn btn-primary">Edit this item</button>
</form>

<?php
}
?>

</div> <!-- close the container -->
    
    <!-- =========================================== -->
    <!-- PLEASE DO NOT EDIT ANYTHING BELOW THIS LINE -->
    <!-- =========================================== -->
    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js" integrity="sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous"></script>
  </body>
</html> 



RE: Edit Item - bmackenty - 01-26-2021

A last favor: please send me URL to your page