Reputation: 23
I'm trying to update a category name with no luck...
on the functions.php i have a function to show all categories:
function get_all_categories(){
global $connection;
$query = 'SELECT * FROM categories';
$select_categories = mysqli_query($connection,$query);
while ($row = mysqli_fetch_assoc($select_categories)) {
$cat_id = $row['cat_id'];
$cat_title = $row['cat_title'];
echo "<tr>";
echo "<td>$cat_id</td>";
echo "<td>$cat_title</td>";
?>
<form method="post">
<input type="hidden" name="delete_cat_id" value="<?php echo $cat_id; ?>">
<?php
echo '<td class="text-center"><input class="btn btn-danger" type="submit" value="Delete" name="delete"></td>';
?>
</form>
<form method="post">
<input type="hidden" name="update_category_id" value="<?php echo $cat_id; ?>">
<?php
echo '<td class="text-center"><input class="btn btn-info" type="submit" value="Update" name="update_category"></td>';
?>
</form>
<?php
// echo "<td class='text-center'><a class='btn btn-info' href='categories.php?update=$cat_id'>Update</a></td>";
echo "</tr>";
}
}
the function is shown in the file categories.php:
<form action="" method="post">
<div class="form-group">
<label for="my_new_cat">Add a new category
<input class="form-control" type="text" name="my_new_cat">
</label>
</div>
<input type="hidden" name="category_id" value="<?php echo $cat_id; ?>">
<div class="form-group">
<input class="btn btn-primary" type="submit" name="submit" value="Add Category">
</div>
</form>
<?php
if (isset($_POST['update_category'])) {
include "includes/update_categories.php";
}
?>
my code in the update_categories.php is:
<?php
$selected_cat_id = $_POST['update_category_id'];
$query = "SELECT * FROM categories WHERE cat_id = $selected_cat_id";
$select_edit_categories = mysqli_query($connection,$query);
while ($row = mysqli_fetch_assoc($select_edit_categories)) {
$cat_id = $row['cat_id'];
$cat_title = $row['cat_title'];
}
?>
<form method="post">
<div class="form-group">
<label for="updated_cat_name">Update category</label>
<input class="form-control" type="text" name="updated_cat_name" value="<?php echo $cat_title; ?>">
</div>
<div class="form-group">
<input class="btn btn-primary" type="submit" name="update_cat_title" value="Update Category">
</div>
</form>
<?php
if(isset($_POST['update_cat_title'])){
$updated_cat_title = $_POST['updated_cat_name'];
$edited_category_id = $_POST['category_id'];
$update_cat_query = "UPDATE categories SET cat_title = '$updated_cat_title' WHERE cat_id = $edited_category_id ";
$update_query = mysqli_query($connection,$update_cat_query);
checkQuery($update_query);
}
?>
there is a button to update the category, the category name is getting into the input field in the code above.
when I click the UPDATE button a new field is revealed via include "includes/update_categories.php";
containing the category name inside it.
when I change the name in the input field and click "Update Category" under the new field of category name I get nothing and nothing changes in the DB.
also, no errors presented...
any ideas? thank you!
Upvotes: 0
Views: 592
Reputation: 23
OK
After a long searching for the answer including the wonderful people here trying to help, I have figured it out.
The problem was in this code:
if (isset($_POST['update_category'])) {
include "includes/update_categories.php";
}
The problem was that when i clicked the "Update" button created in the code above, it included the query for getting the value from the SQL field AND the query to update the value in the SQL.
The thing is when i click the "Update" button again in the code above, the POST is changing to if(isset($_POST['update_cat_title']))
instead.
The result is that the if (isset($_POST['update_category']))
POST in no longer avaliable, which causes the include
of the form to be removed.
Therefore, the UPDATE query is no longer in this page and the results are not sending.
So i put this:
if(isset($_POST['update_cat_title'])){
$newCatName = $_POST['updated_cat_name'];
$catId = $_POST['category_id'];
update_category($newCatName, $catId);
}
(could have put all the function in the same file but decided to move it to the "functions.php" file instead).
So i created this function:
function update_category($newCatName, $catId){
global $connection;
$updated_cat_title = escape($newCatName);
$edited_category_id = escape($catId);
$update_cat_query = "UPDATE categories SET cat_title = '$updated_cat_title' WHERE cat_id = $edited_category_id ";
$update_query = mysqli_query($connection,$update_cat_query);
checkQuery($update_query);
header("Location: categories.php");
}
Now everything works great, Thanks All!
Upvotes: 0
Reputation: 233
$_POST['update_category_id']
is accessed first thing in your code but there is no input for update_category_id
.
Did you missed to set a hidden input for update_category_id
in the form?
You may edit your code to add hidden input around
<input class="form-control" type="text" name="updated_cat_name" ...
<input type="hidden" name="update_category_id"
value=<?php echo $selected_cat_id; ?> />
Edit :
update_category_id
in "functions.php" should have action="categories.php"
.update_category
set in "categories.php" that is being checked in if (isset($_POST['update_category']))
?<input type="hidden" name="update_category_id" ...
in "functions.php" just passed to "categories.php" to be able to display information of selected "update_category_id", where as <input type="hidden" name="update_category_id" ...
is required save the changes made in form in file "includes/update_categories.php".Upvotes: 1