Row with button submitting wrong data to server - php

Ive been working on this issue for some time now, basically, i have a table and at the end of row is a button that submits that rows batchID and Priority value to my .php that handles post requests. the problem comes when i hit submit it takes the batchID column from the last row regardless of which button is pressed. i have this exact same code running on another site which works as intended. when i inspect the network data being sent it shows that all the data in column batchID is being sent to upload.php, but i only need the corresponding batchID to be sent.
<?php
$con = mysqli_connect("localhost", "root", "", "production_management");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
require('databaseDetails.php');
session_start();
$userNameTmp = $_SESSION["usrName"];
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Received Request Form</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<p><button class="button button2">Dashboard</button><p>
<form name="form" method="post" action="upload.php">
<div class="form">
<h1>Batch's</h1>
<p>Hello <?php echo $userNameTmp;?> </p>
<table class="responsive-table" width="100%" border="1" style="border-
collapse:collapse;">
<thead>
<tr>
<th><strong>Started By</strong></th>
<th><strong>Batch Type</strong></th>
<th><strong>Date</strong></th>
<th><strong>Batch ID</strong></th>
<th><strong>Quantity</strong></th>
<th><strong>Progress</strong></th>
<th><strong>Priority</strong></th>
</tr>
</thead>
<tbody>
<?php
$query = "SELECT * FROM batch";
$result = mysqli_query($con,$query);
while($row = mysqli_fetch_assoc($result)) { ?>
<td align="center"><?php echo $row["startedBy"]; ?></td>
<td align="center"><?php echo $row["batchType"]; ?></td>
<td align="center"><?php echo $row["date"]; ?></td>
<td align="center"><?php echo $row["batchID"]; ?></td>
<td align="center"><?php echo $row["quantity"]; ?></td>
<td align="center"><?php echo $row["progress"]; ?></td>
<td align="center"><?php echo $row["priority"]; ?></td>
<td align="center"><input type="hidden" name="postMethod" value="3"></td>
<td align="center"><input type="hidden" name="batchID" value="<?php echo
$row["batchID"]; ?>"></td>
<td align="center"><input type="submit" value="test"></td></tr>
<?php
}
?>
</tbody>
</table>
</div>
</form>
</body>
</html>

Related

Row from database not showing correctly except first row

The first row of this table is shown correctly and centered, while the other rows look like they are just being echo'd out instead of looking like they are part of the table.
This is the part of my code:
<table style="width:20%">
<tr>
<th>ID</th>
<th>Nume</th>
<th>Prenume</th>
</tr>
//fetching data from database
<tr>
<td><?php echo $data['id'];?></td>
<td><?php echo $data['nume'];?></td>
<td><?php echo $data['prenume'];?></td>
</tr>
</table>
The code is supposed to add the written user to a database and display all of the users in a table next to it.
Any help is appreciated! Thanks!
(whole code listed below)
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<form action="index.php" method="post">
<input type="text" name="nume" placeholder="nume" required>
<input type="text" name="prenume" placeholder="prenume" required>
<input type="submit" name="sub" value="Adauga in baza de date">
<br>
</form>
<table style="width:20%">
<tr>
<th>ID</th>
<th>Nume</th>
<th>Prenume</th>
</tr>
<?php
if(isset($_POST['sub']))
{
$pdo = new PDO("mysql:host=localhost;dbname=liamed", "root", "");
$nume = $_POST['nume'];
$prenume = $_POST['prenume'];
$q = "insert into users(nume,prenume)values(:nume,:prenume)";
$r = $pdo->prepare($q);
$r->execute(array(":nume"=>$nume, ":prenume"=>$prenume));
}
$pdo = new PDO("mysql:host=localhost;dbname=liamed", "root", "");
$query = "select * from liamed.users";
$d = $pdo->query($query);
foreach ($d as $data){
?>
<tr>
<td><?php echo $data['id'];?></td>
<td><?php echo $data['nume'];?></td>
<td><?php echo $data['prenume'];?></td>
</tr>
</table>
</body>
<?php }
?>
</html>
You have the end of the table HTML included inside your loop hense the mess. Move that outside the loop and you should be sorted :)
foreach ($d as $data) :
?>
<tr>
<td><?php echo $data['id'];?></td>
<td><?php echo $data['nume'];?></td>
<td><?php echo $data['prenume'];?></td>
</tr>
<?php
endforeach;
?>
</table>
</body>

How do I make a button within an HTML table, to update info in my database

