Php, Mysql can't retrieve column data for each row - php

I'm trying to give each modal form a unique id based on results retrieved from my database so that different data will be passed onto the php form processor. I have 1 table that is placed in a form which has the unique value for each row being "mapid"
However, each row also has a modal which I've placed another form in but the only value I'm getting is the first row of the sql query. Please help.
$sql = "SELECT mapid, location, DATE_FORMAT(date,'%d/%m/%y') AS date, status FROM maps ORDER BY status DESC, date DESC ";
$result = $mysqli->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo
'
<form action="map.php" method="post">
<tr>
<td> '. $row["mapid"].' </td>
<td> '. $row["location"].' </td>
<td> ' . ($row["status"]=='0' ? 'Last Done' : (($row["status"]=='1') ? 'Started' : 'Done')).' </td>
<td> '.$row["date"].' </td>
<td> ' .
(($row["status"]=='0') ?
'<input type="hidden" name="mapid" value="'. $row["mapid"].'"/>
<input type="hidden" name="start" value="start"/>
<button class="btn btn-primary" name="getmap" type="submit">Start</button>'
: (($row["status"]=='1') ?
'<input type="hidden" name="mapid" value="'. $row["mapid"].'"/>
<input type="hidden" name="resume" value="resume"/>
<button class="btn btn-danger" type="submit" name="getmap" value="'. $row["mapid"].'">Resume</button>'
: (($row["status"]=='2') ?
'<input type="hidden" name="mapid" value="'. $row["mapid"].'"/>
<input type="hidden" name="process" value="process"/>
<button class="btn btn-primary" type="submit" name="getmap">Process</button>'
: ''))) . '
</td>
</form>
<td>
<button class="btn btn-primary" data-toggle="modal" data-target="#assign['. $row["mapid"].']">Assign</button>
</td>
<div class="modal fade" id="assign['. $row["mapid"].']" tabindex="-1" role="dialog" aria-labelledby="assignLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<form action="work/allocate.php" method="post">
<div class="modal-header">
<h5 class="modal-title" id="assignLabel">Assign Map</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="mapid">
<p> Assign <strong>Map '. $row["mapid"].' - '. $row["location"].' </strong> to:</p>
<input class="form-control" id="name" name="name" type="text" >
</div>
<div class="modal-footer">
<button class="btn btn-primary" name="assigned" id="assigned" type="submit">Assign</button>
</div>
</form>
</tr>
</div>
</div>
</div>
'

Just needed to give the modal a unique id by adding ['. $row["mapid"].'] next to #assign.
<td>
<button class="btn btn-primary" data-toggle="modal" data-target="#assign['. $row["mapid"].']" value"">Assign</button>
</td>
<div class="modal fade" id="assign['. $row["mapid"].']" tabindex="-1" role="dialog" aria-labelledby="assignLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">

Related

pass variable to php modal

