Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

deleting records from database 1

Status
Not open for further replies.

estrellas

Programmer
Jul 16, 2004
48
US
here's the problem .. i'm trying to have the user select a network, which accesses a table in my database, then from there there the user is given an option to delete that record.

it goes through three pages (see code below) to finally the 3rd page - where i get two errors ...

delete.html
______________
Select the NETWORK to DELETE the record from<br>
<b>REMEMBER THAT ONCE YOU PRESS "DELETE" THE ACTION CANNOT BE REVERSED! BE SMART.</b><br>
<form action="delete.php" method="post" name="coolThing">
<select name="network" >
<option value="ak">Alaska</option>
<option value="c">California</option>
<option value="h">Hawaii</option>
<option value="j">Japan</option>
<option value="k">Korea</option>
<option value="a">Australia</option>
<option value="f">France</option>
<option value="g">Germany</option>
<option value="u">Ukraine</option>
</select>
<input type="Submit" name="submit" value="Enter Network">
</form>



the first page works fine (basic html redirecting to a php page) ...


delete.php
-------------------
<?php
//create a var name for network to search by
$network=$HTTP_POST_VARS['network'];
$network=addslashes($network);

//connect to the database
$db = mysql_connect(###,###,###);

//error statement if database cannot be conencted to
if (!$db) { echo 'Error: Could not connect to the database. Please try again later.'; exit; }

//select the database that we are working withi
mysql_select_db("myDb",$db);

//display all the records in that table so the user can select which one to delete
$query = "SELECT * FROM $network";

//put the query into a result form and tell it which database to look in
$result = mysql_query($query,$db);

//this line is for troubleshooting - it prints out the query that you submitted to the screen
//print "<br>" . $query . "<br>";

//put data in a table for formatting reasons
echo "<p><b>Results for the *** $network *** network</b></p>";
echo "<table><font size=2>";
echo "<tr><td>***</td><td>***</td><td>***</td><td>***</td><td>***</td><td>***</td><td>***</td><td>***</td><td>***</td><td>***</td><td>***</td><td>***</td></font></tr>\n";

//loop through the table and print all of the information
while ($myrow = mysql_fetch_array($result)){
printf("<font size=2><tr><td>$myrow[0]</td><td>$myrow[1]\t</td><td>$myrow[2]</td><td>$myrow[3]</td><td>$myrow[4]</td><td>$myrow[5]</td><td>$myrow[6]</td><td>$myrow[7]</td><td>$myrow[8]</td><td>$myrow[9]</td><td>$myrow[10]</td><td>$myrow[11]</td><td>$myrow[12]</td><td><a href=\"%s?name=%s&delete=yes\"><form action=\"delete_confirm.php\" method=\"post\"<input type=\"submit\" value=\"Delete\"></form></a><br></td></tr></font>\n",$PHP_SELF, $myrow["0"], $myrow["1"], $myrow["2"], $myrow["3"], $myrow["4"], $myrow["5"], $myrow["6"], $myrow["7"], $myrow["8"],$myrow["9"], $myrow["10"],$myrow["11"]);
}
echo "</font></table>";

finally i send it to the last page...

delete_confirm.php
--------------
<?php
$query = "DELETE FROM $network WHERE name=$name";
$result = mysql_query($query,db);
echo "<p><b>Results for the *** $network *** network deleted successfully</b></p>";
echo "<table><font size=2>";
echo "<tr><td>***</td><td>***</td><td>***</td><td>***</td><td>Type</td><td>***</td><td>***</td><td>***</td><td>***</td><td>***</td><td>***</td><td>***</td></font></tr>\n";
//loop through the table and print all of the information
while ($myrow = mysql_fetch_array($result)) {
printf("<font size=2><tr><td>$myrow[0]</td><td>$myrow[1]\t</td><td>$myrow[2]</td><td>$myrow[3]</td><td>$myrow[4]</td><td>$myrow[5]</td><td>$myrow[6]</td><td>$myrow[7]</td><td>$myrow[8]</td><td>$myrow[9]</td><td>$myrow[10]</td><td>$myrow[11]</td><td>$myrow[12]</td><td><a href=\"%s?name=%s&delete=yes\">(DELETE)</a><br></td></tr></font>\n",$PHP_SELF, $myrow["0"], $myrow["1"], $myrow["2"], $myrow["3"], $myrow["4"], $myrow["5"], $myrow["6"], $myrow["7"], $myrow["8"],$myrow["9"], $myrow["10"],$myrow["11"]);
}
echo "</font></table>";
?>

when i do this i get the following errors...

Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource

and

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource

(I've edited out the fields in here and stuff like that, but my first field is "name")

ANy help on fixing this error as well as combining all three pages into one (if it is possible) would be awesome!

Thank you so much in advance :)
 
First thing I noticed is on your third page--

$result = mysql_query($query,db);
should be
$result = mysql_query($query,$db);

Let us know what you get once you fix that.

*cLFlaVA
----------------------------
A polar bear walks into a bar and says, "Can I have a ... beer?"
The bartender asks, "What's with the big pause?
 
I changed
result = mysql_query($query,db);
to
$result = mysql_query($query,$db);

and i still get the same errors ...
could it be something with global variables? i know how to declare/troubleshoot them in c/c++ but am not quite sure if that is a problem in php.

thanks so much for the help :) i really appreciate it :)

