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.
Related
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");
}
}
}
In the top of my admin PHP (admin.php), I have loaded data from "Sport" table in "DogSport" MySQL database. After clicking "Edit" link of a particular record, I can
edit, delete that record on "editform2.php". Even on that page, I can also insert a new record.
Problem: Deleting first and second records do not work! Deleting all following records work fine. This problem only with deleting functionality.
Code segment - admin.php
<?php
include("dbconnect.inc.php");
//Retrieving Data
$query1="SELECT * FROM Sport ORDER BY SportId ASC";
$result2=mysqli_query($con, $query1);
$rows=mysqli_num_rows($result2);
$i=0;
if($rows==0)
echo "<br/>There are no records";
else{
echo "<table id='sport'>";
echo "<tr><th>Sport Id</th><th>Sport Name</th><th>Description</th></tr>";
while ($row = mysqli_fetch_array($result2))
{
echo "<tr><td>" . $row["SportId"] . "</td><td>" . $row["SportName"] . "</td><td>" . $row["Description"] .
"</td><td> <a href='editform2.php?id=" . $row["SportId"] . "' target='_blank'>Edit</a></td></tr>";
$i++;
}
echo "</table>";
}
?>
Code segment - editform2.php
if(isset($_POST["delete"])){
include("dbconnect.inc.php");
$query3="DELETE FROM Sport WHERE SportId=$id";
mysqli_query($con, $query3);
echo "<h1>Deleted Successfully</h1>";
mysqli_close($con);
}
I have uploaded necessary files, you can also use to check it including database .sql script. Your help is appreciated.
https://www.dropbox.com/s/wn40u93oa1sxcph/SO.rar?dl=0
$query3="DELETE FROM Sport WHERE SportId=$id";
Where did you set value of $id? It should be $_POST["id"] right?
As a side note - SQL injection risk ...
Hey i need to get information from a database and display it on my index.php.
<html>
<body>
<?php
$connection = mysql_connect('localhost', 'root', ''); //The Blank string is the password
mysql_select_db('users');
$query = "SELECT * FROM employee"; //You don't need a ; like you do in SQL
$result = mysql_query($query);
echo "<table>"; // start a table tag in the HTML
while($row = mysql_fetch_array($result)){ //Creates a loop to loop through results
echo "<tr><td>" . $row['name'] . "</td><td>" . $row['age'] . "</td></tr>"; //$row['index'] the index here is a field name
}
echo "</table>"; //Close the table in HTML
mysql_close(); //Make sure to close out the database connection
?>
</body>
</html>
however this will show the information for all of the users in the database.
I have a primary key in the database, which is the account number, could we use this to only display information about that account on the page only?
I will also need to save the details into variables on the php page so some guidance doing that would be greatfull.
Thanks Oliver
In order to retrieve data according to a specific user you must explicitly tell mysql. So, add WHERE id={the I'd u want the info for}.
For your second question. "How do u get the id when the user logs in".
The answer to this is you can (a) how a session initialized at login or (b) how another file or even a loop in the script you need the Id for to run a query for the users name and then you get the Id.
The draw backs of option b is that you are slowing this done and are doing alot of call which can crash your overall site.
The choice is your this is how you can get a particular I'd for a user.
<?php
include('session.php');
?>
<html>
<body>
<?php
$connection = mysql_connect('localhost', 'root', 'Oliver'); //The Blank string is the password
mysql_select_db('users');
$query = "SELECT * FROM username WHERE username='$login_session'"; //You don't need a ; like you do in SQL
$result = mysql_query($query);
echo "<table>"; // start a table tag in the HTML
while($row = mysql_fetch_array($result)){ //Creates a loop to loop through results
echo "<tr><td>" . $row['name'] . "</td><td>" . $row['username'] . "</td></tr>"; //$row['index'] the index here is a field name
echo "<tr><td>" . $row['sex'] . "</td><td>" . $row['phone'] . "</td></tr>"; //$row['index'] the index here is a field name
echo "<tr><td>" . $row['email'] . "</td><td>" . $row['password'] . "</td></tr>"; //$row['index'] the index here is a field name
}
echo "</table>"; //Close the table in HTML
mysql_close(); //Make sure to close out the database connection
?>
</body>
</html>
I got the WHERE username="$login_session"
and the login_session value is a unique value for each of the users in the database so it can never be the same, so i can use this to pull up information for that user only
You can use session variables to store the information in the case you are working with sessions, if not, you can use a regular variable to store the information or maybe use post or get variables
Okay, so what I've got here is a PHP generated table in which the values are pulled from a database. Now, if the administrator is logged in, I'd like to add an option to delete a row from the database.
I've got the structure all set up, but I have no idea what to put in the path of the link that is supposed to delete the row. How can I do this?
<?php
$rb = 1;
include 'konekcija.php';
$query = "SELECT * FROM oglasi";
$rezultat = mysql_query($query);
while($niz = mysql_fetch_array($rezultat)){
echo "<tr><td>" . $rb . "</td><td>" . $niz['model'] . "</td><td>" . $niz['cena'] . "</td><td><img src='" . $niz['slika_path'] . "'</img></td>";
if($_SESSION['uloga'] == "admin"){
echo "<td><a href='#'>Obrisi</a></td>";
}
echo "</tr>";
$rb++;
}
?>
You could try something like
echo "<td><a href='delete.php?row=" .$niz['id']. "'>Obrisi</a></td>";
Where delete.php is a php page that checks if the user is an administrator, parses the id value via GET (always remember to do proper escaping to avoid injections, or use other query systems, like prepared statements or stored procedures), and deletes the row with the specified id. Of course every record must have a unique id. Eventually the page redirects to the table view page.
Important sidenote: mysql_* functions are deprecated, use mysqli_* ones instead!
Well first of the link that you should use to delete the row is going to be whatever link you create.
Therefore you will have to create a new php page, or modify an existing one. To handle the requests to delete the rows.
Here is a quick example:
handler.php
<?php
//execute database connection here
switch($_GET['action'])
{
case 'delete':
mysql_query('DELETE FROM `TABLE_NAME` WHERE '.$_GET['where']);
break;
case 'add':
//parse info and submit request to db
break;
case 'edit':
//parse info and submit request to db
break;
}
//close db connection
?>
Disclaimer! This example is by no means secure, do not use it in production.
Then the absolute link you would use would be something like: (unescaped version)
http://host.com/pathtofile/handler.php?action=delete&where=(row_name='rowid')
guo
$rb = 1;
include 'konekcija.php';
$query = "SELECT * FROM oglasi";
$rezultat = mysql_query($query);
while($niz = mysql_fetch_array($rezultat)){
echo "<tr><td>" . $rb . "</td><td>" . $niz['model'] . "</td><td>" . $niz['cena'] . "</td><td><img src='" . $niz['slika_path'] . "'</img></td>";
if($_SESSION['uloga'] == "admin"){
echo "<td><a href='#'>Obrisi</a></td>";
}
echo "</tr>";
$rb++;
}
?>
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);
...
}