I am having this trouble whereby I want to delete items based on their ids. However, in modal, it doesn't get the specific id for me to delete. Instead, it gets the the ids which are the lowest first. For example, if I delete a product with an id of 88, it will deletes the id before it first such as number before 88. How can I delete specifically items with the right id?
<?php
ob_flush();
session_start();
include('includes/header.php');
include('includes/navbar.php');
if($_SESSION['admin_name']){
//do nothing
}
else{
echo "<script type='text/javascript'>window.top.location='http://localhost/CarRentalv3/admin/adminlogin.php';</script>"; exit;
}
$admin_name = $_SESSION['admin_name'];
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href=" http://localhost/CarRentalv3/img/CarRent.ico">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.5.0/css/all.css">
<title>Admin Panel | Admin List</title>
</head>
<body>
<div class="modal fade" id="addadminprofile" 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">Add Admin Data</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<form action="process.php" method="POST">
<div class="modal-body">
<div class="form-group">
<label>Admin Name </label>
<input type="text" name="username" class="form-control" placeholder="Enter Username">
</div>
<div class="form-group">
<label>Email</label>
<input type="email" name="email" class="form-control" placeholder="Enter Email">
</div>
<div class="form-group">
<label>Position</label>
<input type="text" name="position" class="form-control" placeholder="Enter Position">
</div>
<div class="form-group">
<label>Password</label>
<input type="password" name="password" class="form-control" placeholder="Enter Password">
</div>
<div class="form-group">
<label>Confirm Password</label>
<input type="password" name="confirmpassword" class="form-control" placeholder="Confirm Password">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" name="register_btn" class="btn btn-primary">Save</button>
</div>
</form>
</div>
</div>
</div>
<div class="container-fluid">
<!-- DataTales Example -->
<div class="card shadow mb-4">
<div class="card-header py-3">
<h6 class="m-0 font-weight-bold text-primary">Admin Profile
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#addadminprofile">
Add Admin Profile
</button>
</h6>
</div>
<div class="card-body">
<?php
if(isset($_SESSION['success'])&& $_SESSION['success']!=''){
echo '<h2 class="bg-primary text-white">'.$_SESSION['success'].'</h2>';
unset ($_SESSION['success']);
}
if(isset($_SESSION['status'])&& $_SESSION['status']!=''){
echo '<strong>'.'<h2 class="bg-danger text-white">'.$_SESSION['status'].'</strong>'.'</h2>';
unset ($_SESSION['status']);
}
?>
<div class="table-responsive">
<?php
$connection = mysqli_connect("localhost","root","","admindb");
$query = "select * from admin";
$query_run = mysqli_query($connection, $query);
?>
<table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
<thead>
<tr>
<th> ID </th>
<th> ADMIN NAME </th>
<th>EMAIL </th>
<th>POSITION</th>
<th>EDIT </th>
<th>DELETE </th>
</tr>
</thead>
<tbody>
<?php if (mysqli_num_rows($query_run)>0){
while($rows= mysqli_fetch_assoc($query_run)){
?>
<tr>
<td><?php echo $rows['id'];?> </td>
<td><?php echo $rows['admin_name'];?> </td>
<td><?php echo $rows['admin_email'];?></td>
<td><?php echo $rows['admin_position'];?></td>
<td>
<form action="register_edit.php" method="post">
<input type="hidden" name="edit_id" value="<?php echo $rows['id'];?>">
<button type="submit" name="edit_btn" class="btn btn-success"> EDIT</button>
</form>
</td>
<td>
<form action="process.php" method="POST">
<input type="text" id="id" readonly value="<?php echo $rows['id'];?> ">
<button type="button" name="delete" onclick="myFunction();"id="delete" class="btn btn-danger" data-toggle="modal" data-target="#deleteModal">Delete</button>
</td>
</tr>
<?php
}
}
else{
echo "no record found!";
}
?>
<div id="deleteModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<br>
<h5 class="modal-title">PIN Required</h5>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<div class="modal-body">
<label>PIN</label>
<input type="text" id="print" readonly >
<input type="password" name="pin" id="password" class="form-control" />
<br />
<button type="submit" name="delete_btn" id="pin_button" class="btn btn-warning">Confirm</button>
<div>
</div>
</div>
</div>
</form>
</tbody>
</table>
</div>
</div>
</div>
<!-- /.container-fluid -->
<script>
function myFunction() {
document.getElementById("print").value = document.getElementById("id").value;
}
</script>
</body>
</html>
<?php
ob_end_flush();
include('includes/script.php');
include('includes/footer.php');
?>
You are creating buttons with id="delete" inside your while loop. So you end up with a lot of buttons with the same id which is both invalid html and creates your problem here.
You also open your tag inside your while loop and you close it in your modal. That will also create invalid html since you'll be opening a lot of form tags and only closing one of them.
Without having seen the code on your process.php file it is not 100% that the fixes here will solve your issue so bear that in mind.
First of all change
<td>
<form action="process.php" method="POST">
<input type="text" id="id" readonly value="<?php echo $rows['id'];?> ">
<button type="button" name="delete" onclick="myFunction();" id="delete" class="btn btn-danger" data-toggle="modal" data-target="#deleteModal">Delete</button>
</td>
to
<td>
<button type="button" name="delete" onclick="myFunction('<?php echo $rows['id'];?>');" class="btn btn-danger" data-toggle="modal" data-target="#deleteModal">Delete</button>
</td>
Then change your function
function myFunction(print_value) {
document.getElementById("print").value = print_value;
}
Lastly change your modal body
<form action="process.php" method="POST">
<label>PIN</label>
<input type="text" id="print" readonly >
<input type="password" name="pin" id="password" class="form-control" />
<br />
<button type="submit" name="delete_btn" id="pin_button" class="btn btn-warning">Confirm</button>
</form>
and remove the other closing form tag </form> you have before </tbody>