If programming were as fun as cycling, then we'd need more bike mechanics.
 
A) You should have single quotes around the $name variable in your SQL statement.

DELETE FROM $network WHERE name='$name'

B) Your $db variable is not a valid resource link. Where are you setting it? How are you setting it?

C) You should try some error reporting to test better. In other words, echo the SQL statement right after you create it. Test for a result before you perform actions with it.

This would help:
Code:
$query = "DELETE FROM $network WHERE name='$name'";    
echo $query, "<br>";

$result = mysql_query($query,$db);
if (!$result) {
    echo mysql_error();
} else {
    //continue processing...
}

*cLFlaVA
----------------------------
A polar bear walks into a bar and says, "Can I have a ... beer?"
The bartender asks, "What's with the big pause?
 
okay i did that and i think i know a problem but i'm not sure how to fix it...
it's like delete_confirm.php is not getting the variables from delete.php. because here's the errors i'm getting...

Code:
DELETE FROM '' WHERE name=''
You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near ''' WHERE name=''' at line 1
(the first line is the printout of echo $query;

Code:
$query = "DELETE FROM $network WHERE name='$name'";	
	echo $query, "<br>";
	$result = mysql_query($query,$db);
	if (!$result) {
		echo mysql_error();
	}
	else {
             //continue code
        }
how do i get delete_confirm.php to read the variables from delete.php?

If programming were as fun as cycling, then we'd need more bike mechanics.
 
A) In delete.php, change the entire while loop to this:
Code:
while ($myrow = mysql_fetch_array($result)){
echo "
<tr>
<td>$myrow[0]</td>
<td>$myrow[1]</td>
<td>$myrow[2]</td>
<td>$myrow[3]</td>
<td>$myrow[4]</td>
<td>$myrow[5]</td>
<td>$myrow[6]</td>
<td>$myrow[7]</td>
<td>$myrow[8]</td>
<td>$myrow[9]</td>
<td>$myrow[10]</td>
<td>$myrow[11]</td>
<td>$myrow[12]</td>
<td>
<form action=\"delete_confirm.php\" method=\"post\">
<input type=\"hidden\" name=\"network\" value=\"$myrow[network col number]\">
<input type=\"hidden\" name=\"name\" value=\"$myrow[name col number]\">
<input type=\"submit\" value=\"Delete\">
</form><br>
</td>
</tr>";
}

Then, in your delete_confirm.php,

// get posted variables
$network = $_POST['network'];
$name = $_POST['name'];

$query = "DELETE FROM $network WHERE name='$name'";
echo $query, "<br>";
$result = mysql_query($query,$db);
...

*cLFlaVA
----------------------------
A polar bear walks into a bar and says, "Can I have a ... beer?"
The bartender asks, "What's with the big pause?
 
whoo hoo! that worked to submit the values of $name and $network into delete_confirm.php. thank you so much!!!

