Unable to delete row from mysqli in php - php

I have defined Three php file where first as function.php , categories.php and view_all_categories.php
Function.php includes code to delete the row from variables value passed from categories.php. Category.php includes view_all_categories.php inside. When clicked on Delete Button, Modal Box appears and on Confirmation, it doesn't delete the corresponding row. Please Help
Here is the Code for Category.php
<?php include"includes/delete_modal.php"; ?>
<div id="wrapper">
<!-- Navigation -->
<?php include"includes/admin_navigation.php"; ?>
<div id="page-wrapper">
<div class="container-fluid">
<div class="row">
<div class="col-xs-6">
<table class="table table-hover table-bordered">
<thead class="thead-dark">
<tr>
<th>Id</th>
<th>Category Title</th>
<th>Options</th>
</tr>
</thead>
<tbody>
<?php //Displasy table from categories
include "includes/view_all_categories.php";?>
<?php //delete categories
delete_categories();
?>
</tbody>
</table>
</div>
</div>
</div>
<!-- /.row -->
</div>
<!-- /.container-fluid -->
</div>
<!-- /#page-wrapper -->
</div>
<!-- /#wrapper -->
<script>
$('#myModal').on('show.bs.modal', function (e) {
$(this).find('.modal_delete_link').attr('href', $(e.relatedTarget).data('href'));
});
</script>
Similarly code inside function.php is
function delete_categories(){
global $connection;
if(isset($_GET['delete'])){
$del_id = $_GET['delete'];
$query = "DELETE FROM categories WHERE cat_id = $del_id";
$del_cat = mysqli_query($connection,$query);
$query = "DELETE FROM posts WHERE post_category_id=$del_id";
$del_category_rel_post = mysqli_query($connection,$query);
header("Location: categories.php");
}
and view_all_categories.php file include
<?php
$query = "SELECT * FROM categories";
$get_categories = mysqli_query($connection, $query);
while($row = mysqli_fetch_assoc($get_categories)){
$cat_id = $row['cat_id'];
$cat_title = $row['cat_title'];
echo "<tr><td>{$cat_id}</td><td>{$cat_title}</td>
<td><a class='btn btn-danger' data-toggle='modal' data-target='#myModal' data-href='categories.php?delete=$cat_id' href='javascript:void(0)'>Delete</a> <a class='btn btn-primary' href='categories.php?update={$cat_id}'>Update</a></td></tr>";
}
?>
and delete_modal.php has
<div id="myModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal Content -->
<div class="modal-content">
<div class="modal-container">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Delete Confirm Box</h4>
</div>
<div class="modal-body">
<p> Are you sure you want to delete?</p>
</div>
<div class="modal-footer">
<a class="btn btn-danger modal_delete_link" href="">Delete</a>
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
</div>
</div>
</div>
</div>
</div>
Any help is very useful.

Related

How to display result of query in bootstrap modal?

Good afternoon colleagues, I am in a web design course, this work was presented to me, which is a search page, which should show the user's information in a MODAL, obtaining it from the database.
I managed to make the Modal work, but, it throws all the information of the table, when I try to do the query, it does not show me anything.
I hope you can guide me with this detail
<?php
//Conexion para el Modal
$con =mysqli_connect("localhost", "root","");
$db = mysqli_select_db($con, 'requi');
?>
<!DOCTYPE html>
<html lang="en">
<head>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container-fluid">
<a class="navbar-brand" href="./">Search panel</a>
</nav>
<meta charset="UTF=8">
<hr weight= "100%">
<title>Buscar requisicion</titLe>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="col-md-5">
<h2>Buscar requisición</h2>
<div class="form-group">
<form class="form-outline">
<input class="form-control mr-sm-2" type="search" placeholder="Ingresa el ID" name ="id" aria-label="Search">
<!--<button class="btn btn-success my-2 my-sm-0" type="submit" name="search">Buscar</button>-->
<button type="button" class="btn btn-info btn-sm" data-toggle="modal" data-target="#myModal" name="smodal">Busqueda</button>
</div>
<!-- Modal -->
<div id="myModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Mail Search</h4>
</div>
<div class="modal-body">
<p>Your data: </p>
</div>
<div>
<table style='border-collapse: collapse;'>
<tr>
<th>Email</th>
<th>Name</th>
<th> </th>
</tr>
<?php
if(isset($_POST['search'])) {
$id = $_POST['id'];
$query = "select * from info WHERE id = '$id'";
$result = mysqli_query($con,$query);
while($row = mysqli_fetch_array($result)){
$id = $row['id'];
$correo = $row['correo'];
$nombre = $row['nombre'];
echo "<tr>";
echo "<td>".$correo."</td>";
echo "<td>".$nombre."</td>";
echo "</tr>";
}
}
?>
</table>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cerrar</button>
</div>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
</body>
</html>

CRUD edit model data now shown

I got some problem using jquery for displaying existing data when crud edit button is click. The bootstrap popup windows is shown if I remove $('#catid).val(data[0]); and $('#catname).val(data[1]); from the code.If add back the code, the bootstrap popup windows not showing without error. Not sure it is the script problem or the form problem. Anyone can help! Thanks.
enter code here
<!-- Edit Model -->
<div class="modal fade" id="editcatmodel" tabindex="-1" role="dialog" aria-
labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">New message</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<input type="hidden" name="catid" id="catid">
<div class="form-group">
<label>Category Name</label>
<input type="text" name="catname" id="catname" class="form-control">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">save</button>
</div>
</div>
</div>
</div>
<!-- /.modal -->
<!-- Main screen display the crud data with edit button -->
<div class="container">
<table class="table">
<thead>
<th>ID</th>
<th>Category</th>
<th>Action</th>
</thead>
<tbody>
<?php
require_once 'source/db_connect.php';
$sql = "SELECT * FROM category";
$result = mysqli_query($link, $sql);
if ($result->num_rows > 0) {
//output data of each row
while ($row = $result->fetch_assoc()) {
?>
<tr>
<td><?php echo $row['catid']; ?></td>
<td><?php echo $row['catname']; ?> </td>
<td> <button type="button" class="btn btn-primary editcatbtn">edit</button> </td>
</tr>
<?php }
}
?>
<!-- script display the modal -->
<script>
$(document).ready(function () {
$(' .editcatbtn').on('click', function() {
$('#editcatmodel').modal('show');
$tr = $(this).closest('tr');
var data = $tr.children("td").map(function() {
return $(this).text();
}).get();
console.log(data);
$('#catid).val(data[0]);
$('#catname).val(data[1]);
});
});
</script>

bootstrap modal inside a php while loop to display different infornmation

so i have a while loop and i am fethcing the name and the description of them from my db
so when ever i click on one of the parts i want the modal to display the name of the item that i clicked on in the modal i hope this picture would describe it better
below is the code i have so far
at the moment when i click on the modal i get displayed the name of the item which is first on the list no matter where i click
while($row = mysqli_fetch_assoc($result))
{
$name= $row['name_of_product'];
$description = $row['description']
?>
<a href="#" data-toggle="modal" data-target="#mymodal">
<div class="col-sm-4">
<?php echo name; ?>
<?php echo description ; ?>
</div>
</a>
<div id="mymodal" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">
<p><?php echo $name; ?></p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<?php } ?>
You are not doing things the way you have to do. You are re-creating your mymodal for every iteration of your while loop which is not a better way to achieve what you want.
Follow these steps:
Create your mymodal outside of your while loop.
Pass the ID of current row to a javascript function and populate the data of that id using javascript.
I have set the things which needs to be done. Try the following code
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<!--- display table header row-->
<table style="width:100%">
<tr>
<th>name</th>
<th>description</th>
<th>Action</th>
</tr>
<?php
while($row = mysqli_fetch_assoc($result))
{
$name= $row['name_of_product'];
$description = $row['description'];
$id = $row['id']; // I am assuming that your table has auto incremented primary key column by the name of id, if not then replace that by $row['your_id_column']
?>
<!--- desplay the data in a table row -->
<tr>
<td id="<?php echo "name_".$id; ?>"><?php echo $name; ?></td>
<td id="<?php echo "description_".$id; ?>"><?php echo $description ; ?></td>
<td><div href="#" data-toggle="modal" data-target="#mymodal" onClick="showModal(<?php echo $id ; ?>)">Edit</div></td>
</tr>
<?php } ?>
</table>
<!--- Your modal -->
<div id="mymodal" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">
<p id="name"></p> <!--- name will be shown here-->
<p id="description"></p><!--- description will be shown here-->
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<script>
function showModal(id)
{
// setting the values of clicked item in the bootstrap Modal
$("#name").text($("#name_"+id).text());
$("#description").text($("#description_"+id).text());
}
</script>
Looks like you are not serializing the modals... If you wanted to do things that way your code should be something like;
<!-- language-all: lang-html -->
while($row = mysqli_fetch_assoc($result)) {
$procuct_id = $row['ID'];
$name = $row['name_of_product'];
$description = $row['description']
?>
<a href="#" data-toggle="modal" data-target="#mymodal_<?php echo $procuct_id;?>">
<div class="col-sm-4">
<?php echo name; ?>
<?php echo description ; ?>
</div>
</a>
<div id="mymodal_<?php echo $procuct_id;?>" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">
<p><?php echo $name; ?></p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<?php }
<--
A Better way to do it would be to write a javascript function to show the generic modal window, then run an ajax call to load your info into the "modal-body" div. You would have to pass a unique id to the call in order to have your ajax script hit the db and get the info to display.
It's been almost 4 years but I'm writing for everyone; change data-target(id) as row id.
edited code:
while($row = mysqli_fetch_assoc($result))
{
$name= $row['name_of_product'];
$description = $row['description']
?>
<a href="#" data-toggle="modal" data-target="#<?php echo $row['id']; ?>">
<div class="col-sm-4">
<?php echo name; ?>
<?php echo description ; ?>
</div>
</a>
<div id="<?php echo $row['id']; ?>" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">
<p><?php echo $name; ?></p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<?php } ?>

Submiting form within model not saving data to database

I am trying to do a simple save of data to the database. I have a form in a modal.
When I try to save the data to my database, it will not save. If i place my form out of the modal it will save the data to the database.
I have googled for a solution but found none. The submit button is in my form, so thats not causing the issue.
PHP code:
<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL | E_STRICT);
session_start();
include 'config.php';
if(!isset($_SESSION['user_id'])) {
header("Location: login.php");
}
//read out data from db
$query_string = "SELECT * FROM tbl_Category";
$query = mysqli_query($con, $query_string);
//save data to db
if(isset($_POST['saveCategory'])){
$category = mysqli_real_escape_string($con, $_POST['category']);
$query = "INSERT INTO tbl_Category(category) VALUES('$category')";
$result = mysqli_query($con, $query);
if(!$result){
echo mysqli_error();
}
else{
$category = '';
echo "YES";
}
}
?>
My config.php file:
<?php
$con = mysqli_connect("localhost", "root", "root", "challenger") or die("Error " . mysqli_error($con));
// if($con){
// echo"There is a connection";
// }
?>
Part of my html code:
<!-- Page Content -->
<div id="page-wrapper">
<div class="container-fluid">
<div class="row bg-title">
<div class="col-lg-3 col-md-4 col-sm-4 col-xs-12">
<h4 class="page-title">Category</h4>
</div>
<div class="col-lg-9 col-sm-8 col-md-8 col-xs-12">
<ol class="breadcrumb">
<li>Category</li>
<li>Category</li>
</ol>
</div>
<!-- /.col-lg-12 -->
</div>
<!-- /row -->
<div class="row">
<div class="col-sm-12">
<div class="white-box">
<h3 class="box-title m-b-0">Overview Category</h3>
<button class="btn btn-danger btn-outline pull-right" data-toggle="modal" data-target="#addCategory">Add Category</button>
<br><br><br>
<table id="myTable" class="table table-striped">
<thead>
<tr>
<th>Number</th>
<th>Category</th>
</tr>
</thead>
<tbody>
<?php
while($row = mysqli_fetch_assoc($query)){
echo "<tr>";
echo "<td>".$row['category_id']."</td>";
echo "<td>".$row['category']."</td>";
echo "</tr>";
}
?>
</tbody>
</table>
</div>
</div>
<!-- MODAL WINDOW -->
<div class="modal fade" id="addCategory" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel1">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="exampleModalLabel1">Add a new category</h4>
</div>
<div class="modal-body">
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<div class="form-group">
<label for="recipient-name" class="control-label">Category Name:</label>
<input type="text" class="form-control" id="recipient-name1" name="category">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary" name="saveCategory" data-dismiss="modal">Create Category</button>
</div>
</form>
</div><!-- end modal body -->
</div>
</div>
</div>
<!-- END MODAL WINDOW -->
</div>
<!-- /.row -->
</div>
<!-- /.container-fluid -->
</div>
<!-- /end Page Content -->
change this
$query = "INSERT INTO tbl_Category(category) VALUES('$category')";
to this
$query = "INSERT INTO tbl_Category(`category`) VALUES('$category')";

Store text from modal input field in session

I'm storing data from mysql in SESSION and passing it to another page where I make zip with all data's and download it. So far everything work just perfect.
Now I'm trying to make when user click on download button before to make actual download to enter name for the zip archive in modal window and then download the archive. So far I've made the modal but now I can't figured it out how to pass the input field text into the session. All other data is passed correctly. Here is the source so far
if(! isset($_POST['showcart'])) exit;
echo '<a class="btn btn-danger btn-lg pull-right" style="margin: 10px;" data-toggle="modal" data-target="#myModalNorm">Download All Files</a>
<table class="table table-striped table-bordered table-condensed responsive" id="sort">
<thead>
<tr>
<th>ID</th>
<th>Title</th>
<th>Description</th>
<th>Action</th>
</tr>
</thead>';
foreach ($_SESSION['itemid'] as $i):
$sql = "SELECT * FROM uploads WHERE upload_id = :id";
$result = $pdo->prepare($sql);
$result->bindParam(":id", $i);
$result->execute();
$resArray = $result->fetchAll();
foreach ( $resArray as $row ):?>
<div class="row">
<div class="box-content">
<tbody>
<tr>
<td class="center"><?=$row["upload_id"]?></td>
<td class="center"><?=$row["upload_title"]?></td>
<td class="center"><?=$row["upload_description"]?></td>
</tr>
</tbody>
</div>
</div>
<!-- Modal -->
<div class="modal fade" id="myModalNorm" tabindex="-1" role="dialog"
aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<!-- Modal Header -->
<div class="modal-header">
<button type="button" class="close"
data-dismiss="modal">
<span aria-hidden="true">×</span>
<span class="sr-only">Close</span>
</button>
<h4 class="modal-title" id="myModalLabel">
Please enter the name for the archive
</h4>
</div>
<!-- Modal Body -->
<div class="modal-body">
<form role="form">
<div class="form-group">
<label for="exampleInputEmail1"></label>
<input type="text" class="form-control"
id="archiveName" placeholder="Please enter the name for the archive"/>
</div>
</form>
</div>
<!-- Modal Footer -->
<div class="modal-footer">
<button type="button" class="btn btn-default"
data-dismiss="modal">
Close
</button>
<a href="create_zip.php" class="btn btn-primary">
Download
</a>
</div>
</div>
</div>
</div>
<?php endforeach;?>
<?php endforeach; ?>
</table> </div>
On the create_zip.php I take all from $_SESSION['itemid'] but this <input type="text" class="form-control" id="archiveName" placeholder="Please enter the name for the archive"/>
How to take also this input?
You can achieve this quite easy actually. Firstly wrap your table around <form></form>
So it will happen something like this
if(! isset($_POST['showcart'])) exit;
echo '<form action="create_zip.php" method="post">
<a class="btn btn-danger btn-lg pull-right" style="margin: 10px;" data-toggle="modal" data-target="#myModalNorm">Download All Files</a>
<table class="table table-striped table-bordered table-condensed responsive" id="sort">
<thead>
....
<!-- Modal Body -->
<div class="modal-body">
<div class="form-group">
<label for="archiveName"></label>
<input type="text" class="form-control"
id="archiveName" name="archiveName" placeholder="Please enter the name for the archive"/>
</div>
</div>
<!-- Modal Footer -->
<div class="modal-footer">
<button type="button" class="btn btn-default"
data-dismiss="modal">
Close
<input type="submit" name="submit" value="Download" class="btn btn-primary">
</div>
</div>
</div>
</div>
<?php endforeach;?>
<?php endforeach; ?>
</table> </div></form>
Then in create_zip.php just check if isset submit button and assign variable.
if(isset($_POST['submit']))
{
//your code
$archiveName = $_POST['archiveName'];
// other source
} else { echo 'no archive'; }

Categories