I am totally new to coding. That being said I hope you can help me with this issue I am having.
I use PHP and Mysqli to populate information within a html table. I have added a button in one of the columns. I want to click the button and update the information on that row of the table, in the database.
The users earn a commission, so by clicking the button, I want to move the amount in the "pay" column to the "paid" column and update the database.
When I click the button, I get the "success" message that says "Payment Processed Successfully", but nothing changes in the database!
Thanks ahead of time for any help you can provide.
Here's my code:
<html>
<body>
<div class="form">
<h2>View Records</h2>
<table width='100%'>
<thead>
<tr>
<th><strong>user ID</strong></th>
<th><strong>Fist Name</strong></th>
<th><strong>Last Name</strong></th>
<th><strong>pay commission</strong></th>
<th><strong>Paid</strong></th>
<th><strong>Pay Commission</strong></th>
<th><strong>Delete</strong></th>
</tr>
</thead>
<tbody>
<?php
$count=1;
include "db.php";
$sql = "SELECT * FROM users ";
$result = $conn-> query($sql);
if($result -> num_rows > 0){
while ($row = $result-> fetch_assoc()) { ?>
<td align="center"><?php echo $row["id"]; ?></td>
<td align="center"><?php echo $row["first_name"]; ?></td>
<td align="center"><?php echo $row["last_name"]; ?></td>
<td align="center"><?php echo $row["pay"]; ?></td>
<td align="center"><?php echo $row["paid"]; ?></td>
<td align="center">
<div class="input-group">
<form class="reset-form" action="members.php" method="post" >
<div class="input-group">
<button type="submit" class="button_1" name="pay-commission">Pay Commission</button></div></form>
</td>
<td align="center">
Delete
</td>
</tr>
<?php
}
// process commission*******
if(isset($_POST["pay-commission"])){
$id=$row['id'];
$pay=$row['pay'];
$paid=$row['last_paid'];
//date and time of transaction
$trn_date = date("Y-m-d H:i:s");
require('db.php');
$ins_query="update users last_paid='$pay' trm_date='$trn_date' where id= '$id'";
mysqli_query($conn,$ins_query);
if($ins_query){
echo "<p class= 'success'> Payment Processed Successfully <p>";
}else{
echo"<p class= 'error'>something went wrong!!</p>";
}
}
}
?>
</tbody>
</table>
</div>
</body>
</html>
got it working except it updates all users with one click on any button in the "pay commission" column. may not be a bad thing to process all commissions with one click.
Maybe I move the button below the table, to update all users with one click. all of this code is on a page called members.php
<html>
<head></head> <header></header>
<body>
//table structure
<div class="form">
<h2>View Records</h2>
<table width='100%'>
<thead>
<tr>
<th><strong>user ID</strong></th>
<th><strong>Fist Name</strong></th>
<th><strong>Last Name</strong></th>
<th><strong>pay commission</strong></th>
<th><strong>Paid</strong></th>
<th><strong>Pay Commission</strong></th>
<th><strong>Delete</strong></th>
</tr>
</thead>
<tbody>
//populate table with user data
<?php
$count=1;
include "db.php";
$sql = "SELECT * FROM users ";
$result = $conn-> query($sql);
if($result -> num_rows > 0){
while ($row = $result-> fetch_assoc()) { ?>
<td align="center"><?php echo $row["id"]; ?></td>
<td align="center"><?php echo $row["first_name"]; ?></td>
<td align="center"><?php echo $row["last_name"]; ?></td>
<td align="center"><?php echo $row["pay"]; ?></td>
<td align="center"><?php echo $row["last_paid"]; ?></td>
<td align="center">
<div class="input-group">
<form action="members.php" method="post" >
<div class="input-group">
<button type="submit" class="button_1" name="pay-commission">Pay commission</button></div></form>
</td>
<td align="center">
Delete
</td>
</tr>
<?php
// process commission*******
if(isset($_POST["pay-commission"])){
$id=$row['id'];
$pay=$row['pay'];
$paid=$row['last_paid'];
//set the date and time of transaction
$trn_date = date('Y-m-d');
$conn->query("UPDATE users SET last_paid='$pay' WHERE id= '$id'");
$conn->query("UPDATE users SET pay=0.00 WHERE id= '$id'");
$conn->query("UPDATE users SET trn_date='$trn_date' WHERE id= '$id'");
if($conn){
//refresh the page to see changes
header ('Location: members.php');
echo '<p class="success"> A Payment of $$pay Processed Successfully for member ID $id</p>';
}else{
echo '<p class="error">Something went wrong! </p>';
}
}
}
}
$conn->close();
?>
</tbody>
</table>
</div>
</body>
</html>