the only problem left is ...
Code:
Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource

coming from the line ...
Code:
$result = mysql_query($query,$db);

the whole snippet is the same as above except with the $_POST vars added in the top as you mentioned in your last post ...

If programming were as fun as cycling, then we'd need more bike mechanics.
 
I'm going to go ahead and assume you don't have
Code:
//connect to the database
$db = mysql_connect(###,###,###);
in the delete_confirm.php.

Add that line to any script that utilizes your database.

A more efficient method is to create a global include file (I use something called global.inc or global.php in my includes folder).

This file should have any variables or objects you use on a global scale (ie, a database).

Here, you could have the $db = mysql_connect line of code. Then, in each script where you require the database, you can put
Code:
require_once("includes/global.inc");
as the very top line.

Hope that helps...


*cLFlaVA
----------------------------
A polar bear walks into a bar and says, "Can I have a ... beer?"
The bartender asks, "What's with the big pause?
 
cool beans! that worked to connect and stuff and for some reason i messed things up again (getting the Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource) error in the delete.php file.

Code:
		  <?php
		  $db = mysql_connect("","","");
	
	//error statement if database cannot be conencted to
	if (!$db) { echo 'Error: Could not connect to the database. Please try again later.'; exit; }
	
	//select the database that we are working withi
	mysql_select_db("mhDb",$db);
	//create a var name for network to search by
	$network=$HTTP_POST_VARS['network'];
	$network=addslashes($network);
	global $network;
	
	//display all the records in that table so the user can select which one to delete
	$query = "SELECT * FROM $network";
	
	//put the query into a result form and tell it which database to look in
	$result = mysql_query($query,$db);
	
	//this line is for troubleshooting - it prints out the query that you submitted to the screen
	//print "<br>" . $query . "<br>";
	
	//put data in a table for formatting reasons
	echo "<p><b>Results for the $network network</b></p>";
	echo "<table><font size=2>";
	    echo "<tr><td>***</td><td>***</td><td>***</td><td>***</td><td>***</td><td>***</td><td>***</td><td>***</td><td>***</td><td>***</td><td>***</td><td>***</td></font></tr>\n";
	//loop through the table and print all of the information
	while ($myrow = mysql_fetch_array($result)){
	echo " <font size=2><tr>
			<td>$myrow[0]</td>
			<td>$myrow[1]</td>
			<td>$myrow[2]</td>
			<td>$myrow[3]</td>
			<td>$myrow[4]</td>
			<td>$myrow[5]</td>
			<td>$myrow[6]</td>
			<td>$myrow[7]</td>
			<td>$myrow[8]</td>
			<td>$myrow[9]</td>
			<td>$myrow[10]</td>
			<td>$myrow[11]</td>
			<td>$myrow[12]</td>
			<td>
			<form action=\"delete_confirm.php\" method=\"post\">
			<input type=\"hidden\" name=\"network\" value=\"$myrow[12]\">
			<input type=\"hidden\" name=\"name\" value=\"$myrow[0]\"
			<input type=\"submit\" value=\"Delete\">
			</form>
			<br>
			</td>
			</tr>
			</font>";
		}
			
?>
who knows why really, i'm gonna keep looking at it but thank you so so so so much for helping. i appreciate it a bunch.
cheerio :)

If programming were as fun as cycling, then we'd need more bike mechanics.
 
No problem. That's what we're all here for.

I suggest you continue to debug your code. After you set $result, test for an error and print the error out.

$result = mysql_query($query,$db);
if (!$result) {
echo mysql_error();
} else {
// do stuff...
}

Also, if you're passing mysql_connect variables, make sure they are defined within this particular script (or the global.inc)

*cLFlaVA
----------------------------
A polar bear walks into a bar and says, "Can I have a ... beer?"
The bartender asks, "What's with the big pause?
 
ooo awesome! just putting that line in there fixed it for some odd reason. i'm not really sure why, but it works!

hey thank you so much for your help, i appreciate it sooo soooo much :)

If programming were as fun as cycling, then we'd need more bike mechanics.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top