Increase in Value button - php

I wish to be able to create a button that increase the number of upvotes and decreases the number of downvotes
$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>";
As you can see, I am currently echoing a table until there are no search results left for the input. As you can see $row['upvotes'], $row['downvotes'] these are the things that i'd like to be able to generate a button for, on each row.
"Upvote" => upvotes + 1 => 1, 2, 3, etc
"Downvote" => downvotes - 1 => -1, -2, -3, etc
tl;dr: Would like to be able to generate buttons for each row to increase the number of upvotes and decrease the number of downvotes

This might not be what you are looking for, but as far as I understood, this would be my solution:
You will most probably have some kind of primary key in your database. You do actually get it here as you perform a "SELECT *", e.g. retrieve all columns.
Based on the primary key, you can now implement your down- and upvoting functionality. You could for example insert the following into your while-loop (let $row['key'] be the primary key):
echo "<td><a href='action.php?do=up&id=" . $row['key'] . "'>Upvote</a></td>";
echo "<td><a href='action.php?do=down&id=" . $row['key'] . "'>Downvote</a></td>";
Using this, each table row will have two links which will send the user to an action.php and append some GET-Parameters to the URL. The GET-Parameters are "do", which will tell you what to do (either up or down) and "id", which is the primary key.
In action.php, you can read those GET-Parameters and perform any further action, like updating the database. You can get them like this:
$action = $_GET['do'];
$id = $_GET['id'];
Depending on $action, you can now update the downvotes or the upvotes. You can identify the item to update with $id.

Related

Select, Insert MySQL and PHP

