Access to MySQL database through normal php webpage?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ecopetition
Forum Newbie
Posts: 3
Joined: Wed Sep 06, 2006 3:53 pm

Access to MySQL database through normal php webpage?

Post by ecopetition »

I've just developed a web page, which allows me to view data which is in a MySQL database table straight form the page.

However, i want to see if i can take this one step further.

I want to be able to delete entries into this database from this same page. Does anyone have a suggestion?

Thank you for helping a newbie :D
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

You will need an HTML form to select which record(s) you want to delete. And send the action of the form to a PHP script that will perform the SQL query(ies).
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
ecopetition
Forum Newbie
Posts: 3
Joined: Wed Sep 06, 2006 3:53 pm

Post by ecopetition »

:oops: I wish I could understand. The problem is, is that although I can build CSS and HTML pages, i suck when it comes to MySQL and PHP. Can you explain more fully?
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

Post by andym01480 »

phpmyadmin has a neat look for that.


Each row of your table will be a row from the database with a unique id lets call it item_id (assuming your table has an id column autoincrementing!)

In your table of results have a column with links that goes to say deleterecord.php?item=$item_id
Where $item_id is the id of that record row.

Then in deleterecord.php perform a check that you want it deleted and then delete the record.

Code: Select all

if (isset($_POST[confirm])){//delete record
$id=$_POST[id];
$query="DELETE * from tablename WHERE idname='$id'";
mysql_query($query) or die ("Couldn't delete");
exit("Done");
}
$id1=$_REQUEST['item_id'];
//check right record and confirm delete
$query1="SELECT FROM tablename WHERE idname='$id1'";
$result=mysql_query($query1);
$row=mysql_fetch_assoc($result);
//output row


//form for confirm
echo "Are you sure you want to delete?";
echo "<form action=deleterecord.php method=post>";
echo "<input type=hidden name=id value=$id1>"
echo "<input type=hidden name=confirm value=y><input type=submit>";
Don't forget to make it secure from the general public!!!
Before you play back up the table data in case of a coding mistake - I've deleted every record before now!!!!

EDIT - missed a double quote off the query!
Last edited by andym01480 on Thu Sep 07, 2006 10:32 am, edited 4 times in total.
gf05856
Forum Newbie
Posts: 16
Joined: Sat Sep 02, 2006 11:17 am
Location: Belgium

Post by gf05856 »

You could use something like PhpMyEdit

Which does something like :

phpMyEdit generates PHP code for displaying/editing MySQL tables in HTML. All you need to do is to write a simple calling program (a utility to do this is included). It includes a huge set of table manipulation functions (record addition, change, view, copy, and removal), table sorting, filtering, table lookups, and more.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

hmm very generally speaking

HTML Form

Code: Select all

<form action="delete_record.php" method="post">
<input type="text" name="record_to_delete" />
<input type="submit" value="Delete Record" />
</form>
delete_record.php

Code: Select all

<?php
if(!empty($_POST['record_to_delete']))
{
   mysql_query("DELETE FROM `table` WHERE `id` = '".$_POST['record_to_delete']."'") or die(mysql_error());
}
?>
it's a start :D
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
ecopetition
Forum Newbie
Posts: 3
Joined: Wed Sep 06, 2006 3:53 pm

Post by ecopetition »

Thanks for your help :D
Post Reply