Update post for: Increase in Value button
I made a post asking about how make an increase value button. I got one decent answer, I tried it and it didn't work.
Original:
$result = mysqli_query($con, "SELECT * FROM champion_counters_b WHERE champion_name='" . $search_result . "'");
echo "<table class='champion_counters' border='1'><tr><th>Champion Counter</th><th>Up Votes</th><th>Down Votes</th></tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['champion_counter'] . "</td>";
echo "<td>" . $row['upvotes'] . "</td>";
echo "<td>" . $row['downvotes'] . "</td>";
}
echo "</table>";
Current:
$result = mysqli_query($con, "SELECT * FROM champion_counters_b WHERE champion_name='" . $search_result . "'");
echo "<table class='champion_counters' border='1'><tr><th>Champion Counter</th><th>Up Votes</th><th>Down Votes</th></tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['champion_counter'] . "</td>";
echo "<td><a href='action.php?do=up&id=" . $row['upvotes'] . "'>Upvote [" . $row['upvotes'] . "]</a></td>";
echo "<td><a href='action.php?do=down&id=" . $row['downvotes'] . "'>Downvote [" . $row['downvotes'] . "]</a></td>";
}
echo "</table>";
mysqli_close($con);
?>
action.php:
$action = $_GET['do'];
$id = $_GET['id'];
if ($action=="up") {
$result = mysqli_query($con, "UPDATE champion_counters_b SET" . $id . "=" . $id+1 . "'");
}
elseif ($action=="down") {
$result = mysqli_query($con, "UPDATE champion_counters_b SET" . $id . "=" . $id-1 . "'");
}
else {echo "error: 002 / voting error";}
mysqli_close($con);
?>
As you can see I implemented the changes that were recommended and that could of worked, but they didn't rather than updating the data it did nothing. As when pressing "upvote"/"downvote" it grabbed the current value as $id rather than the position of the data to update it.
tl;dr: I tried a fix from the previous thread, it didn't do anything. Help please?
sorry if I have been unclear, let me give it another try:
You insert a link into your table, which calls an action.php. The action.php updates the database. The information needed for updating are passed as GET-parameters.
For updating the database, you will need to know WHAT to do, and secondly, WHICH database entry is actually affected.
Step by step:
First, you have to insert the links into your table:
$result = mysqli_query($con, "SELECT * FROM champion_counters_b WHERE champion_name='" . $search_result . "'");
echo "<table class='champion_counters' border='1'><tr><th>Champion Counter</th><th>Up Votes</th><th></th><th>Down Votes</th><th></th></tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['champion_counter'] . "</td>";
echo "<td>" . $row['upvotes'] . "</td>";
echo "<td><a href='action.php?do=up&id=" . $row['key'] . "'>Upvote</a></td>";
echo "<td>" . $row['downvotes'] . "</td>";
echo "<td><a href='action.php?do=down&id=" . $row['key'] . "'>Downvote</a></td>";
echo "</tr>";
}
echo "</table>";
On clicking the link, we are sending two pieces of information to the action.php.
The action we have to perform, either up- or downvoting. It tells us WHAT to do.
The id of the element we have to up- or downvote. The id tells us WHICH element is affected, and not the number of votes an element has.
As mentioned in my answer to your previous post, $row['key'] should contain the primary key of your database, e.g. an unique identifier. You should have such an identifier in your database, otherwise you cannot make a reference to a specific database entry. However, as we want to update the vote count for a very specific database entry, we need a possibility to uniquely identify this database entry.
In your action.php, you have to do the following:
$action = $_GET['do'];
$id = $_GET['id'];
if ($action=="up") {
mysqli_query($con, "UPDATE champion_counters_b SET upvotes = upvotes + 1 WHERE key = " . $id . "'");
}
elseif ($action=="down") {
mysqli_query($con, "UPDATE champion_counters_b SET downvotes = downvotes - 1 WHERE key = " . $id . "'"); }
else {echo "error: 002 / voting error";}
mysqli_close($con);
We use the id value to select the database entry we have to update. This is done by WHERE key = $id in the UPDATE-statement. The current vote count is taken from the database itself - there is no need to pass the current vote count in the GET-parameters.
After you have updated your database, you can redirect to the original page and reload your table. Then, the updated vote count should show up.
Related
I have an interesting challenge that I haven't been able to get my head around, I have a html table that is generated from php while loop and a mysql database, each generated row of this table has a button that allows someone to modify a row in a database, but whenever the button is pressed the action is performed across the whole generated table thanks to the while loop, and I don't know how to only get that row to perform an action.
Here is the code that I have so far in this section.
while ($submissionrow = mysqli_fetch_array($currentsubmissions)) {
if ($submissionrow[approved] === '1') {
$approvedtick = 'Approved';
}else {
$approvedtick = '';
}
if ($submissionrow[rejected] === '1') {
$rejectedsymbol = 'Rejected';
}else {
$rejectedsymbol = '';
}
if ($approvedtick == $blanksymbol && $rejectedsymbol == $blanksymbol) {
$inreviewmessage = 'In Review';
}else {
$inreviewmessage = '';
}
echo "<tr>";
echo "<td>" . $submissionrow[submission_ID] . "</td>";
echo "<td>" . $submissionrow[Username] . "</td>";
echo "<td>" . $currentyear . "</td>";
echo "<td>" . $submissionrow[CSEvent] . "</td>";
echo "<td>" . "";?>
<form method='post' action='<?php $_PHP_SELF ?>'>
<?php
echo "<button type='submit' name='$submissionrow[submission_ID]' id='accept_cs'>Approve</button>";
echo "<button type='submit' name='$submissionrow[submission_ID]' id='reject_cs'>Reject</button>";
?>
</form>
<?php echo "
" . "</td>";
echo "<td>" . $approvedtick . $rejectedsymbol . $inreviewmessage . "</td>";
if(isset($_POST['accept_cs'])) {
$accept_cs_sql = 'UPDATE CommunityService SET approved =1 WHERE submission_ID =' . $submissionrow[submission_ID];
printf($accept_cs_sql);
$retval = $dbcon->query("$accept_cs_sql");
echo "Updated Accepted Status successfully\n";
}
if(isset($_POST['reject_cs'])) {
$reject_cs_sql = 'UPDATE CommunityService SET rejected =1 WHERE submission_ID =' . $submissionrow[submission_ID];
printf($reject_cs_sql);
$retval = $dbcon->query("$reject_cs_sql");
echo "Updated Rejected Status successfully\n";
}
}
I can make the code submit the performed action, but it enacts this action on the ID of every row in the table, not just the row with that button that was pressed.
Thank you in advance
-jx
I'm trying to design a webpage where it simply shows info from a database using PHP.
I almost solve all of my problem but it's the first time I'm using HTML and I am having trouble finding a solution for my problem.
The code I have so far is:
<?php
$connection = mysql_connect('localhost','XXXX','XXXX');
mysql_select_db('cardata_laptimer');
echo "<table>";
echo "<table border = 1>";
$query = "SELECT * from tempos GROUP BY piloto";
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
{
$query = "SELECT * from tempos WHERE piloto = '" . $row['piloto'] . "' ORDER BY tempo";
$resultado = mysql_query($query);
echo "<td>" . $row['piloto'] . "</td></tr>";
while($rows = mysql_fetch_array($resultado))
{
echo "<td>" . $rows['tempo'] . "</td><td>" . $rows['data'] . "</td></tr>";
}
}
The output I'm getting can be seen in www.cardata.pt
1st problem: How to I make the "piloto" (for example AA) occupy the space of 2 cells?
2nd problem: I want the table to show "pilotos" side by side and the info for each one (tempo and data) down the piloto name.
Thanks in advance
To occupy the space of 2 cells add : colspan="2"
here is my edit of your code :
<?php
$connection = mysql_connect('localhost','XXXX','XXXX');
mysql_select_db('cardata_laptimer');
echo '<table border="0"><tr>';
$query = "SELECT * from tempos GROUP BY piloto";
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
{
echo '<td><table border="1"><tr>';
$query = "SELECT * from tempos WHERE piloto = '" . $row['piloto'] . "' ORDER BY tempo";
$resultado = mysql_query($query);
echo '<td colspan="2">' . $row['piloto'] . "</td></tr><tr>";
while($rows = mysql_fetch_array($resultado))
{
echo "<td>" . $rows['tempo'] . "</td><td>" . $rows['data'] . "</td>";
}
echo '</tr></table></td>';
}
echo '</tr></table>';
?>
For some reason this is returning everything from the member_details table when all i want is to return a single row that equals to the selected item in the drop down list.
Here is the php for it: i thought it would return the row which equaled the value of the drop down but its just listing it all.
<?php
if (isset($_POST['members'])) {
$ResultSet = getTableResults("member_details");
echo "<h1> Member Details </h1>";
echo "<table border='1' cellpadding='6'>";
echo "<tr> <th>Id</th> <th>Name</th> <th>Job</th> <th>Wage</th> <th>Hobby</th> ";
foreach ($ResultSet as $row) {
echo "<tr>";
echo "<td>" . $row ['member_id'] . "</td>";
echo "<td>" . $row['first_name'] . " " . $row ['second_name'] . "</td>";
echo "<td>" . $row['job'] . "</td>";
echo "<td>" . $row['wage'] . "</td>";
echo "<td>" . $row['hobby'] . "</td>";
echo "</tr>";
}
echo "<table>";
}
?>
If you want to see more code to make more sense of it please ask and il edit and update the question.
function getTableResults() {
$sql = "SELECT DISTINCT member_details.member_id, members.first_name, members.second_name, member_details.wage, member_details.job, member_details.hobby
FROM members
INNER JOIN member_details
ON members.member_id=member_details.member_id";
$mysqlConnection = getSQLConnection();
$ResultSet = $mysqlConnection->query($sql);
return $ResultSet;
}
You do not have any Condition while requesting your information, getTableResults() fetches all Data that is stored to your table. You need to change that function and add a WHERE xy condition. Probably something like this:
function getTableResults( $id ) {
$sql = "SELECT DISTINCT member_details.member_id, members.first_name,members.second_name, member_details.wage, member_details.job, member_details.hobby
FROM members
INNER JOIN member_details
ON members.member_id=member_details.member_id WHERE member_details.member_id = '$id'";
$mysqlConnection = getSQLConnection();
$ResultSet = $mysqlConnection->query($sql);
return $ResultSet;
}
Then Call the function with the requred member-id as parameter.
Anyway, I'd recommend you to inform yourself about prepared statements to prevent SQL-Injection here!
I have two tables in mysql
practice_sheets and parent_pin
And I want to use one select statement and get data from both tables.
I have tried
$result = mysqli_query($con,"SELECT * FROM practice_sheets AND parent_pin
WHERE student_name='$_SESSION[SESS_FIRST_NAME] $_SESSION[SESS_LAST_NAME]'");
and also:
$result = mysqli_query($con,"SELECT * FROM practice_sheets, parent_pin
WHERE student_name='$_SESSION[SESS_FIRST_NAME] $_SESSION[SESS_LAST_NAME]'");
I've never tried to do this before and the previous solutions are what I found searching.
Update
I think it would help if I included my full code. the table data is going into a table on my page. the student_name field from the practice_sheets and parents_student from parent_pin will be matched.
<?php
$con=mysqli_connect();
// Check connection
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM practice_sheets
WHERE student_name='$_SESSION[SESS_FIRST_NAME] $_SESSION[SESS_LAST_NAME]'");
$numrows = mysqli_num_rows($result);
if($numrows == 0) {
echo "<div class='alert alert-danger'>";
echo "No Entries, See your instructor for details.";
echo "</div>";
} else {
echo "<table class='mws-table table-striped table-hover'>";
echo "<thead align='center'>";
echo "<tr>";
echo "<th>Sheet Number</th>";
echo "<th>Total Minutes</th>";
echo "<th>Due Date</th>";
echo "<th>PIN</th>";
echo "<th>View</th>";
echo "</tr>";
echo "</thead>";
echo "<tbody align='center'>";
while($row = mysqli_fetch_array($result)){
if ($row["total_min"]>=$row["required_min"]) {
echo "<tr class='success'>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['total_min'] . "</td>";
echo "<td>" . $row['due_date'] . "</td>";
echo "<td>" . $row['parent_pin'] . "</td>";
echo "<td> <a href='account/practiceSheets?id=" . $row["id"] . "&total_min=" . $row["total_min"] ."&due_date=" . $row["due_date"] ."&mon_min=" . $row["mon_min"] ."&tues_min=" . $row["tues_min"] ."&wed_min=" . $row["wed_min"] ."&thurs_min=" . $row["thurs_min"] ."&fri_min=" . $row["fri_min"] ."&sat_min=" . $row["sat_min"] ."&sun_min=" . $row["sun_min"] ."&name=" . $row["student_name"] ."&assignment=" . $row["assignment"] ."&required_min=" . $row["required_min"] ."'> <i class='icon-eye-open'> </i> </a> </td>";
echo "</tr>";
} else {
echo "<tr class='info'>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['total_min'] . "</td>";
echo "<td>" . $row['due_date'] . "</td>";
echo "<td>" . $row['parent_pin'] . "</td>";
echo "<td> <a href='account/practiceSheets?id=" . $row["id"] . "&total_min=" . $row["total_min"] ."&due_date=" . $row["due_date"] ."&mon_min=" . $row["mon_min"] ."&tues_min=" . $row["tues_min"] ."&wed_min=" . $row["wed_min"] ."&thurs_min=" . $row["thurs_min"] ."&fri_min=" . $row["fri_min"] ."&sat_min=" . $row["sat_min"] ."&sun_min=" . $row["sun_min"] ."&name=" . $row["student_name"] ."&assignment=" . $row["assignment"] ."&required_min=" . $row["required_min"] ."'> <i class='icon-eye-open'> </i> </a> </td>";
echo "</tr>";
}
}
echo "</tbody>";
echo "</table>";
mysqli_close($con);
}
?>
$result = mysqli_query($con,"SELECT *
FROM practice_sheets, parent_pin
WHERE student_name = parents_student
AND student_name='$_SESSION[SESS_FIRST_NAME] $_SESSION[SESS_LAST_NAME]'");
Use explicit names for WHERE statament, e.g.
$result = mysqli_query("SELECT student_name.practice_sheets FROM practice_sheets AND parent_pin WHERE student_name.practice_sheets = '{$_SESSION['SESS_FIRST_NAME']} {$_SESSION['SESS_LAST_NAME']}'");
MySQL will not AFAIK automatically check where the constraints are and rightly so considering that you may have conflicting names. Note that this is still pseudo code and you will need to change the fetched results accordingly. Usually it is considered to be good practice to also define explicitly the columns you wish to fetch, but otherwise you can use JOIN as well.
And to help writing shorter code, you can also use shorthands for the table names, e.g.
$result = mysqli_query("SELECT student_name.ps AS name, pin.pp AS pin FROM practice_sheets AS ps, parent_pin AS pp WHERE student_name.ps = '{$_SESSION['SESS_FIRST_NAME']} {$_SESSION['SESS_LAST_NAME']}'");
Update
You also have in your updated version an issue. You call mysqli_fetch_array, which returns an ordered (i.e. numbered) array. If you wish to use keyed, use mysqli_fetch_assoc.
And you are closing the MySQL connection at the moment only if the query was successful. Move mysqli_close outside of the brackets.
I have done a little bit of research on this, but currently I'm stuck.
My situation:
My database has a serverName, and serverBanner for each entry. When I do this following code:
function listBanner() {
include("mysql.php");
$votes = "serverVotes";
$results = mysql_query("SELECT * FROM toplist ORDER BY $votes ASC");
while($row = mysql_fetch_array($results)){
echo " " . "<img src=" . $row['serverBanner'] . " height=60 width=460 />";
}
}
If you noticed, it is pulling all the info from toplist table. I need to pull all the info from toplist table, but make it so that I can put each on if them in a table row. Right now if I did that, it would put each banner in the same table row.
Also, how would I go about implementing this into a table?
Do you mean:
<?php
echo "<tr>";
while($row = mysql_fetch_array($results)){
echo "<td>" . $row['serverName'] . "</td>";
echo "<td>" . "<img src=" . $row['serverBanner'] . " height=60 width=460 />"."</td>";
}
echo "</tr>"
?>