Retrieve row id from table displayed in webpage - php

I have a MySQL table that is displayed in the webpage and every row has a submit button. When the submit button for a particular row is clicked, the first element i.e. row id should be retrieved. Should I use arrays to store the data or can I obtain the row id from the table I've echoed on screen. Here's the code:
echo "<table>";
while($row = mysqli_fetch_row($result))
{
echo "<tr>";
echo "<td align='center' name='bus_id'>" . $row[0] . "</td>";
echo "<td>" . $row[1] . "</td>";
echo "<td>" . $row[2] . "</td>";
echo "<td>" . $row[3] . "</td>";
echo "<td>" . $row[4] . "</td>";
echo "<td>" . $row[5] . "</td>";
echo "<td>" . $row[6] . "</td>";
echo "<td>" . $row[7] . "</td>";
echo "<td>" . $row[7] . "</td>";
echo "<td><form name='f1' method='post' action='schedule_list.php'>
<input type='submit' name='book' value='Book'></form></td>";
echo "</tr>";
}
echo "</table";

Use a a button with type submit to submit the ID as follows (and still display a meaningful button title
echo "<td><form name='f1' method='post' action='schedule_list.php'>
<button type='submit' name='book' value='".$row[0]."'>Book</button></form></td>";

Related

From php table retrieve data from selected checkbox

I have below php table, it has row and column. Each row has checkbox, I am trying to retrieve email data from row that checkbox is selected. I can do this with Jquery, but since I can't save in text file. I am trying to do same thing with PHP. I try couple things, but none worked.
<?php
echo "<table border='1' bordercolor='red' id='event_table'>
<tr>
<th> <input type='checkbox' id='chk_all' /> </th>
<th>Id</td>
<th>Email</th>
<th>Name</th>
<th>Car</th>
<th>Phone</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td> <input type='checkbox' id='chk[]'/> </td>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['car'] . "</td>";
echo "<td>" . $row['phone'] , "</td>";
echo "</tr>";
}
echo "</table>";
?>
You can do this using Hidden fields and some indentity implementation on name= (and id= as well). Here are some inputs from my end but you can modify the code as per your requirements.
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td> <input type='checkbox' name='chk_".$row['id']."' id='SHOULD_BE_UNIQUE_ON_THIS_PAGE'/> </td>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['email'] . "<input type='hidden' name='chk_".$id."_email' value='".$row['email']."' id='SHOULD_BE_UNIQUE_ON_THIS_PAGE' /></td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['car'] . "</td>";
echo "<td>" . $row['phone'] . "</td>";
echo "</tr>";
}
echo "</table>";
Here chk_{ID} is your main identifier while filtering data. When you submit the form, you should get to know whether checkbox is checked or not and if it is checked then you will come to know which hidden input field has your data. Hidden fields will not appear in HTML page.
As PHP is a Hypertext Preprocessor language, you can't detect any client action on your page. So here is the best solution: you must detect changes you need with JavaScript/JQuery, and then send the result to a php file via Ajax and handle the result there (In your case save it in a file);
I hope it would be helpful for you. Let me know if you have any questions

How do update a specific row using checkboxes

