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')";
Related
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>
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.
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 } ?>
This is my code, i was supposed to get the "$pw" outside the modal.. But it seems that i can't get the value of it and Also i can't seem to fetch the "$errMSG" value.. If it's if($pin != $pww) cant get the err MESSAGE.. Really need some help.. Thank you!
This is my "$pw" code, its on the same page as my modal..
<?php
include('connectdb.php');
$sql = "SELECT * FROM accounts WHERE usernamee='$userid'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($data = $result->fetch_assoc()) {
$pww = $data['passwordd'];
}
}
?>
This is my modal code:
<div class="container">
<form method="post">
<h2>Enter your Pin Number</h2>
<!-- Trigger the modal with a button -->
<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open Modal</button>
<!-- Modal -->
<div class="modal fade" id="myModal" 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">Enter your Pin Number</h4>
</div>
<div class="modal-body text-center">
<input type="password" id="first-name" required="required" class="form-control" name="pin">
<?php
if(isset($errMSG)){
?>
<div class="alert alert-danger">
<span class="glyphicon glyphicon-infos-sign"></span> <strong><?php echo $errMSG; ?></strong>
</div>
<?php
}
else if(isset($successMSG)){
?>
<div class="alert alert-success">
<strong><span class="glyphicon glyphicon-info-sign"></span> <?php echo $successMSG; ?></strong>
</div>
<?php
}
?>
</div>
<div class="modal-footer">
<button type="submit" name="submit3" class="btn btn-default"><i class="fa fa-key"></i> Confirm Pin</button>
</div>
</div>
</div>
</div>
<!-- START OF PHP -->
<?php include ('connectdb.php');
if(isset($_POST['submit3']))
{
$pin = $_POST["pin"];
if ($pin != $pww) {
$errMSG= "WRONG PIN.. TRY AGAIN..";
}
else{
header('Location: myorders.php');
}
} // SUBMIT
?>
</form>
</div>
include('connectdb.php');
$pww = '';
$sql = "SELECT * FROM accounts WHERE usernamee='$userid'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($data = $result->fetch_assoc()) {
$pww = $data['passwordd'];
}
}
Try this code, You should declare pww before your if or while brackets, to get acces to this variable.
I displayed a html table within a php while loop extracting some data from a database and included an edit button in the rows of the table to edit the entries in that row (database finally). But , irrespective of on which edit button I click on , it is editing the first row elements only. How can I fix it?
<div class="col-md-8">
<?php
$test=1;
if(!empty($_SESSION['uid']))
{
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "Hostel";
$test++;
$flag=FALSE;
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}
// $id=$_POST['complid'];
$c_name=$_SESSION['name'];
$c_usn=$_SESSION['uid'];
?>
<div class="container col-md-12" >
<h1 style="text-color:white;">The posts are listed below :</h1>
<hr style="box-shadow:1px 1px 1px black;">
<style>
h1
{
color: white;
}
</style>
<div class="table-responsive" style="border-radius:10px;border:2px solid gray;box-shadow:10px 10px 10px black;background-color:#d2d2d2;">
<table class="table table-bordered table-hover">
<tbody>
<tr>
<th>Post.No.</th>
<th>Date</th>
<th>Title</th>
<th>Operations</th>
</tr>
<?php
$query = "SELECT * FROM posts";
$result = mysqli_query($conn,$query);
while ($row=mysqli_fetch_array($result,MYSQLI_ASSOC))
{
?>
<tr>
<td><?php echo $row{'id'};?></td>
<td><?php echo $row{'date'};?></td>
<td><?php echo $row{'title'};?></td>
<td>
<b><button type="button" id="editbut" style="color:yellow;background-color:black;border:1px solid gray;border-radius:3px;" data-toggle="modal" data-target="#exampleModal">Edit</button> </b>
<b><button type="button" style="color:white;background-color:red;border:1px solid gray;border-radius:3px;" data-toggle="modal" data-target="#exampleModa2">Delete</button> </b>
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
<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="exampleModalLabel">Edit your post here</h4>
</div>
<div class="modal-body">
<form>
<div class="form-group">
<label for="message-text" class="control-label">Title :</label>
<textarea class="form-control" id="message-text"><?php echo $row['title']; ?></textarea>
<label for="message-text2" class="control-label">Matter :</label>
<textarea class="form-control" id="message-text"><?php echo $row['matter']; ?></textarea>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="button" class="btn btn-primary">Edit Post</button>
</div>
</div>
</div>
</div>
<script type="text/javascript">
posttitle = "<?php echo $row{'title'}; ?>";
postid = "<?php echo $row{'id'}; ?>";
document.getElementById("deletebut").onclick = function () {
alert(postid);
//location.href = "deletepost.php?id="+postid;
};
</script>
<div class="modal fade" id="exampleModa2" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
<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="exampleModalLabel">Confirm your delete request:</h4>
</div>
<div class="modal-body">
<label for="message-text" class="control-label">Are you sure, you want to delete this post?</label>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="button" id="deletebut" class="btn btn-danger">Delete post</button>
</div>
</div>
</div>
</div>
</td>
</tr>
<?php
}
}
else
{
echo "<script type='text/javascript'alert(\"Please Login.\");>window.location.href = 'sign.php';</script>";
exit();
}
?>
</tbody>
</table>
</div>
</div>
Your edit button is not contained in a form.
The form labelled "edit your post" does not contain the database ID of the post.
Your JavaScript handler for the delete button references an element ID which does not exist.
You have multiple form elements with the same ID and no name
I could go on, but this is pretty badly broken.