I have created a search function and was able to display the rows.
But I have no idea what to do about this thing:
For example, I searched name starting with J, and lists all the names starting with J with login time and has not been logout. Meaning, the timeOut has been set to null from the first place. It displays like:
1 | Johny | 7:58 | (timeOut)
4 | Jess | 8:05 | (timeOut)`
The (timeOut) is a input type="button". And I what I want is whenever I click the logout, for example, I click timeOut for Johny, it will insert values now() for the timeout, not affecting the row for Jess.
I have this code:
<?php
$result=mysqli_query($con,"SELECT * FROM records WHERE PlateNumber LIKE '%$name%'");
while($row = mysqli_fetch_array($result)) {
if($row['TimeOut'] == null){
echo "<table border='1'>" . "<tr>" ."<td style='width:100px;'>" . $row['Number']. "</td>". "<td style='width:100px;'>" . $row['PlateNumber']. "</td>". "<td style='width:100px;'>" . $row['TimeIn'] . "</td>" ."<td style='width:100px;'>" . "<form>" . "<input type='submit' name='logout' value='Log Out'/>" . "</form>". "</td>" . "</table>" ;
if(isset($_POST['logout'])){
//HELP ME IN THIS PART
}
}
}
?>
I'm not really good at php so please bear me with this. Thanks for your help.
I think you meant to update not to insert, and you have to feed the userid to update that particular row.
Also you do not need to spawn every form in the loop. Wrap the table with the form instead.
if(isset($_POST['logout'])) { // if logout button is submitted
// do not insert but update
$id = $con->real_escape_string($_POST['logout']);
$query = $con->query("UPDATE records SET TimeOut = NOW() WHERE id = '$id'");
}
$name = $con->real_escape_string($name);
$result = mysqli_query($con, "SELECT * FROM records WHERE PlateNumber LIKE '%$name%'");
echo '<form method="POST">';
echo '<table boder="1" cellpadding="10">';
while($row = mysqli_fetch_assoc($result)) {
if(empty($row['TimeOut'])){
$id = $row['id'];
echo
"<tr>" .
"<td style='width:100px;'>" . $row['Number']. "</td>".
"<td style='width:100px;'>" . $row['PlateNumber']. "</td>".
"<td style='width:100px;'>" . $row['TimeIn'] . "</td>" .
"<td style='width:100px;'>" .
"<button type='submit' name='logout' value='$id'>Logout</button>" . "</td>" .
"</tr>";
}
}
echo '</table>';
You need to put a record ID of some sort in the 'value' portion of your input. Not just "log out". You need to post "log out jess" (or better yet, just "Jess" so you don't have to edit it when you retrieve it to remove "Log Out" from the string). Usually this is done with a primary key field. But it can also be done with any unique field. So instead of value='Log Out' try value='Jess'.
Then $_POST['logout'] will actually contain Jess and you can build your update sql query from it.
Another method would be to skip the form entirely and just build a web link on the "Log Out" next to "Jess" which would contain "GET" parameters instead of POST. May be easier on the eye.
Instead of a select statement you need to use an update statement and use a where clause to scope the record to the current user based off user id.
So for you it would be like this:
UPDATE records
SET logouttime=now()
WHERE userid='4'

PHP Add button to view all the contents of entire record MySQL

Currently I have a query running that brings up all the contents of the 'products' table and the 'user' associated with the products. It prints on a table. I want to create a button, that brings up the entire records.
Please note that I must only be able to view the selected record only.. How would I go about this?
echo "<table border='1'>
<tr>
<th>user_id</th>
<th>user_name</th>
<th>selected_product</th>
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
echo "<td>" . $row['user_id'] . "</td>";
echo "<td>" . $row['user_name'] . "</td>";
echo "<td>" . $row['selected_product'] . "</td>";
So a button should appear for each record that is returned as a result. When I click this button another window should appear which shows the entire contents (for this record only) and the user associated.
How does the button know which record its associated with.
<a href="anotherpage.php?id=<?php echo $row['user_id']?>" ><button>View Details</button></a>
This will take you to another page.
Use get method tho get the id into php variable.
$userId = $_GET['id'];
And now use sql query to fetch all the data from the database for single user.
$userResult = mysql_query("SELECT * FROM userTable WHERE user_id = ".$userId);

how do i reset particular fields from a row in table using php

i have to reset columns of the particular field from the row in database. I am using MYSQL and in a database table i have given multiple fields like
1) Sr. Number
2) Serial Key
3) Activation Date
4) Valid From Date
5) Valid Till Date
6) Name
7) Device ID
8) Activity Count
i have made one form in which i am fetching the database entries. and after each entry i have given reset button, on the click of which i have to reset serial key and device id from database. I got one suggestion to take pointer to the Sr. number. but i am not getting any idea of resetting these two columns. in which 1 is at no.2 position and another is at no.7. Your suggestion will be needful and if any examples u know then please suggest.
echo "<table align='center' id='tbl' border='1' >"; // start a table tag in the HTML
echo "<tr><th>id</th><th>device_id</th><th>serialkey</th><th>firstname</th><th>created_on</th><th>keygenerated_on</th><th>modify_on</th><th>expiry_date</th><th>active</th><th>delete</th></tr>";
while($row = mysql_fetch_array($query)){ //Creates a loop to loop through results
echo "<tr><td>" . $row['id'] . "</td><td>" . $row['device_id'] . "</td><td>" .$row['serialkey'] . "</td><td>" .$row['firstname'] . "</td><td>" .$row['created_on'] . "</td><td>" .$row['keygenerated_on'] . "</td><td>" .$row['modify_on'] . "</td><td>" .$row['expiry_date'] . "</td><td>" .$row['active'] . "</td><td><a href='display.php?del=0&id=".$row['id']."'>delete</a></td></tr>"; //$row['index'] the index here is a field name
this is a sample code which i have used for delete. please suggest me for reset.
attached screenshot also
In your listing table (shown in the screen shot) instead of putting 'delete' link do
<button type="button">Reset</button>
In some_script.php
<?php
$reset_id=$_GET['reset_id']; //consider sanitizing the data
$query="update <the_table_name> set `serial key` = NULL, `device id`= NULL where id = '".$reset_id."' "; //assuming reset mean setting to NULL. Replace <the_table_name> with the table name you are working with
$con=mysqli_connect("yourhost","db_username","db_password","your_db_name");
if(!mysqli_query($con,$query))
{
//error
}
//else you did it
Why don't you put the values of device_id and serial_id in text fields. Then you can add a new 'reset' button that will reset the fields to the required values when clicked.
<td><input type='text' name='serialkey' value='$row[serialkey]'></td>

I need to remove some characters from a SQL field without altering database

I am using a database that was already created and I can access, but do not have the permission to alter the database at all.
I am using the query
select last_count, query, job_id from twitterinblack46.job where job_id in ('.$job_id_all.') order by last_count desc;
to call three columns (last_count, query, and job_id) and display them in a table.
This query works as I want it to, but the only issue is the query column displays data with either a "%23","%40","%20", or "q=" in front of the desired data.
I need to figure out how to get rid of these strings before displaying the table.
Here is the while statement used to generate the table:
while($row = mysql_fetch_array($result)){
echo"<tr>";
echo "<td>" . $row["job_id"] . "</td>";
echo "<td>" . $row["last_count"] . "</td>";
echo "<td>" . $row['query'] . "</td>";
echo "<tr>";
}
echo "</table>";
I have created this query:
select replace(replace(replace(REPLACE(query,'%23', ''),'%40',''),'q=',''),'%20','') from job;
to get rid of these characters and it works perfectly as I need it, but how can I incorporate this query into my other $result before creating the table?
You can remove the offending strings when printing the table:
echo "<td>" . str_replace(array('%23', '%40', '%20"', 'q='), '', $row['query']) . "</td>";
(If you want to limit removal to only characters at the beginning of the string you can look at preg_replace)
$lst_search = array("%23", "%40", "%20", "q=");
$query = str_replace($lst_search, "", $row["query"]);
Then use $query in place of $row["query"] when creating the table.

Include different mysql data on a page based on the users choice

I have a problem here I can't solve. I have a database of houses with country, location, price etc entities. I print only the countries in a table using:
while ($data = mysql_fetch_array($select))
where $select is a query that selects specific data from the db. I want to display the full information of the selected db data on a different page i have created. The problem is that i don't know how to take the price of the data the user selected. When i get the price of the $_SESSION['counter'], its the number of the last entity of the db. . I don't want to use javascript etc. Here's the code of the page:
<?php
require_once 'php/core.php'; // opens the database
require_once 'php/openDB.php'; // starts the new session
$select = mysql_query("SELECT ID, Country, City FROM Houses");
$counter = 1;
echo "<form action='house_profile.php' method='get'>
<table width='400'>
<tr>
<td>No.</td>
<td>Country</td>
<td>City</td>";
echo "<tr>";
while ($data = mysql_fetch_array($select))
{
echo "<tr>";
echo "<td>" . $counter . "</td>";
echo "<td>" . $data['Country'] . "</td>";
// echo "<td>" . "<a href='house_profile.php' type='submit' value='$counter'>" . $data['City'] . "</a>" . "</td>";
echo "<td>" . $data['City'] . "</td>";
echo "<td><input type='submit' value='info' name='" . $counter . "'></td>";
echo "</tr>";
$_SESSION['counter'] = $counter;
$counter++;
}
echo "</table>
</form>";
?>`
Clarification:
You can pass any value to house_profile.php by adding a ? to start the querystring, then the name of the variable and the value. If you want to pass the id to house_profile then the link should look like this.
"<a href='house_profile.php?ID=" . $data['ID'] . "'>"
then in house_profile.php update your query like this
$select = mysql_query("SELECT * FROM Houses where ID = " . $_GET['ID']);
Now only the entity they clicked will be available.
FYI $counter is just the count, it's not necessarily the ID of the entity. e.g. if you delete ID 1 your records will start from ID 2 but counter will always start with 1. So to reference your entities correctly use the database ID.
Your query retrieves each house's ID from the database but doesn't display this (unless I don't understand your code). When the user chooses a house, you have to retrieve the ID and then use this in a separate query, such as
select price
from houses
where id = :ID
Currently you're displaying a line counter on your form as opposed to the house's ID - which explains why you write...
When i get the price of the $_SESSION['counter'], its the number of the last entity of the db.
You're passing the value of the row counter to the second query instead of the house's ID.

Categories