...

View Full Version : Having problems with mysql deteting from database using php form



treichhart
01-22-2012, 05:27 AM
Hi Guys
I am having problems with my database deleting data from my php form and here is my code I have:

Alright I was able to get the script to add names but having issues on deleting from database

DELETE FROM `vmail`.`alias` WHERE `alias`.`address` = 'test7@domain.com';

this is the code from delete.php


<?php
$id = $_GET['address'];?>
<div align="center">
<h2>Are you sure?</h2>
<h2><a href="deleted.php?address=<?php echo "$id" ?>">Yes</a> - <a href="index.php">No</a></h2>
</div>


then this is deleted.php code


<a href="index.php">Back to List</a><br>
<br>
<?php
include("connect.php");

$id = $_GET['address'];

$delete = "DELETE FROM alias WHERE id = '$id' ";
mysql_query($delete);
mysql_close();

echo "Entry deleted";

?>


but the database of vmail is already selected but its getting table information from alias.

djh101
01-22-2012, 08:19 AM
View your source and make sure that the correct id is being sent. And on a side note, you should NEVER send raw user submitted data to a database. See http://php.net/manual/en/function.mysql-real-escape-string.php.

Old Pedant
01-22-2012, 08:23 AM
Bring the page up in your browser.

Click on the browser's VIEW menu.

Click on the SOURCE or PAGE SOURCE menu item.

Find the HTML code that corresponds to that PHP code.

Look at what the <a href=> shows you .

HINT: It's going to show something like

<a href="deleted.php?address="173">Yes</a>

So now, when you do $_GET["address"], the value you get is "173".
Yes, with the quote marks.

KABLOOEY.

In short, kill the quote marks.

treichhart
01-22-2012, 05:01 PM
I looked and it doesnt show anything what your saying in there. I guess if I have to delete the email address I will do it from inside of phpmyadmin since the coding isnt working bc I used the coding from this website: http://www.turningturnip.co.uk/free-mysql-php-generator/ then I just took it out the id since my database wasnt using id format.



Bring the page up in your browser.

Click on the browser's VIEW menu.

Click on the SOURCE or PAGE SOURCE menu item.

Find the HTML code that corresponds to that PHP code.

Look at what the <a href=> shows you .

HINT: It's going to show something like

<a href="deleted.php?address="173">Yes</a>

So now, when you do $_GET["address"], the value you get is "173".
Yes, with the quote marks.

KABLOOEY.

In short, kill the quote marks.

Old Pedant
01-23-2012, 12:49 AM
I looked and it doesnt show anything what your saying in there.

Okay, so show us what the HTML *does* look like.

Don't make us guess.

And yes, you can do it via phpmyadmin, but man is that a clumsy way to run a website!

treichhart
01-23-2012, 12:53 AM
Alright here is the original code from: http://www.turningturnip.co.uk/free-mysql-php-generator/

delete.php code:



<?php
/// In order to use this script freely
/// you must leave the following copyright
/// information in this file:
/// Copyright 2006 www.turningturnip.co.uk
/// All rights reserved.

$id = $_GET['id'];?>
<div align="center">
<h2>Are you sure?</h2>
<h2><a href="deleted.php?id=<?php echo "$id" ?>">Yes</a> - <a href="index.php">No</a></h2>
</div>

and here is mine delete.php code


<?php
$address = $_GET['address'];?>
<div align="center">
<h2>Are you sure?</h2>
<h2><a href="deleted.php?address=<?php echo "$address" ?>">Yes</a> - <a href="index.php">No</a></h2>
</div>

here is the code deleted.php from http://www.turningturnip.co.uk/free-mysql-php-generator :

<a href="index.php">Back to List</a><br>
<br>
<?php
/// In order to use this script freely
/// you must leave the following copyright
/// information in this file:
/// Copyright 2006 www.turningturnip.co.uk
/// All rights reserved.

include("connect.php");

$id = $_GET['id'];

$delete = "DELETE FROM alias WHERE id = '$id' ";
mysql_query($delete);
mysql_close();

echo "Entry deleted";

?>

and here is mine deleted.php code

<a href="index.php">Back to List</a><br>
<br>
<?php
include("connect.php");

$id = $_GET['address'];

$delete = "DELETE FROM alias WHERE address = '$address' ";
mysql_query($delete);
mysql_close();

echo "Entry deleted";

?>


but my database does not have id in it at all. It only have vmail,alias,address,goto domain.

Old Pedant
01-23-2012, 12:56 AM
As I read your code, what the HTML is currently looking like is probably


<a href="deleted.php?address="test7@domain.com">Yes</a>

And that means your SQL query will end up looking like


DELETE FROM alias WHERE id = '"test7@domain.com"'

So I say again, kill the quote marks.

You could easily find out if I'm correct with just the TINIEST bit of DEBUGGING.



<a href="index.php">Back to List</a><br>
<br>
<?php
include("connect.php");

$address = $_GET['address'];

$delete = "DELETE FROM alias WHERE id = '$id' ";
echo "<hr/>DEBUG SQL: " . $delete . "<hr/>\n";
mysql_query($delete) or die(mysql_error);
mysql_close();

echo "Entry deleted";
?>

Old Pedant
01-23-2012, 12:59 AM
You did *NOT* show use the code I asked for.

I want you to bring the PHP page with the <a> link for deleting up in your browser.

The click on the VIEW menu of the browser.

Then click on the SOURCE or PAGE SOURCE option of that menu.

Then show us what HTML the browser is seeing. I do *NOT* want to see the PHP code again.

Old Pedant
01-23-2012, 01:01 AM
IN any case, your deleted.php code is completely wrong.



<a href="index.php">Back to List</a><br>
<br>
<?php
include("connect.php");

$address = $_GET['address']; // *NOT* $id! You aren't using $id, so why get it???

$delete = "DELETE FROM alias WHERE address = '$address' ";
mysql_query($delete);
mysql_close();

echo "Entry deleted";

?>

treichhart
01-23-2012, 01:12 AM
here you go from the html coding
http://i44.tinypic.com/sn1dhd.jpg
http://i43.tinypic.com/2nsrgl.jpg

so what do I need to do kill the " " or what do I need to do to fix this problem?

Old Pedant
01-23-2012, 01:21 AM
Next time, just copy/paste the HTML code. That image makes it nearly impossible to read the text.

Anyway, clearly you have a bug.

Look at what is showing there:


<a href="deleted.php?id=">Yes</a>

That does not match *AT ALL* the PHP code you showed us, which is this:


<?php
$address = $_GET['address'];?>
<div align="center">
<h2>Are you sure?</h2>
<h2><a href="deleted.php?address=<?php echo "$address" ?>">Yes</a> - <a href="index.php">No</a></h2>
</div>


Why does the HTML say id= when you claim the PHP code has address=?

I think that you are still running the original code rather that your modified version. So it's never going to work until you get the right version of the code running.

treichhart
01-23-2012, 01:26 AM
its probably in my index.php in this part of the code:

echo "<a href=\"update.php?id=$id\">Update</a> <a href=\"delete.php?id=$id\">Delete</a>";

treichhart
01-23-2012, 01:27 AM
There I corrected it and that worked and thanks for your help.

I switched it to: echo "<a href=\"update.php?id=$id\">Update</a> <a href=\"delete.php?address=$address\">Delete</a>";



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum