Jump to content


PHP Conversion Problem


4 replies to this topic

#1 Mac

    Young Padawan

  • Members
  • Pip
  • 12 posts

Posted 22 March 2006 - 09:34 PM

In the past, you guys have been extremely helpful. I've really learned a lot here, but I'm afraid I have one last question about PHP/MySQL. I've been using a script to edit MySQL databases via an admin panel for about 2 years now. It's so old that I don't know where exactly the script came from.

Recently, I've switched to a new website host and now the script doesn't work.

Before, I was using PHP 4.4.1 and MySQL 4.0.25, and now I'm using PHP 5.0.4 and MySQL 4.1.14. The script itself, and the information in the databases are identicle, so I've come to believe that this is a compatability problem with the different versions.

Here's the script, for anyone that follows me so far:

<?php
mysql_connect("localhost","#####","******"); 
mysql_select_db("DB"); 
if(!isset($cmd)) { 
$result = mysql_query("select * from TABLE order by id desc limit 5"); 
while($r=mysql_fetch_array($result)) { 
$week=$r["week"];
$id=$r["id"];
echo "<br /><center><div style='font-size: 10px;'><a href='TABLE-edit.php?cmd=edit&id=$id'>TABLE #$week</a></div></center><br /><hr />";
} } ?>
<?
if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit") {
if (!isset($_POST["submit"])) {
$id = $_GET["id"];
$sql = "SELECT * FROM TABLE WHERE id=$id";
$result = mysql_query($sql);		
$myrow = mysql_fetch_array($result);
?>
<form action="TABLE-edit.php" method="post">
<input type="hidden" name="id" value="<?php echo $myrow["id"] ?>" />
<input type="hidden" name="cmd" value="edit" />
<table width="98%" cellspacing="4" cellpadding="3" border="0" align="center">
<tr><td width="100%"><div style="font-size: 10px; font-weight: bold; color: #C3AB97;">Winner's Name:</div></td></tr>
<tr><td width="100%"><input name="winner" type="text" style="width: 220px; font-weight: bold;" value="<?php echo $myrow["winner"] ?>" /></td></tr>
<tr><td width="100%"><div style="font-size: 10px; font-weight: bold; color: #C3AB97;">TABLE #:</div></td></tr>
<tr><td width="100%"><input name="week" type="text" style="width: 220px; font-weight: bold;" value="<?php echo $myrow["week"] ?>" /></td></tr>
<tr><td width="100%"><div style="font-size: 10px; font-weight: bold; color: #C3AB97;">Date:</div></td></tr>
<tr><td width="100%"><input name="date" type="text" style="width: 220px; font-weight: bold;" value="<?php echo $myrow["date"] ?>" /></td></tr>
<tr><td width="100%"><div style="font-size: 10px; font-weight: bold; color: #C3AB97;">Cropped (279x92 recommended):</div></td></tr>
<tr><td width="100%"><input name="cropped" type="text" style="width: 220px; font-weight: bold;" value="<?php echo $myrow["cropped"] ?>" /></td></tr>
<tr><td width="100%"><div style="font-size: 10px; font-weight: bold; color: #C3AB97;">Full Sized :</div></td></tr>
<tr><td width="100%"><input name="full" type="text" style="width: 220px; font-weight: bold;" value="<?php echo $myrow["full"] ?>" /></td></tr>
<tr><td width="100%"><div style="font-size: 10px; font-weight: bold; color: #C3AB97;">Be sure to upload the Cropped and Full-Sized Sig. Due to the nature of MySQL databases, images
can't be stored in the same way text can. You'll have to FTP the sigs.</div></td></tr>
<tr>
<td width="100%" align="center"><input type="submit" value="submit" name="submit" style="text-transform: uppercase; font-weight: bold;"/></td>
</tr>
</table>
</form>
<? } ?>
<?
if ($_POST["$submit"]) {
$winner = $_POST['winner'];
$week = $_POST['week'];
$date = $_POST['date'];
$cropped = $_POST['cropped'];
$full = $_POST['full'];
$sql = "UPDATE TABLE SET winner='$winner',week='$week', date='$date', cropped='$cropped', full='$full' WHERE id=$id";
$result = mysql_query($sql);
echo "
<center>TABLE Updated</div><br /><br /><br /><br />
<a href='index.php'>Click Here To Return To The Admin Section</a></center>
";
}
}
?>

