display and edit mysqli record in modal window - php

I am pulling records from a MySQL table and output them in a table on my webpage. Now I am in need of being able to edit the records which I tried to accomplish by placing an edit button at the end of each row, which is then supposed to open the record details in my bootstrap modal.
Sadly, this doesnt work as the passed on variables are all for the last entry of the queried records. How do I create a modal link that addresses the record of the specified row?
Here what I have so far.
Query:
<?php
// Start MySQLi connection
include '../../plugins/MySQL/connect_db.php';
$db = new mysqli($dbhost,$dbuser,$dbpass,$dbname);
if($db->connect_errno > 0){
die('Unable to connect to database [' . $db->connect_error . ']');
}
// Build basic query
$sql = ("SELECT CRS, ROOM, ARR, DEP, NTS, TITLE, FIRST, LAST, NAT, IHG_LVL, EMAIL FROM qci_poststay_ADMIN_temp");
// run the query or show an error message
if(!$result = $db->query($sql)){
echo('There was an error running the query [' . $db->error . ']');
}
while($row = mysqli_fetch_array($result)){
$crs = $row['CRS'];
$room = $row['ROOM'];
$arrival = $row['ARR'];
$departure = $row['DEP'];
$nights = $row['NTS'];
$nationality = $row['NAT'];
$title = $row['TITLE'];
$first = $row['FIRST'];
$last = $row['LAST'];
$lvl = $row['IHG_LVL'];
$email = $row['EMAIL'];
echo "
<tr>
<td id=\"crs\">$crs</td>
<td>$room</td>
<td>$arrival</td>
<td>$departure</td>
<td>$nights</td>
<td>$nationality</td>
<td>$title</td>
<td>$first</td>
<td>$last</td>
<td>$lvl</td>
<td>$email</td>
<td>00</td>
<td>yyyy-mm-dd</td>
<td>
<a id=\"Send_Mail\" class=\"btn btn-block btn-primary btn-xs\" target=\"_blank\" href=\"./sendmail.php?crs=$crs\" method=\"POST\">Send</a>
<button type=\"button\" class=\"btn btn-block btn-warning btn-xs\" data-toggle=\"modal\" data-target=\"#edit\">Edit</button>
</td>
</tr>";
}
//$result->free();
$db->close();
?>
Modal:
<!-- Modal -->
<div class="modal fade" id="edit" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<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="myModalLabel">Edit Details</h4>
</div>
<div class="modal-body">
<table class="table">
<tr>
<?php
echo "
<td>CRS No.: </td><td>$crs</td>";
?>
</tr>
</table>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
<!-- /.Modal -->
Much appreciate some help on this.
Thanks!

Related

How to delete a row printed in a while loop using a modal window in PHP, MYSQL

I'm very noob in PHP, but i'm really stuck here trying to figure out how to delete a row printed in a while loop here :
<?php
$sql5 = "SELECT * FROM user_exp WHERE id=".$_SESSION["ID"]."";
$result3 = mysqli_query($conn, $sql5);
if (mysqli_num_rows($result3) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result3)) {
echo "<h4>" . $row["exp_title"]. "<a href='#' title=''><i class='fa fa-pencil'></i><i onclick='location.href='userprofile.php?deleteID=".$row["auto_id"]."';' class='fa fa-minus-square' data-toggle='modal' data-target='#EXPDELModal' value='delete_exp'></i></a></h4>";
echo "<p>" . $row["exp_detail"]. "</p>";
}
}
else
{
echo "<div class='textfaded2'>Add you experience here.</div>";
}
?>
Note: i want to delete the row using a font-awesome icon and i'm using
'auto_id' as the auto_increment, primary key to define the row i want
to delete.
Here is the code for the modal:
<div class="modal fade" id="EXPDELModal" role="dialog">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Delete Experience</h4>
</div>
<div class="modal-body">
<h3>Are you sure?</h3>
<br>
<br>
</div>
<div class="modal-footer">
<form action="userprofile.php?deleteID=<?php '.$row["auto_id"].';?>"
style="width: 100%;" method="post" value="delete_exp">
<button type="button" name="delete" class="btn btn-default" data-
dismiss="modal">YES</button>
<button type="button" class="btn btn-default" data-
dismiss="modal">NO</button>
</form>
</div>
</div>
</div>
</div>
And finally the query for delete:
<?php
if(isset($_POST['delete']))
{
$sql6="DELETE FROM `user_exp` WHERE auto_id=".$_GET['deleteID']."";
$result=mysqli_query($conn,$sql6) or die();
}
?>
I would like to thank you for taking the time to read this.
<?php
$sql5 = "SELECT * FROM user_exp WHERE id=" . $_SESSION["ID"] . "";
$result3 = mysqli_query($conn, $sql5);
if (mysqli_num_rows($result3) > 0) {
while ($row = mysqli_fetch_assoc($result3)) {
?>
<h4><?php echo $row["exp_title"]; ?>
<a href='#' title=''>
<i class='fa fa-pencil'></i>
<i onclick='setValue("<?php $row["auto_id"]; ?>");' class='fa fa-minus-square' data-toggle='modal'
data-target='#EXPDELModal'></i>
</a>
</h4>
<p><?php echo $row["exp_detail"]; ?></p>;
<?php
}
} else {
echo "<div class='textfaded2'>Add you experience here.</div>";
}
if (isset($_POST['delete'])) {
if($_POST['deleteID']) {
$sql6 = "DELETE FROM `user_exp` WHERE auto_id=" . $_POST['deleteID'] . "";
$result = mysqli_query($conn, $sql6) or die();
}
}
?>
<script>
function setValue(value) {
document.getElementById("deleteId").value = value;
}
</script>
<div class="modal fade" id="EXPDELModal" role="dialog">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Delete Experience</h4>
</div>
<div class="modal-body">
<h3>Are you sure?</h3>
<br>
<br>
</div>
<div class="modal-footer">
<form action="userprofile.php" style="width: 100%;" method="post" name="delete_exp">
<input type="hidden" value="0" name="deleteID" id="deleteId" readonly/>
<button type="submit" name="delete" class="btn btn-default" data-dismiss="modal">YES</button>
<button type="button" class="btn btn-default" data-dismiss="modal">NO</button>
</form>
</div>
</div>
</div>
</div>