Get value from hidden field by clicking different button

I have different products and it should have a view description that when clicked, a modal will popup and will show descriptions designated to it. But, I tried first getting the value of the hidden field to test it out. It shows the value of the hidden field when I clicked the first button but when I clicked the others, it didn't change. For example:
Button 1 (View Description):
Button 2 (View Description):
When I clicked button 1 it will show in the modal the value of the hidden field which is 1. But when I click the button 2 it will show in the modal 1 again. What should I do to get the value of the hidden field by clicking the button? Here is my code:
<div id="display">
<?php
include('database_connection.php');
$query = "SELECT * FROM tbl_product ORDER BY id DESC";
$statement = $connect->prepare($query);
if($statement->execute())
{
$result = $statement->fetchAll();
$output = '';
foreach($result as $row)
{
$output .= '
<form method="post">
<div class="col-md-3" style="margin-top:12px;">
<div style="border:1px solid #333; background-color:#f1f1f1; border-radius:5px; padding:16px; height: 383px;" align="center">
<img src="images/'.$row["image"].'" class="img-responsive" /><br />
<h4 class="text-info">'.$row["name"].'</h4>
<h4 class="text-danger">$ '.$row["price"] .'</h4>
<input type="text" name="quantity" id="quantity' . $row["id"] .'" class="form-control" value="1" />
<input type="hidden" name="hidden_name" id="name'.$row["id"].'" value="'.$row["name"].'" />
<input type="hidden" name="hidden_price" id="price'.$row["id"].'" value="'.$row["price"].'" />
<input type="button" name="add_to_cart" id="'.$row["id"].'" style="margin-top:5px;" class="btn btn-success form-control add_to_cart" value="Add to Cart" />
<input type="hidden" name="getprod" id="getprod" value="'.$row["id"].'"/>
<input type="button" name="description" data-toggle="modal" data-target="#myModal" style="margin-top:5px;" class="btn btn-success form-control description" value="View Description" />
</div>
</div>
</form>
';
}
echo $output;
}
?>
</div>
<!-- Modal -->
<div class="modal fade" id="myModal" 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">Modal title</h4>
</div>
<div class="modal-body">
<?php $prod = $_POST["getprod"]; ?>
<h1><?php echo $prod; ?></h1>
</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>
</div>
</body>

In my modal not update value...bt query is run and disply garbage value

this is my php code:
<?php
if(isset($_POST['update'])){
$idid=$_POST['i_d'];
$v=$_POST['vvv'];
$course=$_POST['courseu'];
$sts=$_POST['statuesupdate'];
$query ="UPDATE `application_process` a left join `oferte` o on(a.`oferteid`= o.`idoferta`) SET a.`app_status`='$sts' where
a.`app_course`='$course' AND a.`oferteid`='$idid'" ;
$Recordset55 = mysql_query($query);
if($Recordset55){
echo $v;
}
}
?>
this is my modal htmlcode:
<div class="modal fade bs-example-modal-sm" tabindex="-1" id="myModal<?php echo $row_Recordset550['idoferta']; ?>">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">x</span>
</button>
<h4 class="modal-title" id="mySmallModalLabel">Update statues</h4>
</div>
<div class="modal-body">
<input type="text" name="i_d" value="<?php echo $id; ?>" />
<input type="text" name="vvv" value="<?php echo $row_Recordset550['id']; ?>" />
<input type="text" name="courseu" value="<?php echo $row_Recordset550['app_course']; ?>" />
<select name="statuesupdate">
<option value=""> -- Select Status --</option>
<option value="Under Processed">Active(UnderProcessed)</option>
<option value="processed">Closed(Processed)</option>
<option value="pending">Pending</option>
</select>
<input type="submit" name="update" value="update" class="btn btn-sm btn-warning">
</div>
</div>
</div>
</div>
this is my php and html code plese help me query is run successful bt data is not update and when print the id that time garbage value is print so how can my code update
this is my code open a tag
<td><a href="#myModal<?php echo $row_Recordset550['idoferta'];?>" class="btn btn-sm btn-danger" data-toggle="modal" >Edit</a></td>