I'm trying to update specific rows in a table that is built from a form. I am using checkboxes but the whole table is being updated instead of the specific row that I am filling in. I tried ISSET to see if I could get it to only update when the boxes were ticked, but that didn't work. Here's what I have (obviously not the whole code, but I can provide more as needed):
<?php
include 'connect.php';
// All possible parameters
$params = array(
'Student',
'homeroom',
'teacher'
);
$wheres = array();
foreach ($params as $param) {
// Is the param set?
// If so, let's add it to our list of WHERE clauses
if (isset($_GET[$param]) && !empty($_GET[$param])) {
$wheres[] = sprintf("%s LIKE '%%%s%%'", $param, mysqli_real_escape_string($connect, $_GET[$param]));
}
}
if ($db_found) {
// Now let's make the SQL.
$SQL = 'SELECT * FROM studentlist';
// We only want to add the WHERE clause if we had some parameters passed
if (count($wheres) > 0) {
$SQL .= ' WHERE ' . implode(' OR ', $wheres);
}
$result = mysqli_query($connect, $SQL);
while ($row = mysqli_fetch_array($result, MYSQLI_BOTH)) {
echo "<tr>";
echo "<td>" . $row['Student'] . "</td>";
echo "<td>" . $row['homeroom'] . "</td>";
echo "<td>" . "<input type='checkbox' name='responsibility' value='E'>" . "</td>";
echo "<td>" . "<input type='checkbox' name='responsibility' value='G'>" . "</td>";
echo "<td>" . "<input type='checkbox' name='responsibility' value='S'>" . "</td>";
echo "<td>" . "<input type='checkbox' name='responsibility' value='N'>" . "</td>";
echo "<td>" . "" . "</td>";
echo "<td>" . "<input type='checkbox' name='organization' value='E'>" . "</td>";
echo "<td>" . "<input type='checkbox' name='organization' value='G'>" . "</td>";
echo "<td>" . "<input type='checkbox' name='organization' value='S'>" . "</td>";
echo "<td>" . "<input type='checkbox' name='organization' value='N'>" . "</td>";
echo "<td>" . "" . "</td>";
echo "<td>" . "<input type='checkbox' name='independentwork' value='E'>" . "</td>";
echo "<td>" . "<input type='checkbox' name='independentwork' value='G'>" . "</td>";
echo "<td>" . "<input type='checkbox' name='independentwork' value='S'>" . "</td>";
echo "<td>" . "<input type='checkbox' name='independentwork' value='N'>" . "</td>";
echo "<td>" . "" . "</td>";
echo "<td>" . "<input type='checkbox' name='collaboration' value='E'>" . "</td>";
echo "<td>" . "<input type='checkbox' name='collaboration' value='G'>" . "</td>";
echo "<td>" . "<input type='checkbox' name='collaboration' value='S'>" . "</td>";
echo "<td>" . "<input type='checkbox' name='collaboration' value='N'>" . "</td>";
echo "<td>" . "" . "</td>";
echo "<td>" . "<input type='checkbox' name='initiative' value='E'>" . "</td>";
echo "<td>" . "<input type='checkbox' name='initiative' value='G'>" . "</td>";
echo "<td>" . "<input type='checkbox' name='initiative' value='S'>" . "</td>";
echo "<td>" . "<input type='checkbox' name='initiative' value='N'>" . "</td>";
echo "<td>" . "" . "</td>";
echo "<td>" . "<input type='checkbox' name='selfregulation' value='E'>" . "</td>";
echo "<td>" . "<input type='checkbox' name='selfregulation' value='G'>" . "</td>";
echo "<td>" . "<input type='checkbox' name='selfregulation' value='S'>" . "</td>";
echo "<td>" . "<input type='checkbox' name='selfregulation' value='N'>" . "</td>";
echo "</tr>";
And then I have this as my update:
<?php
include 'connect.php';
// Get values from form
$resp=$_POST['responsibility'];
$org=$_POST['organization'];
$ind=$_POST['independentwork'];
$coll=$_POST['collaboration'];
$init=$_POST['initiative'];
$self=$_POST['selfregulation'];
// Insert data into mysqli
$sql = "UPDATE studentlist
SET responsibility='$resp', organization='$org', independentwork='$ind',
collaboration='$coll', initiative='$init', selfregulation='$self'";
$result=mysqli_query($connect, $sql);
// if successfully insert data into database, displays message "Successful".
if($result){
echo "Successful";
}
else {
echo "ERROR";
}
?>
<?php
// close connection
mysqli_close($connect);
?>
I understand that I have all the checkboxes named the same thing, but I'm not sure how to move away from that. And yes, I am new to this!!!!!
You have to add to the end of your querya WHERE condition to upsate just the row you want, if you use for example StudentId as the identifier of your Student then you write :
$sql = "UPDATE studentlistSET responsibility='$resp', organization='$org', independentwork='$ind',collaboration='$coll', initiative='$init', selfregulation='$self' WHERE StudentId='$studentId' ;

Insert row into DB using radio button

i need some help here.
I need to create this grid that allows the user to select a winner, and that winner will be stored into a table in my database.
Im geeting all done, until the point were i need to me this Radio Button working, it inserts an empty row.
This my form...
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" .$row['id']."</td>";
echo "<td>" . "<input type='radio' name='premio' value='".$row['id']."'>" ."</td>";
echo "<td >" .$row['nombre']. "</td>";
echo "<td name='correo'>" . $row['correo'] . "</td>";
echo "<td name='telefono'>" . $row['telefono'] . "</td>";
echo "<td>" . $row['ar1'] . "</td>";
echo "<td>" . $row['ar2'] . "</td>";
echo "<td>" . $row['br1'] . "</td>";
echo "<td>" . $row['br2'] . "</td>";
echo "<td>" . $row['cr1'] . "</td>";
echo "<td>" . $row['dr1'] . "</td>";
echo "<td>" . $row['dr2'] . "</td>";
echo "<td>" . $row['dr3'] . "</td>";
echo "<td>" . $row['er1'] . "</td>";
echo "<td>" . $row['er2'] . "</td>";
echo "</tr>";
}
echo "</table>";
echo "<input type='submit' value='enviar'/>";
mysqli_close($con);
?>
</form>
</div>
And the link if you want to check it out.
http://www.gifted.cl/clientes/servier/registro_evento_1.php
php
<?php
$con=mysqli_connect("localhost","user","pass","db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="INSERT INTO ganadoresevento (nombre,correo,telefono)
VALUES
('$_POST[nombre]','$_POST[correo]','$_POST[telefono]')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "Respuestas registradas";
mysqli_close($con);
?>
Thanks in advance
echo "<td>" . "<input type='radio' name='premio' value='".$row['id']."'>" ."</td>";
echo "<td>" . "<input type='radio' name='nombre' value='".$row['nombre']."'>" ."</td>";
echo "<td>" . "<input type='radio' name='correo' value='".$row['correo']."'>" ."</td>";
echo "<td>" . "<input type='radio' name='telefono' value='".$row['telefono']."'>" ."</td>";
Don't really know what inputs you want to use but this uses radio button. You could use <input type="text"> or <input type="hidden">
Have a look at prepared statement also http://www.php.net/manual/en/mysqli.prepare.php

How to calculate multiplication value inside the while loop in PHP?

I have one while loop which displays data from the database. Now I want multiply two values in one row and display the result in the same row, the same way multiply the values and display the result in every row. I am not getting the result. Can anyone help me? I am new to PHP.
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td class='alt'>" . $row['id'] . "</td>";
echo "<td>" . $row['item'] . "</td>";
echo "<td>" . $row['amount'] . "</td>";
$ss=$row['amount'];
echo '<td >'.'<input type="checkbox" name="status" value="" >'.'</td>';
echo '<td >'.'<input type="text" name="qty">'.'</td>';
echo "<td>" . $rr1 . "</td>";
echo "</tr>";
}
From Where you are getting $rr1??
You can have
echo "<td>" . $row['item'] * $row['amount'] . "</td>";
Hope this is what you want...
you can try
printf('amount is %d',$row['item'] * $row['amount']);
reference
Try below :
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td class='alt'>" . $row['id'] . "</td>";
echo "<td>" . $row['item'] . "</td>";
echo "<td>" . $row['amount'] . "</td>";
echo "<td>" .($row['item'] * $row['amount']). "</td>";
echo "</tr>";
}
Change the query as
$result= mysql_query("Select id, item, amount, (item * amount) total
from table_name");
then in while loop
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td >" . $row['id'] . "</td>";
echo "<td>" . $row['item'] . "</td>";
echo "<td>" . $row['amount'] . "</td>";
echo "<td>" .($row['total'] "</td>";
echo "</tr>";
}

mysql php connector not returning last cell value

$sql = ""
$result = mysql_query($sql);
while ($row = #mysql_fetch_row($result)) {
echo "<tr bgcolor='#dcdcdc'>" ;
echo "<td>$row[0] </td>";
echo "<td>" . $row[1] . "</td>";
echo "<td>" . $row[2] . "</td>";
echo "<td>" . $row[3] . "</td>";
echo "<td>" . $row[4] . "</td>";
echo "<td>" . $row[5] . "</td>";
echo "<td>" . $row[6] . "</td>";
}
when I run the above php code on my local mysql server , my last row value is correctly displayed.
But when the same is deployed on the server and access from client PC , last cell value is empty.
Please help me how to sort this issue out.
I think you're missing the trailing </tr> from your code, that could potentially do it:
while ($row = #mysql_fetch_row($result)) {
echo "<tr bgcolor='#dcdcdc'>" ;
echo "<td>$row[0] </td>";
echo "<td>" . $row[1] . "</td>";
echo "<td>" . $row[2] . "</td>";
echo "<td>" . $row[3] . "</td>";
echo "<td>" . $row[4] . "</td>";
echo "<td>" . $row[5] . "</td>";
echo "<td>" . $row[6] . "</td>";
echo "</tr>";
}
I'd check the database to make sure you have all of the rows in your table. That would pretty much have to be the problem.
Make sure you have at least 7 columns in there.

Categories