I have a simple table with a accept and decline button in each row. If you press accept i need it to add 10 to a MYSQL column based on a unique reference. If decline add 20 with the same reference (one per row)
I have the following code to create the table and buttons.(connection etc taken out)
<form Action="leaveupdate.php?" method="POST" >
$query= "
SELECT t0.*, t1.id_user as t1user, t1.name as t1name
FROM $loc t0
LEFT JOIN login1st t1 ON t0.User_id = t1.id_user
WHERE t0.status >=10 and status <20 ORDER BY Date_Input ASC
";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
$fieldCount = mysql_num_fields($result);
echo "<TABLE border='1' cellspacing='0' cellpadding='15' id='sort_table' width='490px'> <thead><TR bgcolor=#ffffff>";
?>
<legend>Research results!</legend>
<table border="1" width="75%" cellpadding="2" cellspacing="0">
<tr>
<TD align="center">Requestor</TD>
<TD align="center">Leave type</TD>
<TD align="center">Date requested</TD>
<TD align="center">Date Requested</TD>
<TD align="center">ACCEPT</TD>
<TD align="center">DECLINE</TD>
</tr>
<?
echo "</TR></thead><tbody>";
while ($row = mysql_fetch_assoc($result)) {
echo '<tr>';
echo'<td align=middle>' . $row["t1name"] . '</td>' ; // Requestor
echo'<td align=middle>' . $res . '</td>' ; // Status
echo'<td align=middle>' . date("l", $row["date_effected"]) . "<BR>". date("j-M-Y", $row["date_effected"]) . '</td>' ; // Day off
echo'<td align=middle>' . date("d-m-y @ H:i", $row["Date_Input"]) . '</td>' ; //Date Requested
echo'</td><td><input type="submit" name="edit" value="10" class="buttons2">';//Add 10
echo'</td><td><input type="submit" name="edit" value="20" class="buttons3">';//Add 20
echo'<td alighn=middle>'.$row["Unique_id"].'</td>';Just for testing not needed will be removed from the table
echo '</tr>';
}
echo "</tbody></TABLE>";
?>
</table>
</body>
</html>
This is posted ( i use get to see what happens but will be changed) to this page
<?php
$loc = $fgmembersite->UserLocation();
$conn = mysql_connect("****","******","Pword");
if (!$conn) die ("Could not connect MySQL");
mysql_select_db("****",$conn) or die ("Could not open database");
if(!isset($_POST['edit']))
{
//add die here
}
$IU = ($_POST['Unique_id']);
$change = ($_POST['edit']);
$sql_query_update = "update $loc set status = status+$change where unique_id = IU";
mysql_query( $sql_query_update );
echo $sql_query_update;//only for testing will be removed
mysql_Close()
?>
as you can see i was hoping to have two variables IU and change, but can’t figure it out. Probably easy but if some could assist it would be helpful.I have been reading the web and seen reference to AJAX but have never used or looked into this.
Thanks JD
Instead of trying to transport multiple pieces of data via the submit button, use a hidden
<input>:The ID will be available in
$_POST['unique_id'].Also consider adding a check to prevent the user from freely setting the amount to change. If you only want to change by
10or20then enforce that with a server side check – currently you are accepting the actual amount to change by, via user input.Side note: the
mysql_*library is deprecated, it is recommended to upgrade to PDO or MySQLi. Your code is vulnerable to SQL Injection, you should upgrade your MySQLi library and use a Prepared Statement or at least use the appropriate escaping function (mysql_real_escape_string()in this case).