php only updating first database entry regardless of what entry i try to update

So I'm doing a project for school, and I am trying to update certain items in column for certain rows in my database, however, regardless of what entry I try to update, only the first entry takes on the intended change.
Here is my update function:
if (isset($_POST['update']) && isset($_POST['serviceNum']))
{
$progress = get_post($conn, 'progress');
$serviceNum = get_post($conn, 'serviceNum');
$query = "UPDATE classics SET progress='$progress' WHERE serviceNum='$serviceNum'";
$result = $conn->query($query);
if (!$result) echo "UPDATE failed: $query<br>" .
$conn->error . "<br><br>";
}
And here is my update form:
<form action="admin.php" method="post">
<br>
<select name="progress">
<option value=" "> </option>
<option value="In Progress">In Progress</option>
<option value="Completed">Completed</option>
</select>
<input type="hidden" name="update" value="progress">
<input type="hidden" name="serviceNum" value="$row[0]">
<br><br>
<input type="submit" value="UPDATE">
<br><br>
</form>
$row[0] is the row that holds the primary key which is serviceNum. I have the form nested in a table, and when I run it on my localhost, and view source, it shows that each entry has the proper serviceNum for that row of data. However, regardless of which entry I use the update button on, it only updates the first entry of the database table.
EDIT:
for ($j = 0; $j < $rows; ++$j)
{
$result->data_seek($j);
$row = $result->fetch_array(MYSQLI_NUM);
echo "<tr>";
echo <<<_END
<td>$row[0]</td>
<td>$row[1]</td>
<td>$row[2]</td>
<td>$row[3]</td>
<td>$row[4]</td>
<td>
<!-- Trigger Update Modal -->
<center>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#UpdateModal"> <span class="glyphicon glyphicon-folder-open" aria-hidden="true"></span> </button>
</center>
<!-- Update Modal -->
<div class="modal fade" id="UpdateModal" 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">Update Record</h4>
</div>
<div class="modal-boday">
<center>
<form action="admin.php" method="post">
<br>
<select name="progress">
<option value=" "> </option>
<option value="In Progress">In Progress</option>
<option value="Completed">Completed</option>
</select>
<input type="hidden" name="update" value="yes">
<input type="hidden" name="serviceNum" value="$row[0]">
<br><br>
<input type="submit" value="UPDATE">
EDIT 2: MODAL
<center>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#UpdateModal">
<span class="glyphicon glyphicon-folder-open" aria-hidden="true"></span>
</button>
</center>
<!-- Update Modal -->
<div class="modal fade" id="UpdateModal" 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">Update Record</h4>
</div>
<div class="modal-boday">
<center>
<form action="admin.php" method="post">
<br>
<select name="progress">
<option value=" "> </option>
<option value="In Progress">In Progress</option>
<option value="Completed">Completed</option>
</select>
<input type="hidden" name="update" value="status">
<input type="hidden" name="ticketnum" value="$row[0]">
<br><br>
<input type="submit" value="UPDATE">
<br><br>
</form>
</center>
</div>
</div>
</div>
<!-- End Update Modal -->
The problem is that you're using the same id="update-modal" in every row of the table. IDs are supposed to be unique, and looking up an ID always returns the first one. So data-target="#update-modal" always targets the modal on the first row, regardless of which one you click.
Add the item ID into the update-modal ID.
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#UpdateModal{$row[0]}">
and
<div class="modal fade" id="UpdateModal{$row[0]}" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">

Why is save button in my modal does not work?

osHome.php
this are parts of the code and the "Save Changes" button does not do anything. I want to save the data to the database and my code seem to do nothing. please help me anyone :(
//php code clip
<?php
session_start();
//Check whether the session variable User_name is present or not
if(!isset($_SESSION['User_name']) || (trim($_SESSION['User_name']) == '')) {
header("location: login.php");
exit();
}
include_once('../config.php');
if(isset($_POST['addNewOs'])){
$new_name = $_POST['newOs_name'];
$new_edition = $_POST['newOs_edition'];
$new_version = $_POST['newOs_version'];
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}else {
$sql = "INSERT INTO oslist (oslist_name, oslist_edition, oslist_version)
VALUES ('$new_name', '$new_edition','$new_version')";
mysqli_query($conn, $sql);
echo "<script type='text/javascript'>alert('New record created successfully');</script>";
}
}
?>
//modal
<div class="modal fade" id="basicModal" tabindex="-1" role="dialog" aria-labelledby="basicModal" 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">x</button>
<h4 class="modal-title" id="myModalLabel">Add New OS</h4>
</div>
<div class="modal-body">
<form method="post" action="osHome.php">
<table style="width: 100%" class="table table-bordered" >
<tr><th><label>OS Name</label></th>
<td colspan="3" ><input type="text" name="newOs_name" class="form-control"></td>
</tr>
<tr><th><label>Edition</label></th>
<td colspan="3" ><input type="text" name="newOs_edition" class="form-control"></td>
</tr>
<tr><th><label>Version</label></th>
<td colspan="3" ><input type="text" name="newOs_version" class="form-control" ></td>
</tr>
</table>
</form>
</div>
<div class="modal-footer">
<button class="btn btn-danger" data-dismiss="modal">Cancel</button>
<input type="submit" name="addNewOs" value="Save Changes" class="btn btn-primary">
</div>
</div>
</div>
</div>
<div id="selectOS"><b>Operating System info will be listed here.</b></div>
the modal and the button are included, maybe there might be some errors that I haven't noticed.. -_-
//button
Add New OS
Your button is outside the </form> tag and is not associated with the form. Either expand what is in the form or use the <button form="formid" > attribute after giving your form an id.
Buttons
Option 1
<div class="modal fade" id="basicModal" tabindex="-1" role="dialog" aria-labelledby="basicModal" 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">x</button>
<h4 class="modal-title" id="myModalLabel">Add New OS</h4>
</div>
<form method="post" action="osHome.php"><!-- start form here-->
<div class="modal-body">
<table style="width: 100%" class="table table-bordered" >
<tr><th><label>OS Name</label></th>
<td colspan="3" ><input type="text" name="newOs_name" class="form-control"></td>
</tr>
<tr><th><label>Edition</label></th>
<td colspan="3" ><input type="text" name="newOs_edition" class="form-control"></td>
</tr>
<tr><th><label>Version</label></th>
<td colspan="3" ><input type="text" name="newOs_version" class="form-control" ></td>
</tr>
</table>
</div>
<div class="modal-footer">
<button class="btn btn-danger" data-dismiss="modal">Cancel</button>
<input type="submit" name="addNewOs" value="Save Changes" class="btn btn-primary">
</div>
</form><!-- end form here-->
</div>
</div>
</div>
Option 2
<div class="modal fade" id="basicModal" tabindex="-1" role="dialog" aria-labelledby="basicModal" 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">x</button>
<h4 class="modal-title" id="myModalLabel">Add New OS</h4>
</div>
<div class="modal-body">
<form method="post" action="osHome.php" id="myForm"><!-- id = myForm -->
<table style="width: 100%" class="table table-bordered" >
<tr><th><label>OS Name</label></th>
<td colspan="3" ><input type="text" name="newOs_name" class="form-control"></td>
</tr>
<tr><th><label>Edition</label></th>
<td colspan="3" ><input type="text" name="newOs_edition" class="form-control"></td>
</tr>
<tr><th><label>Version</label></th>
<td colspan="3" ><input type="text" name="newOs_version" class="form-control" ></td>
</tr>
</table>
</form>
</div>
<div class="modal-footer">
<button class="btn btn-danger" data-dismiss="modal">Cancel</button>
<input type="submit" name="addNewOs" value="Save Changes" class="btn btn-primary" form="myForm"> <!-- button associated with myform-->
</div>
</div>
</div>
</div>

Categories