does not identify the id in the table

I am making the modification for the query and already add and everything but in modifying it does not work with 'id' that I put after putting the address of the modification page
this is the table
<!DOCTYPE html>
<html>
<head>
<title> TABLA </title>
</head>
<body>
<center>
<table bgcolor="#85C1E9" border="3">
<thead>
<tr>
<th colspan="1">Nuevo</th>
<th colspan="5">Lista de usuarios</th>
</tr>
</thead>
<tbody>
<tr>
<td>Id</td>
<td>Nombre</td>
<td>Apellido</td>
<td>Correo</td>
<td colspan="2"><center>Operaciones</center></td>
</tr>
<?php
include("conexiones.php");
$query="SELECT * FROM usuarios";
$resultado=$conexion->query($query);
while ($row=$resultado->fetch_assoc()) {
?>
<tr>
<td><?php echo $row['id'];?></td>
<td><?php echo $row['nombre'];?></td>
<td><?php echo $row['apellido'];?></td>
<td><?php echo $row['correo'];?></td>
<td><a href="modificar.php?id=<?php echo $row['id'];
?>">Modificar</a></td>
<td><a href="eliminar.php?id=<?php echo $row['id']; ?
>">Eliminar</a></td>
</tr>
<?php
}
?>
</tbody>
</table>
</center>
</body>
</html>
here the modify page
<!DOCTYPE html>
<html>
<head>
<title>Guardar</title>
</head>
<body>
<center>
<form action="guardar.php" method="POST" name="guardar"><br><br><br>
<?php
$id=$_REQUEST['id'];
include("conexiones.php");
$query="SELECT * FROM usuarios";
$resultado=$conexion->query($query);
$row=$resultado->fetch_assoc();
?>
<input type="text" required="" name="nombre" placeholder="Nombre..."
value="<?php echo $row['nombre'];?>"><br><br>
<input type="text" required="" name="apellido"
placeholder="Apellido..." value="<?php echo $row['apellido'];?>">
<br><br>
<input type="text" required="" name="correo"
placeholder="Correo..." value="<?php echo $row['correo'];?>"><br>
<br>
<input type="submit" value="Aceptar">
</form>
</center>
</body>
</html>
the error is that when I press modify in the query I get like this:
http://localhost/modificar.php?id=%3C?echo%20$row[%27id%27];?%3E
and the way it should go (for example) is like this:
http://localhost/modificar.php?id=1
Try this:
<?php
include("conexiones.php");
$query="SELECT * FROM usuarios";
$resultado=$conexion->query($query);
?>
<?php while ($row=$resultado->fetch_assoc()): ?>
<tr>
<td><?= $row['id'] ?></td>
<td><?= $row['nombre'] ?></td>
<td><?= $row['apellido'] ?></td>
<td><?= $row['correo'] ?></td>
<td><a href="modificar.php?id=<?= $row['id'] ?>">
Modificar</a></td>
<td><a href="eliminar.php?id=<?= $row['id'] ?>">
Eliminar</a></td>
</tr>
<?php endwhile; ?>
hope it helps.

Spanish Text in PHP WHILE loop from MySQL database

