I want to fetch row values of the table whenever I click on that row.
In this I have to display data of the table in row..
Whenever I click on the row I need to return other variables values of that row ..
Like When I am clicking on the subject I need to show Message of respective subject from database.
PHP Code to return row values from database.
This snippet will not run as it is PHP code(just add in the snipppet to good formatting )
In this image, Popup Model is not showing correct subject and message ..
I need this particular subject and message in the popup
<?php
include('../config/conn.php');
$sql = "SELECT * FROM helpdesk where user_id='$user_id'";
$result = $conn->query($sql);
$sr=1;
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$user_id=$row["user_id"];
$pooler_id=$row["pooler_id"];
$date=$row["date"];
$subject=$row["subject"];
$req=$row['req_id'];
$message=$row['message'];
$sql1 = "SELECT * FROM pooler where id='$pooler_id' ";
$result1 = $conn->query($sql1);
if ($result1->num_rows > 0) {
// output data of each row
while($row1 = $result1->fetch_assoc()) {
$username=$row1['user_name'];
$email=$row1['email'];
}
}
echo ' <tr>
<td>'.$sr.'</td>
<td>'.$username.'</td>
<td><a href="#" data-toggle="modal" data-target="#myModal">'.$subject.'</td>
<td>'.$date.'</td>
</tr>';
$sr++;
}
} else {
echo "0 results";
}
?>
Modal POP Code where Sender Name, Subject and Message should be displayed.
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title"> <?php echo $username; ?> <br><br> <?php echo $subject; ?> </h4>
</div>
<div class="modal-body">
<p>
<?php
echo $message;
?>
</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
In your html code
<td><a href="#" data-toggle="modal" data-target="#myModal" onclick="showData('.$pooler_id.')">'.$subject.'</td>
Please pass your unique id in javascript function.
Modal
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">
<span id="user_name"><?php echo $username; ?></span> <br><br> <span id="subject"><?php echo $subject; ?></span> </h4>
</div>
<div class="modal-body">
<p id="msg">
<?php
echo $message;
?>
</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
Script
<script>
function showData(pooler_id)
{
$.ajax({
type: POST,
url : 'some_file.php',
data: {pooler_id: pooler_id},
success: function(response){
var resp = JSON.parse(response);
$('#user_name').html(resp.user_name);
$('#subject').html(resp.subject);
$('#msg').html(resp.msg);
}
});
}
</script>
In some_file.php
$pooler_id = $_POST['pooler_id'];
//get records of your primary key json_decode it
//get details from database
//this will be result fetched from database
$responseData['user_name'] = $name;
$responseData['subject'] = $subject;
$responseData['msg'] = $msg;
echo json_decode($responseData);
If your issue is not resolved yet and you try to avoid usage of JSON:
You could try to add an individual id (e.g. $sr) to your <div class="modal-content"> id property and <a href="#" data-toggle="modal" data-target="#myModal">'.$subject.' call in view. Got this from http://www.webdesignerdepot.com/2012/10/creating-a-modal-window-with-html5-and-css3/
Maybe this works for you.
Add to your PHP code <a href="#modal-content-'.$sr.'" data-toggle="modal" data-target="#myModal">'.$subject.':
<?php
include('../config/conn.php');
$sql = "SELECT * FROM helpdesk where user_id='$user_id'";
$result = $conn->query($sql);
$sr=1;
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$user_id=$row["user_id"];
$pooler_id=$row["pooler_id"];
$date=$row["date"];
$subject=$row["subject"];
$req=$row['req_id'];
$message=$row['message'];
$sql1 = "SELECT * FROM pooler where id='$pooler_id' ";
$result1 = $conn->query($sql1);
if ($result1->num_rows > 0) {
// output data of each row
while($row1 = $result1->fetch_assoc()) {
$username=$row1['user_name'];
$email=$row1['email']; //note this value is never used
echo ' <tr>
<td>'.$sr.'</td>
<td>'.$username.'</td>
<td>
'.$subject.'
</td>
<td>'.$date.'</td>
</tr>';
$sr++;
}
}
}
} else {
echo "0 results";
}
?>
and to your Modal <div class="modal-content-<?php echo $sr; ?>">:
<!-- Modal content -->
<div class="modal-content-<?php echo $sr; ?>">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">
<span id="user_name"><?php echo $username; ?></span> <br><br> <span id="subject"><?php echo $subject; ?></span> </h4>
</div>
<div class="modal-body">
<p id="msg"><?php echo $message; ?></p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
Related
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'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>
This question already has answers here:
PDO and nested fetching
(2 answers)
Closed 4 years ago.
Desired result: Loop an equivalent amount of modals as ID's within my database table1 whilst successfully looping images from table2 within those looped modals.
Current result: Loop one modal period whilst looping all images from table2 inside that one successful modal.
What do, and how?
<?php
include("dbconfig.php");
/*include("class.user.php");*/
$user_id = $_SESSION['user_session'];
$user_name = $_SESSION['user_name'];
$stmt = $DB_con->prepare("SELECT * FROM comment_imgs");
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
$id = $row['id'];
$date = $row['date'];
$comment = $row['comment'];
$project_id = $row['project_id'];
$display_id = $row['display_id'];
$user_name = $row['user_name'];
?>
<div class="card" style="width: 18rem;" id="display">
<div class="card-body">
<h5 class="card-title"><?php echo $project_id;?></h5>
<p class="card-text"><?php echo $date;?></p>
<p class="card-text"><?php echo $user_name;?></p>
<p class="card-text"><?php echo $comment;?></p>
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary btn-block" data-toggle="modal" data-target="#exampleModal<?php echo $id;?>" id="formButtons">
Button
</button>
<!-- Modal -->
<div class="modal fade" id="exampleModal<?php echo $id;?>" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModal<?php echo $id;?>Label"><?php echo $project_id;?></h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<?php echo $date;?>
<?php echo $comment;?>
<?php echo $user_name;?>
<?php
$stmt = $DB_con->prepare("SELECT * FROM uploads");
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
$image_path = $row["image_path"]."/".$row["image_name"];
$display_id = $row['display_id'];
?>
<img src="<?php echo $image_path; ?>" class="images" /><?php } ?>
</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 changes</button>
</div>
</div>
</div>
</div>
</div>
</div>
<?php
}
?>
</div>
table1:
Table 1 (comment_imgs)
table2:Table 2 (Uploads)
Change the result variable $stmt in inner loop to something else, since you are overwriting the variable with which you are iterating.
$stmt = $DB_con->prepare("SELECT * FROM comment_imgs");
$stmt->execute();
$stmt = $DB_con->prepare("SELECT * FROM uploads");
$stmt->execute();
// the variable result is overwritten
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>
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!