How do update a specific row using checkboxes - php

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' ;

Related

How should I change the cell color according to a specific price?

I want to change the color of the entire cell according to a specific value. But what should I do when the grammar is wrong and it doesn't work?
in this my code
echo "<tr if( $row['treatment_fees_check_division'] == 'error') : style='yellow' >";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['chart_num'] . "</td>";
echo "<td>" . $row['chart_name'] . "</td>";
echo "<td>" . substr($row['visit'], 0,10) . "</td>";
echo "<td >" . number_format($row['total_medical_bills']) . "</td>";
echo "<td >" . number_format($row['total_amount']) . "</td>";
echo "<td>" . number_format($row['amount_asked']) . "</td>";
echo "<td>" . number_format($row['medical_bills_payment']) . "</td>";
echo "<td>" . number_format($row['personal_liability_amount']) . "</td>";
echo "<td >" . number_format($row['non_payment']) . "</td>";
echo "<td >" . number_format($row['non_payment_sales']) . "</td>";
echo "<td>" . $row['insurance_division'] . "</td>";
echo "<td>" . $row['division'] . "</td>";
echo "<td>" . number_format($row['cash_amount_received']) . "</td>";
echo "<td>" . number_format($row['cash_receipt']) . "</td>";
echo "<td>" . number_format($row['cash_receipt_non_payment']) . "</td>";
echo "<td>" . number_format($row['cash_receipt_payment']) . "</td>";
echo "<td>" . number_format($row['card_amount_received']) . "</td>";
echo "<td>" . number_format($row['card_non_payment']) . "</td>";
echo "<td>" . number_format($row['card_payment']) . "</td>";
echo "<td>" . number_format($row['treatment_fees_difference']) . "</td>";
echo "<td>" . $row['treatment_fees_check_division'] . "</td>";
echo "</tr>";
Try replacing your first line with something like this.
if ($row['treatment_fees_check_division'] == 'error') {
echo "<tr style='yellow' >";
} else {
echo "<tr >";
}

Retrieve row id from table displayed in webpage

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>";

Pass multiple variables through link (reads only one)

I'm trying to pass some variables from a link but for some reason it only reads one of them. Below is the link and the code. roundNo shows up fine.
archery/viewround.php?gameNo=1&roundNo=7
echo $_GET['gameNo'];
echo $_GET['roundNo'];
echo "<table border><tr><th>Round ID</th><th>Match Name</th><th>Player Name</th><th>Shot 1</th><th>Shot 2</th><th>Shot 3</th><th>Shot 4</th><th>Shot 5</th><th>Shot 6</th><th>Shot 7</th><th>Shot 8</th><th>Shot 9</th><th>Shot 10</th></tr>";
while ($row = mysql_fetch_array($sql)) {
$tempPlayerName = getnamewithid($row["player_id"]);
$tempMatchName = getmatchnamewithid($_GET['gameNo']);
echo "<tr>";
echo '<td>' . $row["round_id"].'</td>';
echo "<td>" . $tempMatchName. "</td>";
echo "<td>" . $tempPlayerName. "</td>";
echo "<td>" . $row["shot_1"] . "</td>";
echo "<td>" . $row["shot_2"] . "</td>";
echo "<td>" . $row["shot_3"] . "</td>";
echo "<td>" . $row["shot_4"] . "</td>";
echo "<td>" . $row["shot_5"] . "</td>";
echo "<td>" . $row["shot_6"] . "</td>";
echo "<td>" . $row["shot_7"] . "</td>";
echo "<td>" . $row["shot_8"] . "</td>";
echo "<td>" . $row["shot_9"] . "</td>";
echo "<td>" . $row["shot_10"] . "</td>";
echo "</tr>";
$i++;
}
echo "</table>";

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

Passing array and deleting from MySQL table

I have a table in mysql and I'm printing it on screen by this code:
include ('mysql_con.php');
mysql_select_db("jaz", $con);
$res1 = mysql_query("SELECT * FROM hovno ORDER BY id DESC");
echo "<form action='delete_from_db.php' method='POST'>";
echo "<table border='1'>";
echo "<tr>";
echo "<td colspan='6'>" . "<input type='submit' value='Delete Article'>" . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>" . "<b>" . "Delete" . "</b>" . "</td>";
echo "<td>" . "<b>" . "ID" . "</b>" . "</td>";
echo "<td>" . "<b>" . "Date" . "</b>" . "</td>";
echo "<td>" . "<b>" . "Title" . "</b>" . "</td>";
echo "<td>" . "<b>" . "Preview" . "</b>" . "</td>";
echo "<td>" . "<b>" . "Author" . "</b>" . "</td>";
echo "</tr>";
while($a=mysql_fetch_array($res1)) {
echo "<tr>";
echo "<td>" . "<input type='checkbox' name='checkbox[]' value='" . $a["id"] . "'>" . "</td>";
echo "<td>" . $a["id"] . "</td>";
echo "<td>" . $a["created"] . "</td>";
echo "<td>" . $a["title"] . "</td>";
echo "<td>" . $a["preview"] . "</td>";
echo "<td>" . $a["author"] . "</td>";
echo "</tr>";
}
echo "</table>";
echo "</form>";
So I'm putting id's as checkbox values into array.
In another php file I wan't to use this id's in array to delete this records with those id's. I don't know what code to use for that buy i tried this:
<?php
include ('mysql_con.php');
mysql_select_db("jaz", $con) or die(mysql_error());
$del[]=$_POST["checkbox"];
$res1 = mysql_query("SELECT * FROM hovno WHERE id='$del'") or die(mysql_error());
while($a = mysql_fetch_array($res1)){
$b = $a["id"];
mysql_query("DELETE FROM hovno WHERE id ='$b'") or die(mysql_error());
}
?>
Any idea how to actually make it work? Thanks
Although I'm not sure about PHP, if you get the list of IDs, separated by commas you can use SQL operator "in". Something like this:
$res1 = mysql_query("SELECT * FROM hovno WHERE id in (1,2,3)")
comment about variable names:
A jestli si ty promenne slusne nepojmenujes, tak se v tom uz nikdy nevyznas ;)
<?php
$del = $_POST['checkbox'];
$idsToDelete = implode($del, ', ');
$res1 = mysql_query("SELECT * FROM hovno WHERE id in ($idsToDelete)");
?>
foreach($_POST['checkbox'] as $key => $value) {
// delete query here
}

Categories