So hey guys!
I currently have a code that gets data from a custom table called wp_refundrequests, and prints them as a table to the page. On the page the admin can either Accept, or Deny the request by pressing a button on the side of each order. Denying the request just deletes the request from the table, but accepting should delete it from the current table and insert the information to the next table called "accepted requests".
The wp_refundrequests table contains customer's order that they want to refund.
The code that gets the info and prints it:
global $wpdb;
$requests = $wpdb->get_results("SELECT * FROM wp_refundrequests", ARRAY_A);
foreach ($requests as $row) {
echo "<div class='requests'>" . "<li class='refunds'>" . "Palauttajan nimi: ".
$row['customer_name'] . "</br>" ."Palautettavat tuotteet: ".$row['product_name']."<br> "."Määrä: ".
$row['product_qty'] . " "
. "<br>Kommentti: " . $row['comment'] . "<br> " . "Hinta: " . $row['refund_total'] . "€ " .
"<br>" . "Päivämäärä: " . $row['request_date'] . " " .
"<a class='right' href='admin-page?deleteid=" . $row['request_id'] . "'>Deny</a></li>" .
"<li class='refundaccepts'><a href='admin-page?acceptid=" . $row['request_id']
. "'>Accept</a></li>" . "</div>";
$_SESSION['custname'] = $row['customer_name'];
$_SESSION['prodname'] = $row['product_name'];
}
With my current code, the "Accept" button deletes it, and inserts information in to the new table, BUT the information that is inserted is wrong. It seems like it wants to either insert the latest data that had been inserted in to the wp_refundrequests table to the wp_acceptedrequests, or it keeps the data from the latest refund request and tries to insert that instead because for example as seen here(Sorry for the bits of Finnish as well):
If I were to click the "Accept" button on the above, older one, the query would still insert it like this:
So it basically inserts the info from the latest refund_request insert and inserts that instead of the one selected. However the one that had been selected still gets deleted from the table.
Here's the code that is triggered when the user clicks on "Accept"
$custname = $_SESSION['custname'];
$prodname = $_SESSION['prodname'];
if(isset($_GET['acceptid'])) {
$accept = $_GET['acceptid'];
/* Query to do whatever here */
$wpdb->print_error();
$wpdb->insert("wp_acceptedrequests", [
"customer_name" => "$custname",
"name_product" => "$prodname",
"date" => date("Y/m/d/G:i:sa") ,
]);
$wpdb->print_error();
$wpdb->query("DELETE FROM wp_refundrequests WHERE request_id = $accept");
}
I have to say I have no idea why it doesn't want to insert the selected request, please comment if there's something confusing, I'll try to clear it up then.
Thanks in advance!
You redefine $_SESSION with in foreach loop so at the end of foreach it will equal to the last one, pass each row parameter to it is accept link like this
"<li class='refundaccepts'><a href='admin-page?acceptid=" . $row['request_id']."&custname=".$row['customer_name']."&prodname=".$row['product_name']."'>Accept</a></li></div>";
Then call it the same way you get $accept-ID
if(isset($_GET['acceptid'])) {
$accept = $_GET['acceptid'];
$custname = $_GET['custname'];
$prodname = $_GET['prodname'];
Note:Iuse my phone so make sure if it was a syntax error in the href part of the code
Please try like this and comment out the session variable.
if(isset($_GET['acceptid'])) {
$accept = $_GET['acceptid'];
$accepted_requests = $wpdb->get_results("SELECT * FROM wp_refundrequests WHERE id = $accept", ARRAY_A);
if( !empty($accepted_requests) ) {
$insert = $wpdb->insert("wp_acceptedrequests", $accepted_requests);
if($insert) {
$wpdb->query("DELETE FROM wp_refundrequests WHERE request_id = $accept");
}
}
}
Related
So basically I have the following code:
if(isset($_GET['deleteid'])) {
$delete = $_GET['deleteid'];
$denyrequest = "DELETE FROM wp_refundrequests WHERE request_id = $delete";
}
$requests = $wpdb->get_results("SELECT * FROM wp_refundrequests", ARRAY_A);
foreach ($requests as $row) {
echo "<li>" . $row['product_name'] . " " .
"<a href='admin-page?deleteid=" . $row['request_id'] . "'>Deny</a></li>" .
"<a href='admin-page?acceptid=" . $row['request_id'] . "'>Accept</a></li>";
}
Where if users clicks either Deny or Accept on the list of items, it adds a parameter to the URL and with that parameter, deletes the wanted row from the database. Currently however after pressing on any of the Deny (Or Accept) links, the result is the following, where there is a slash before the parameter:
I have done the same kind of code to delete DB data using a list once before, but it was without WordPress and I had no problems by just linking the file normally. The deletion doesn't go through and I'm assuming that this is the cause.
Basically at the moment nothing happens other than the URL looks like that.
Thank you in advance!
Like comment stated, you do not take any delete action, just prepare a string
Try adding something like:
$wpdb->delete( "wp_refundrequests", array( 'request_id' => 1 );
to your 1st if:
if(isset($_GET['deleteid'])) {
$delete = $_GET['deleteid'];
$wpdb->delete( "wp_refundrequests", array( 'request_id' => 1 ));
}
reference
So, I'm using a database to store ship data.
As I load data into the database, I am checking whether the ship already exists. Sometimes, more than one ship has the same name. This code tries to go through the array, pull out all the ships with the same name, and then ask, in turn if that is the right one- if not, then it's yet another with the same name.
$sql = "SELECT Ship_Primary_Key, ship_original_rate, Ship_Launch_Year from Ships WHERE Ship_Name = '" . $shipname . "'";
$result = $conn->query ($sql);
if ($result-> num_rows > 0) //Does the ship name already exist in the DB? {
$ships_in_db = mysqli_fetch_all ($result,MYSQLI_ASSOC);
foreach ($ships_in_db as $row) {
echo "new record is " . $shipname . " as a " . $shiprate . ". Records already include " . $shipname . ", launched " . $row["Ship_Launch_Year"] . " as a " . $row ['ship_original_ra
$yesno = trim(fread(STDIN,5));
if ($yesno == "y" || $yesno == "yes") {
//ship already exists in the DB. Get the Key
echo $shipname . " is not new to the database and the key is " . $row["Ship_Primary_Key"] . "\n";
$shipkey = $row["Ship_Primary_Key"];
break 1;
}
}
//if you get through the loop of ships without assigning a primary key, ship is new
if (empty($shipkey)) {
$shipkey = write_ship_to_DB($shipname,$shiprate,$launchyear,$launchname,$conn);
}
}
So the problem is, I know that I have at least three ships with the same name in the first set of data (that are different). The problem is, it only ever asks about the first one. When I put 'n', it just goes on, and never asks about the second ship with the same name that already exists.
I think it's a problem with the Foreach loop and the break statement.
I'd appreciate any help with this
I've figured out the problem.
Because of the loop- I wasn't resetting the "Ship_Id" variable, which meant that the second or nth time a ship with the same name came around, a new ship wasn't created.
Now I've solved that. So the problem isn't this code at all- it's other code.
I'm doing a movie library as a school assignment and have list of movies from a database table that I want to have a edit and a delete option next to on each row.
I want to use a edit/delete links for it. Like:
"<a href='moviestorage.php?edit=" . $id . "'>Edit</a>"
But I'm not sure how I can fish up the id for each movie so that it's deleted from the database. What's the query that I should write? Do I need to have a separate delete.php file?
I´m a very newbie so bear with me:)
Below you can see the code that I've done.
<?php
require 'connect.inc.php';
//This feels incomplete... I´m trying here to fish the ID...
$id = "SELECT id from movies";
$query = "DELETE FROM movies WHERE id='$id'";
$query = "SELECT * FROM movies, categories WHERE movies.genre_id = categories.genre_id";
$result = mysql_query($query);
if (!$result) die ("Database access failed:" .mysql_error()) ;
$rows = mysql_num_rows($result);
echo '<table><tr><th>Title</th><th>Release year</th><th>Genre</th><th>Director</th><th>Update</th><th>Delete</th></tr>';
for ($j = 0 ; $j < $rows ; ++$j) {
echo '<tr><td>' . mysql_result($result,$j,'title') . '</td>' ;
echo '<td>' . mysql_result($result,$j,'release_year') . '</td>' ;
echo '<td>' . mysql_result($result,$j,'genre') . '</td>' ;
echo '<td>' . mysql_result($result,$j,'director') . '</td>' ;
echo '<td>'."<a href='edit_movie.php?edit=" . $id . "'>Edit</a>".'</td>' ;
echo '<td>'."<a href='delete.php?delete=" . $id . "'>Delete</a>".'</td></tr>' ;
}
echo '</table>';
include 'add_movie.php';
?>
I would recommend that you use a form for every row with a delete and edit button and a hidden field with the ID. You can then post that form to the right script and determine the action to take there.
If you have to use a link to delete an item, at least have the link lead to another confirmation page with a form that the user has to submit and that posts to your delete script.
if you have to edit and delete on new page than you can put hyperlink this way
echo "<a href=\"edit.php?id=".mysql_result($result,$j,'id')."\"><strong>EDIT</strong>";
echo "<a href=\"delete.php?id=".mysql_result($result,$j,'id')."\"><strong>delete</strong>";
and for conformation for delete you can use this on delete link
echo "<a href=\"delete.php?id=".mysql_result($result,$j,'id')."\" onclick=\"return confirm('You want to delete your own account???');\"><strong>delete</strong>";
than get the id on next page using
$id = $_GET['id'];
hope it will be usefull for you
If you need a simple and effective solution, use datatables. :)
please search in google. spent some time to study how to implement it, it will save a lot of time in future.
After using the search criteria for my database search, I do get the desired records, but I dont want to display all the fields(85 fields) for each of the records; instead I've displayed only 5 of them, and then wish to give a hyperlink saying "Click for further Details", so that the users can then go ahead and view all the fields for a particular record.
For eg, if the user enters "abc" in Search box, and the database contains 5 records with "abc", then it'll show all 5 records, but only 5 specific fields. But to view all the fields, for say the 1st "abc" record, the user will click on "further details", which will direct him to another page with all the 85 fields for that particular 1st abc record.
Kindly guide me through! I dont know what conditions to give, because i tried doing it with header(Location), but displays all the records, and doesn't even take that particular processed sql query!
Here's an example of what you could do. It's a self-referencing search page that first uses a generic query to get all records. Then, as it cycles through each record (only grabbing specific fields, it makes a form that uses a hidden variable with the Primary Key/Unique ID to send to itself (search.php) as a POST variable. Then, when that POST variable is detected, the query only grabs that 1 specific record and using an IF/THEN statement in the WHILE loop, shows all 80+ fields.
There are better ways to do querying and handling form data, but this should get you started. Please select this as an answer if it works.
search.php
<?php
$query = "SELECT * FROM table";
if (!empty($_POST['id'])) {
$query = "SELECT * FROM table WHERE id_field = '" . $_POST['id'] . "'";
}
$query_info = mysql_query($query) or die(mysql_error());
$query_info_count = mysql_num_rows($query_info);
while($query_info_details = mysql_fetch_array($query_info)){
if ($query_info_count > 1) {
$field_a = $query_info_details['field_a'];
$field_d = $query_info_details['field_d']; // Say this is the Primary Key
$field_f = $query_info_details['field_f'];
$field_g = $query_info_details['field_g'];
$field_s = $query_info_details['field_s'];
echo "<form method='post' action='search.php'>";
echo "Name: " . $field_a . " - " . $field_f . " - ID: " . $field_d;
echo " - Location: " . $field_g . "(" . $field_s . ")";
echo "<input type='hidden' name='id' value='" . $field_d . "'>
<input type='submit' value='View This Record'><br><br>";
}
else {
// Display all fields for record using $query_info_details['field_X'] from above
}
}
?>
$where_str="";
if(isset($_POST['searchBox']))
{
$searchStr=$_POST['searchText'];
$where_str="colx =". $searchStr ;
}
$standard_query ="select col1, col2,col3,col4,col5 from tableName ". $where_str;
Then in display
while($row=mysql_fetch_array($records))
{
echo"<a href='' onclick='openDetail(\"$row[index]\")'>$row[index] </a>";
...
}
Javascript
function openDetail(index)
{
window.open("Detail.php?rowindex"+ index);
return false;
}
// Detail.php
if(isset($_GET['rowindex'])
{
$row_index=$_GET['rowindex'];
$detail_query="select * from tableName where index=$row_index";
...
}
I'm working at this thing that takes information from a MySQL database that comes from a contact form and displays it all to the user. Well, I got that far without issues using:
$result = mysql_query("SELECT * FROM contact");
while($row = mysql_fetch_array($result))
{
echo $row['name'] . "<br/>" . " " . $row['email'] . "";
echo "<br/>";
echo $row['message'];
echo "<br />";
}
Now I would like to add a button next to each item that would single them out, basically taking me to a page where only the selected row shows up, not all of them, as happens on this page.
The thing is that i don't know how to do that. So, does anyone have any ideas?
Thanks!
Add a button that refreshes the page with a "GET" parameter, such as yourpage.php?uid=123; Then do
if ($_GET['uid'] !== null) {
$id = (int) $_GET['uid'];
$result = mysql_query('SELECT * FROM contact WHERE id='.$id);
...
}