Value of the php variable fetched from mysql database cannot display in bootstrap modal

I would to fetch the mysql database table rows and display it into the html tables after that when user click on the value in table, bootstrap modal trigger and display all the column values of that clicked row in modal.
Here is php code
<tbody>
<?php
include "connection.php";
$sql="SELECT email,date_t from users";
$result=mysqli_query($conn,$sql);
while($row=mysqli_fetch_array($result))
{
$mail=$row['email'];
$getID = mysqli_fetch_assoc(mysqli_query($conn, "SELECT fname,email from users where email='$mail'"));
$fname = $getID['fname'];
$email = $getID['email'];
echo '<tr>
<td>'.$fname.'</td>
<td>'.$row['email'].'</td>
<td>'. "$email" .'</td>
<td>'.$row['date_t'].'</td>
</tr>';
}
?>
</tbody>
Here is bootstrap modal
<div id="mymodel" 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">Email Reply</h4>
</div>
<div class="modal-body">
<p><?php echo $email; echo $fname; ?></p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
It will display value of $fname in modal but not display the value of $email
Any help will be appreciated!

Delete Query Inside While Loop

i'm trying to delete database record inside a while loop. I'm showing my user list in a table with while loop. I have a button, bootstrap modal opening a modal window. In that window i have submit to delete button. With while loop.
Problem is, i'm trying to delete this record, but its deleting random record. can you check is there a problem?
Thanks already.
here is my code:
<table class="table table-hover">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Surname</th>
<th>Email</th>
<th>Password</th>
<th>*</th>
</tr>
</thead>
<tbody>
<?php
$q = "SELECT * FROM users";
$r = mysqli_query($dbc,$q);
while($list = mysqli_fetch_assoc($r)){
if(isset($_POST['del_submit'])){
$q = "DELETE FROM users WHERE id = '$list[id]' ";
$r = mysqli_query($dbc, $q);
header('Location: index.php?page=7');
}
echo '<tr>';
echo '<td>'.$list['id'].'</td>';
echo '<td>'.$list['name'].'</td>';
echo '<td>'.$list['surname'].'</td>';
echo '<td>'.$list['email'].'</td>';
echo '<td>'.$list['password'].'</td>';
echo '<td><button class="btn btn-danger btn-xs" data-toggle="modal" data-target=".delete'.$list['id'].'"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span></button> ';
echo '</tr><form method="post" action="#">';
echo '<div class="modal fade delete'.$list['id'].'">
<div class="modal-dialog modal-sm" 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">Delete <strong class="text-primary">'.$list['name'].' ?</strong></h4>
</div>
<div class="modal-body">
<strong class="text-primary">'.$list['name'].' '.$list['surname'].'</strong><br>
Are you Sure?
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="submit" name="del_submit" id="del_submit" class="btn btn-danger"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span> Delete</button>
</div>
</div>
</div>
</div></form>';
}
?>
</tbody>
</table>
Create a delete page and link to it.
first you need to add link to the same page with action and id parameters
<?php
// replace
?>
<button type="submit" name="del_submit" id="del_submit" class="btn btn-danger"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span> Delete</button>
<?php
// with this:
?>
<input type='hidden' name='id' value='<?= $list['id']; ?>'>
<input type='hidden' name='action' value='delete'>
<button class="btn btn-danger" type='submit'><span class="glyphicon glyphicon-trash" aria-hidden="true"></span> Delete</button>
add this to the top of your page
<?php
if(isset($_POST['id'], $_POST['action']) && $_POST['action'] === 'delete')
{
$id = $_POST['id'];
$query = 'DELETE FROM `users` WHERE `id` = ?';
$db = new Mysqli('localhost','user','password','database');
$stmt = $db->prepare($query);
$stmt->bind_param('i',$id);
$stmt->execute();
$stmt->close();
$db->close();
echo 'Deleted! (if exists)';
}
i will first acknowledge that your script is not safe and efficient this way. Why not handle delete operations with Ajax and call to a different script?
You have to pass the value of the id with the delete button and then use the id value you passed with the button instead.
For your delete button inside the modal add value attribute
<button type="submit" name="del_submit" value="'.$list['id'].'" id="del_submit" class="btn btn-danger"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span> Delete</button>
Then retrieve the id and use the id. Its should work
if(isset($_POST['del_submit'])){
$listItemID = $_POST['del_submit'];
$q = "DELETE FROM users WHERE id = '$listItemID' ";
$r = mysqli_query($dbc, $q);
header('Location: index.php?page=7');
}
If where you are redirecting the user is with the id of the deleted element simply use
`header('Location: index.php?page=$listItemID'); instead of header('Location: index.php?page=7');
Full code becomes
<table class="table table-hover">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Surname</th>
<th>Email</th>
<th>Password</th>
<th>*</th>
</tr>
</thead>
<tbody>
<?php
$q = "SELECT * FROM users";
$r = mysqli_query($dbc,$q);
while($list = mysqli_fetch_assoc($r)){
if(isset($_POST['del_submit'])){
$listItemID = $_POST['del_submit'];
$q = "DELETE FROM users WHERE id = '$listItemID' ";
$r = mysqli_query($dbc, $q);
header('Location: index.php?page=7');
}
echo '<tr>';
echo '<td>'.$list['id'].'</td>';
echo '<td>'.$list['name'].'</td>';
echo '<td>'.$list['surname'].'</td>';
echo '<td>'.$list['email'].'</td>';
echo '<td>'.$list['password'].'</td>';
echo '<td><button class="btn btn-danger btn-xs" data-toggle="modal" data-target=".delete'.$list['id'].'"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span></button> ';
echo '</tr><form method="post" action="#">';
echo '<div class="modal fade delete'.$list['id'].'">
<div class="modal-dialog modal-sm" 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">Delete <strong class="text-primary">'.$list['name'].' ?</strong></h4>
</div>
<div class="modal-body">
<strong class="text-primary">'.$list['name'].' '.$list['surname'].'</strong><br>
Are you Sure?
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="submit" name="del_submit" value="'.$list['id'].'" id="del_submit" class="btn btn-danger"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span> Delete</button>
</div>
</div>
</div>
</div></form>';
}
?>
</tbody>
</table>

displayed html table with button in each row using php code, but not able to access individual row through that button

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.

PHP Simple Html Dom Parser - "double" dimension arrays

I realize there are no multi-dimension arrays with PHP, however here is the issue.
I have this inside a for ($fix) loop:
$html->find('tr[class] td', $fix);
Now in a row there are more than one td being grabbed, so say the first one holds the username
I would like to grab the username, and this is what I'm trying but its not working:
$username = $html[0];
I also tried something like this
$username = $html->find('tr[class] td', $fix, 0)
Neither of these works, what should I be using?
Full Code:
<?php
include('simple_html_dom.php');
$q=$_REQUEST["q"]; $hint="";
$ctx = stream_context_create(array(
'http' => array(
'timeout' => 15
)
)
);
$html = file_get_html('http://www.example.com/index.php?q=' . urlencode($q), 0, $ctx);
$tr = $html->find('tr');
// lookup all hints from array if $q is different from ""
$fix = 0;
if ($q !== "") {
$q=strtolower($q); $len=strlen($q);
foreach($tr as $name) {
$fix++;
if($fix > 2) {
$selector = $html->find('tr[class] td');
$username = $selector[$fix][0]->plaintext;
$name = $selector;
$username_fix = str_replace ('.',"",$username);
if ($hint==="") {
$hint='<tr>
<td>' . $name->plaintext . '<br />' . $username . '</td>
<td> </td>
<td><button type="button" class="btn btn-sm btn-primary" data-toggle="modal" data-target="#M' . $username_fix . '">This is me!</button></td>
<div class="modal fade" id="M' . $username_fix . '" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">Yo</h4>
</div>
<div class="modal-body">
<p>Hey…' . $name->plaintext . '</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
</tr>';
} else {
$hint .='<tr>
<td>' . $name->plaintext . '<br />' . $username . '</td>
<td> </td>
<td><button type="button" class="btn btn-sm btn-primary" data-toggle="modal" data-target="#M' . $username_fix . '">This is me!</button></td>
<div class="modal fade" id="M' . $username_fix . '" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">Yo</h4>
</div>
<div class="modal-body">
<p>Hey…' . $name->plaintext . '</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
</tr>';
}
}
}
}
// Output "no suggestion" if no hint were found
// or output the correct values
if($hint === "") {
echo 'My records indicate you a liar!';
} else {
echo '
<table class="table table-striped table-bordered">
<tr>
<th style="min-width:20%;">Name</th>
<th>Department</th>
<th width="9%"> </th>
</tr>
';
echo $hint;
echo '</table>';
}
?>
still dirty as ever, would like it functional first

Categories