Forum Moderators: coopster

Message Too Old, No Replies

my code isn't generating the correct output

my results don't work!

         

mjar81

7:00 pm on Feb 3, 2004 (gmt 0)

10+ Year Member



ok... so i have a database that has a primary key.

i'm sorting all the results descending from the database and displaying them along with a button to delete the entry from the database.

the button links to remove.php?Id=XXXXXXX
i have the button set so that it displays the unique id munber on the button (for troubleshooting)

the numbers on the buttons are correct according to my database, but when i click on, say, 43, it links to "remove.php?Id=40" instead of "remove.php?Id=43"

can someone please tell me what i might be doing wrong?



//create the table
echo '<TABLE align="center" border="1" cellpadding="2" cellspacing="1" bordercolor="000000" width="80%">';

//connect to the database "localhost" using the supplied
//username and password and assign it the variable $db
$db= mysql_connect('localhost', '******', '******')
or die("Cant connect: " .mysql_error());

//select the database "prayer" from mySQL
mysql_select_db("prayer",$db);
//assign the variable $result to everything from the "prayer_content" table
$query = ("SELECT * FROM prayer_content ORDER BY 'Id' DESC");
$result = mysql_query($query);

//this outputs html to format the table and input some text
echo"<TR><TD><B>Name:</B><TD><B>Request:</B><TD><B>Date Submitted:</B><TD><B>Email:</B><td><b>Delete:</b></tr>";

//while loop assigning $myrow to the $result array
while($myrow = mysql_fetch_array($result))
{
echo "<TR><TD>";
echo $myrow["name"];
echo "<TD>";
echo $myrow["request"];
echo "<td>";
echo $myrow["date"];
echo "<br \>";
echo $myrow["time"];
echo "<TD>";

//if statement to decide whether or not to display the email
if($myrow["displayemail"] == 1)
{
//email display function
//html code
echo "<a href=mailto:";
echo $myrow["email"];
echo ">";
echo $myrow["email"];
echo "</a>";
}
else
{
//make sure you say that the email address is hidden
echo "<center><b>(HIDDEN)</b></center>";
echo "<a href=mailto:";
echo $myrow["email"];
echo ">";
echo $myrow["email"];
echo "</a>";
}
echo "<td>";

//This form creates a button called "Delete" that when clicked will post to the
//remove.php file and tell it what item to delete.
echo '<form name="form1" method="post" action="remove.php?ID='.$myrow["Id"];
echo '"><input name="" type="submit" id="Delete" value="'.$myrow["Id"].'"';
echo '</form>';
}

//close the table
echo "</TABLE>";

thanks!
mjar81

jatar_k

7:50 pm on Feb 3, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld mjar81,

have you looked through to see if it starts listing the wrong id after a certain record or some other recognizable deviation?

here's also a bit of shorthand for your if statement to decide whether or not to display the email

if($myrow["displayemail"] == 1) echo "<center><b>(HIDDEN)</b></center>";
echo "<a href=mailto:",$myrow["email"],">",$myrow["email"],"</a><td>";

since both the if and else display the same link but one has an extra bit to show hidden you can shorten it up. You can also use a comma in your echo statement to list various items.

That will make your code a lot shorter. :)

The first line in your while loop could also be like so

echo "<TR><TD>",$myrow["name"],"<TD>",$myrow["request"],"<td>",$myrow["date"],"<br \>",$myrow["time"],"<TD>";

mjar81

8:29 pm on Feb 3, 2004 (gmt 0)

10+ Year Member



i think i know what my probem is: i'm sorting the database DESCENDING and the Id refernce isn't getting sorted like it should...

i think this might be the problem, but i ahve no clue how to fix it!

can i take the $myrow["Id"] array and reverse the order of it before i output the reaults?

ok... here's what's happening...

i have a list

1
2
3
4
5
6

when i click 1, it deletes 1
i have:

2
3
4
5
6

i click 6 and it erases 2
i have:

3
4
5
6

i click 3 and it deletes 3
i have:

4
5
6

here's the kicker: if i click 5, it deletes 6!

what's going on!?

thanks for the help

jatar_k

8:35 pm on Feb 3, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



did you try it with out sorting to see if the sorting is the issue?

mjar81

2:32 am on Feb 10, 2004 (gmt 0)

10+ Year Member



I FIGURED OUT WHAT THE PROBLEM WAS!

i was creating a form in the loop that would make the page go to the first data point entered... my <form> tag would screw things up...

i switched to an image link (that looks like a button) and everythign works great!

here's what i did:


while($myrow = mysql_fetch_array($result))
{

$id = $myrow['Id'];
echo "<TR><TD>";
echo $myrow["name"];
echo "<TD>";
echo $myrow["request"];
echo "<td>";
echo $myrow["date"];
echo "<br \>";
echo $myrow["time"];
echo "<TD>";

//if statement to decide whether or not to display the email
if($myrow["displayemail"] == 0)echo "<center><b>(HIDDEN)</b></center>";
echo "<a href=mailto:".$myrow["email"].">".$myrow["email"]."</a>";

echo "<td>";

//This form creates a button called "Delete" that when clicked will post to the
//remove.php file and tell it what item to delete.
echo '<a href="remove.php?ID=';
echo $myrow["Id"];
echo '"><img src="delete.jpg" border=0></a>';

//This line was for testing purposes... to see if the correct data was
//being deleted from the database

//echo $myrow["Id"];

}

thanks for all your help... now it's on the the next major issue! lol :)

mjar81