Hey: I'm trying to display data from a MySQL database in an HTML table. However, some of the text is in Spanish and the special characters are showing up as �. Does anyone know how to fix this?
Database import script db.php
<?php
header('Content-type: text/html; charset=UTF-8');
// Enter your Host, username, password, database below.
// I left password empty because i do not set password on localhost.
$con = mysqli_connect("localhost","root","","yamanagolddb");
// Check connection
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
Table display script table.php:
<?php
header('Content-type: text/html; charset=UTF-8');
require('db.php');
include("auth.php");
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content Type" content="text/html; charset=utf-8"/>
<title>View Records</title>
<link rel="stylesheet" href="css/style.css" />
</head>
<body>
<div accept-charset="UTF-8" class="form">
<p>Home
| Insert New Record
| Logout</p>
<h2>View Records</h2>
<table width="100%" border="1" style="border-collapse:collapse;">
<thead>
<tr>
<th><strong>S.No</strong></th>
<th><strong>Hecho Por</strong></th>
<th><strong>Nombre</strong></th>
<th><strong>CÓDIGO</strong></th>
<th><strong>INFLUENCIA</strong>
(1) Sin influencia a (5) Influencia maxima</th>
<th><strong>POSTURA</strong>
(1) Apoyo activo a (5) Oposicion activa</th>
<th><strong>CREDIBILIDAD</strong>
(1) Sin credibilidad a (5) Credibilidad Maxima</th>
<th><strong>ACCESIBILIDAD</strong>
(1) Alta disposicon a dialogar a (5) Sin disposicion a dialogar</th>
<th><strong>Fecha</strong></th>
<th><strong>Edit</strong></th>
<th><strong>Delete</strong></th>
</tr>
</thead>
<tbody>
<?php
$count=1;
$sel_query="Select * from surveydata ORDER BY 'jectid' desc;";
$result = mysqli_query($con,$sel_query);
while($row = mysqli_fetch_assoc($result)) { ?>
<tr><td align="center"><?php echo $count; ?></td>
<td align="center"><?php echo html_entity_decode($row["hecho_por"]); ?></td>
<td align="center"><?php echo $row["pi_nbre"]; ?></td>
<td align="center"><?php echo $row["id_nombre"]; ?></td>
<td align="center"><?php echo $row["pi_influenc"]; ?></td>
<td align="center"><?php echo $row["pi_posicio"]; ?></td>
<td align="center"><?php echo $row["pi_credib"]; ?></td>
<td align="center"><?php echo $row["pi_acces"]; ?></td>
<td align="center"><?php echo $row["fecha_actu"]; ?></td>
<td align="center">
Edit
</td>
<td align="center">
Delete
</td>
</tr>
<?php $count++; } ?>
</tbody>
</table>
</div>
</body>
</html>
In the html parts, if I add a Spanish word with special characters they display correctly. For example, CÓDIGO displays correctly. However, the data pulled from the database shows special characters as �.

execution of isset before click the save button in php