In plain english, this script connects to the database, retrieves the last 5 rows, and echos them out in a link. The link is clicked, and an HTML form with all the information is displayed so the row can be edited. The submit button is hit, the row in the database is updated, and a confirmation page is displayed saying that everything worked the way it was supposed to work.

Now here's the problem. On the new server, the links are echoed out just fine, but when one of them is clicked, they don't dissappear. The links stay at the top of the page, and the HTML form comes on the bottom. And when the submit button is hit, the script goes back to the links echoed out instead of updating the database. The confirmation page never displays.

I'm sure there is a very specific problem here, but I'm not sure what it is. Is PHP known to do this from time to time? Would the new versions of PHP and MySQL conflict with the older version's coding?

Any and all help would be greately appreciated. Thank you all for your time.

#2 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 22 March 2006 - 11:08 PM

PHP is known to do this, but not sporadically, the cause is the code. From the looks of things, the version change is so slight that it would have to be a minor problem that caused this (very minor, but i don't know that much about php 5, so my guess would be as good as the next guys).

Unforunately, the code is a little to sloppy to pin-point something, but at second glance, my guess would be you have one to many variables in there.
if ($_POST["$submit"]) {
I'm not sure who i mentioned this to, but i know i've told somebody about this before (you can check your previous posts to see if it was you, although, it doesn't really matter). Basically, just remove the $ before submit and you should be good (unless there's something else i'm missing).

Edited by rc69, 22 March 2006 - 11:09 PM.


#3 Mac

    Young Padawan

  • Members
  • Pip
  • 12 posts

Posted 23 March 2006 - 08:33 PM

Thank you sir!

You just solved half the problem in one blow. The database updates itself now, and the script is functional again. It's not entirely fixed though.

The rows are echoed out, but they don't go away when a link clicked. This narrows the problem down to about 15 lines of code. I've been playing around with this for a little a while, and I have a hunch that there's something wrong with the $cmd varriable.

Could I trouble you sir, to look at a section of code one more time? I looked it over for more double variables like the $submit, but I couldn't find anything similar to that.

<?php
mysql_connect("localhost","#####","*****"); 
mysql_select_db("DATABASE"); 
if(!isset($cmd)) { 
$result = mysql_query("select * from news order by id desc limit 5"); 
while($r=mysql_fetch_array($result)) {
$id=$r["id"];
$title=$r["title"];
echo "<br /><center><div style='font-size: 10px;'><a href='news-edit.php?cmd=edit&id=$id'>$title</a></div></center><br /><hr />";
} } ?>
<?
if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit") {
if (!isset($_POST["submit"])) {
$id = $_GET["id"];
$sql = "select * from news where id=$id";
$result = mysql_query($sql);		
$myrow = mysql_fetch_array($result);


Again, thank you sir. I can actually use these scripts for the time being. Before your help, they didn't work at all.

#4 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 23 March 2006 - 11:03 PM

lol, this problem is even more obvious. Nothing against you, it's just something that i had a problem with about a year ago, and something i should've noticed earlier (and probably would've missed again had i not looked about 8 lines down :))
if(!isset($cmd)) {
http://php.net/register_globals
if(!isset($_GET['cmd']){
If that doesn't work, then your server has more issues than i can possibly begin to imagine (or i missing something else ;)).

#5 Mac

    Young Padawan

  • Members
  • Pip
  • 12 posts

Posted 24 March 2006 - 12:28 AM

Thank you! Even more progress! That moved the error up to the final confirmation page. Every time you post it gets a little bit better! I won't trouble you by posting the source code again - you've done more than enough to help me - but I have one final question for you.

if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit") {

Is there something wrong with this section of code? That's the last thing I can think of that might be wrong with this script. If that's not it... I give up! ;) I'm trying to salvage a 2 year old script that was written poorly to begin with. If this does't solve it, it'll probably be easier to get a fresh start.

Thanks again for your time! Even if this script is a lost cause, I just learned about register_globals. Thanks for the mini-lesson.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users