I am having two php files, one contains a form and another to display the content entered and save button. After confirming the details are correct he has to save and the data should be inserted into the table. I have given the code below
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<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.1.1/jquery.min.js">
</script>
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.1.1.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"> </script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"> </script>
</head>
<body>
<?php
$roll=$_POST['roll'];
$sname=$_POST['sname'];
$fname=$_POST['fname'];
$gender=$_POST['gender'];
$aadhar=$_POST['aadhar'];
$caste=$_POST['caste'];
?>
<div class="container container-center">
<!-------------total panel------------------------------------>
<div class="panel-info">
<!------panel heading------>
<div class="panel-heading">
<h2><center><u>STUDENT DETAILS</u></center></h2>
</div> <!-panel heading->
<div class="panel-body">
<form method="post" action="">
<table align="center" border="0">
<tbody>
<tr>
<td rowspan="7"><?php echo"<img src=/photo/$roll.jpg width='200px' height='300px' id='photo'>"; ?></td>
<td>1. Roll Number & Photo:</td>
<td><?php echo"".strtoupper($roll).""; ?></td>
</tr>
<tr>
<td>2. Name of the Student:</td>
<td><?php echo"".strtoupper($sname).""; ?></td>
</tr>
<tr>
<td>3. Name of the Father:</td>
<td><?php echo"".strtoupper($fname).""; ?></td>
</tr>
<tr>
<td>4. Gender:</td>
<td><?php
if($gender=='M')
{ echo"Male";}
else
{echo"Female";}
?></td>
</tr>
<tr>
<td>5. Aadhar Number:</td>
<td><?php echo"$aadhar"; ?></td>
</tr>
<tr>
<td>6. Caste:</td>
<td><?php echo"$caste"; ?></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" id="submit" name="submit" class="btn btn-info" value="save"></td>
</tr>
</tbody>
</table>
</form>
</div>
</div>
</div>
<?php
if(isset($_POST['submit']))
{
echo"<script>alert('success')</script>";
}
?>
</body>
when i click the save button then only the data must be saved. but even if not click the save the php code in isset is executing.
the data obtained by $_POST is from the form.
Please help me.
NOTE:
Actually Problem is that, you don't get the data on this page.. in your POST request with given variable so that Please Just Check it . Other Code Is Fine. !! ( Here I am Just Pass Dummy Data ) Like Below.
$roll= '1234';
$sname='XYZ';
$fname= 'ABC';
$gender= 'M';
$aadhar= '123456789100';
$caste= 'A12345';
Solution: You Have To Get DATA Here in post request from your first_php_page form where user enter the details regarding given fields: roll,sname,fname,gender,aadhar,caste
After here in secound_php_page check that Submit button is set then store the values in varable Like below Code:
NOTE: At The End YoU Have To Write The Insert Query On Save Button Click. To Store The data in Database.
<?php
if(isset($_POST['Submit'])){
$roll=$_POST['roll'];
$sname=$_POST['sname'];
$fname=$_POST['fname'];
$gender=$_POST['gender'];
$aadhar=$_POST['aadhar'];
$caste=$_POST['caste'];
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<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.1.1/jquery.min.js">
</script>
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.1.1.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"> </script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"> </script>
</head>
<body>
<?php
/*
$roll=$_POST['roll'];
$sname=$_POST['sname'];
$fname=$_POST['fname'];
$gender=$_POST['gender'];
$aadhar=$_POST['aadhar'];
$caste=$_POST['caste'];
*/
// SET THE DUMMY DATA FOR Result check
$roll= '1234';
$sname='XYZ';
$fname= 'ABC';
$gender= 'M';
$aadhar= '123456789100';
$caste= 'A12345';
?>
<div class="container container-center">
<!-- total panel -->
<div class="panel-info">
<!-- panel heading -->
<div class="panel-heading">
<h2><center><u>STUDENT DETAILS</u></center></h2>
</div>
<!--panel heading -->
<div class="panel-body">
<form method="post" action="">
<table align="center" border="0">
<tbody>
<tr>
<td rowspan="7"><?php echo"<img src=/photo/$roll.jpg width='200px' height='300px' id='photo'>"; ?></td>
<td>1. Roll Number & Photo:</td>
<td><?php echo"".strtoupper($roll).""; ?></td>
</tr>
<tr>
<td>2. Name of the Student:</td>
<td><?php echo"".strtoupper($sname).""; ?></td>
</tr>
<tr>
<td>3. Name of the Father:</td>
<td><?php echo"".strtoupper($fname).""; ?></td>
</tr>
<tr>
<td>4. Gender:</td>
<td><?php
if($gender=='M')
{ echo"Male";}
else
{echo"Female";}
?></td>
</tr>
<tr>
<td>5. Aadhar Number:</td>
<td><?php echo"$aadhar"; ?></td>
</tr>
<tr>
<td>6. Caste:</td>
<td><?php echo"$caste"; ?></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" id="submit" name="submit" class="btn btn-info" value="save"></td>
</tr>
</tbody>
</table>
</form>
</div>
</div>
</div>
<?php
if(isset($_POST['submit']))
{
echo"<script>alert('success')</script>";
}
?>
</body>
</HTML>
OUTPUT: DATA Display
AND ONCLICK SAVE
There are few issues with your code, such as:
There's no point having a form and save button(in another page) if you don't have input elements and don't send your data using $_POST superglobal. Create input elements for each of your fields with type="hidden", for example like this:
<td><?php echo strtoupper($roll); ?><input type="hidden" name="roll" value="<?php echo strtoupper($roll); ?>"></td>
Here's the full form code,
// your code
<form method="post" action="">
<table align="center" border="0">
<tbody>
<tr>
<td rowspan="7"><?php echo"<img src=/photo/$roll.jpg width='200px' height='300px' id='photo'>"; ?></td>
<td>1. Roll Number & Photo:</td>
<td><?php echo strtoupper($roll); ?><input type="hidden" name="roll" value="<?php echo strtoupper($roll); ?>"></td>
</tr>
<tr>
<td>2. Name of the Student:</td>
<td><?php echo strtoupper($sname); ?><input type="hidden" name="sname" value="<?php echo strtoupper($sname); ?>"></td>
</tr>
<tr>
<td>3. Name of the Father:</td>
<td><?php echo strtoupper($fname); ?><input type="hidden" name="fname" value="<?php echo strtoupper($fname); ?>"></td>
</tr>
<tr>
<td>4. Gender:</td>
<td>
<?php
if($gender=='M'){
echo"Male";
}else{
echo"Female";
}
?>
<input type="hidden" name="gender" value="<?php echo $gender; ?>">
</td>
</tr>
<tr>
<td>5. Aadhar Number:</td>
<td><?php echo $aadhar; ?><input type="hidden" name="aadhar" value="<?php echo $aadhar; ?>"></td>
</tr>
<tr>
<td>6. Caste:</td>
<td><?php echo $caste; ?><input type="hidden" name="caste" value="<?php echo $caste; ?>"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" id="submit" name="submit" class="btn btn-info" value="save"></td>
</tr>
</tbody>
</table>
</form>
// your code
I don't see any code where you're inserting the form data into the table, you should have a code block like this in another page(where you have displayed the form data and has the save button):
if(isset($_POST['submit'])){
$roll = $_POST['roll'];
$sname = $_POST['sname'];
$fname = $_POST['fname'];
$gender = $_POST['gender'];
$aadhar = $_POST['aadhar'];
$caste = $_POST['caste'];
// your INSERT operation
}

Categories