Jump to content


Photo

Expert eye need to to spot mistakes!! - PLEASE


  • Please log in to reply
16 replies to this topic

#1 N30Cr0n

N30Cr0n
  • Members
  • PipPip
  • Member
  • 21 posts
  • LocationUK

Posted 02 August 2006 - 07:23 AM

hi, can somebody please have look over this code and tell me where i am goin wrong?

i am trying to pull data from an sql table, display it on a php page, using text area, so i can alter the data and them update the table all in one simple page.

i can seem to get the UPDATE syntax to, well, update the table! lol

ARGH!

<?php

if (!eregi("modules.php", $_SERVER['SCRIPT_NAME'])) {
    die ("You can't access this file directly...");
}

require_once("mainfile.php");
-$module_name = basename(dirname(__FILE__));
get_lang($module_name);
$pagetitle = "- Premier League Table Admin";

/**********************************/
/* Configuration                  */
/*                                */
/* $index = 0; (right side off)   */
/**********************************/
$index = 1;
$subject = "$sitename Premier League Table Admin";
/**********************************/

include("header.php");
global $prefix, $db;

$premadmin = $db->sql_query("SELECT team_id, team, shortname, played, points FROM wcs_premtable ORDER BY points DESC");

if ($db->sql_numrows($premadmin) == 0) {
   include("header.php");
   include("footer.php");
}

if ($db->sql_numrows($premadmin) > 0) {
    $r_options = "";
    if (isset($cookie[4])) { $r_options .= "&amp;mode=$cookie[4]"; }
    if (isset($cookie[5])) { $r_options .= "&amp;order=$cookie[5]"; }
    if (isset($cookie[6])) { $r_options .= "&amp;thold=$cookie[6]"; }
	
OpenTable();
	
	echo "<table class=forumline cellpadding=\"3\" border=\"0\" width='100%'>"
    ."<form name=\"PremAdmin\" action=\"modules.php?name=$module_name\" method=\"post\">";

	echo "<center><table border=\"0\" width=\"210\" align=\"center\" cellpadding=\"1\">"
    	. "<tr><td align=\"center\" width=\"100\" bgcolor=\"#6a6eff\"><b>Team</b></td><td align=\"center\" width=\"5\"> </td><td align=\"center\" width=\"50\" bgcolor=\"#6a6eff\"><b>Played</b></td><td align=\"center\" width=\"5\"> </td><td align=\"center\" width=\"50\" bgcolor=\"#6a6eff\"><b>Points</b></td></tr>";
    
	 	while ($row = $db->sql_fetchrow($premadmin)) {
			$team_id = stripslashes($row['team_id']);
	 	 	$team = stripslashes($row['team']);
	 	 	$played = stripslashes($row['played']);
	 	 	$points = stripslashes($row['points']);

		echo "<tr><td align=\"center\" width=\"100\" bgcolor=\"#d6d7ff\">$team</a></td><td align=\"center\" width=\"5\"> </td><td><input type=\"text\" name=\"played\" value=\"$played\" size=\"6\" maxlength=\"5\"></td><td align=\"center\" width=\"5\"> </td><td><input type=\"text\" name=\"points\" value=\"$points\" size=\"6\" maxlength=\"5\"></td></tr>";
        		
			}
		
		echo "</center></table>";
		
	echo "<br>";

	echo "<input type=\"hidden\" name=\"team_id\" value=\"$team_id\">";
	echo "<input type=\"hidden\" name=\"team\" value=\"$team\">";
	echo "<input type=\"hidden\" name=\"played\" value=\"$played\">";
	echo "<input type=\"hidden\" name=\"points\" value=\"$points\">";
	echo "<input type=\"hidden\" name=\"op\" value=\"update\">";
	echo "<center><input class=button type=\"submit\" value=\"Update Table\"></center>";
	
	}
	
	echo "</form>";
    
CloseTable();

include("footer.php");

function update($team_id, $team, $played, $points) {
	global $user_prefix, $db, $module_name;
	
    $sql = "SELECT * FROM wcs_premtable LIMIT 0,30";
    $result = $db->sql_query($sql);
    $row = $db->sql_fetchrow($result);
	$db->sql_query("LOCK TABLES wcs_premtable WRITE");
	$db->sql_query("UPDATE wcs_premtable SET played = '$played', points = '$points' WHERE team_id = '$team_id'");
    }

   Header("Location: modules.php?name=$module_name");

switch($op) {

    case "update":
	update($team_id, $team, $played, $points);
	break;
}

?>

thanks guys
..:: [SG]N30Cr0n ::..
-= Co-Founder =-

Smokin Gunz [SG] Evolution

#2 DocSeuss

DocSeuss
  • Members
  • PipPipPip
  • Advanced Member
  • 73 posts
  • LocationTexas, USA

Posted 02 August 2006 - 08:58 AM

It appears you are tyring to access your form variables old school style (global vars on), try accessing them via $_REQUEST['team'] or $_POST['team'].
To call your function
update($_REQUEST['team_id'], $_REQUEST['team']......ect.)


"Unlimited budget? Great, for $5,000,0000 we will run your ad banner for one year"  ----Rasmus Lerdorf

#3 N30Cr0n

N30Cr0n
  • Members
  • PipPip
  • Member
  • 21 posts
  • LocationUK

Posted 02 August 2006 - 09:05 AM

It appears you are tyring to access your form variables old school style (global vars on), try accessing them via $_REQUEST['team'] or $_POST['team'].
To call your function
update($_REQUEST['team_id'], $_REQUEST['team']......ect.)


sorry DocSeuss, i am a n00b when it comes to understanding what needs to be done, haven't been working with PHP/mySQL very long. Could you possible go into this further?

Thanks a million
..:: [SG]N30Cr0n ::..
-= Co-Founder =-

Smokin Gunz [SG] Evolution

#4 sw0o0sh

sw0o0sh
  • Members
  • PipPipPip
  • Advanced Member
  • 151 posts
  • LocationMassachusetts

Posted 02 August 2006 - 09:20 AM

I think the guys saying, change the part that says..



function update($team_id, $team, $played, $points)

to

function update($_REQUEST["team_id"], $_REQUEST["team"], $_REQUEST["played"], $_REQUEST["points"])



#5 BillyBoB

BillyBoB
  • Members
  • PipPipPip
  • Advanced Member
  • 630 posts

Posted 02 August 2006 - 09:22 AM

nope i dont think so

theres a difference in what you get when your info is posted via POST or REQUEST

#6 sw0o0sh

sw0o0sh
  • Members
  • PipPipPip
  • Advanced Member
  • 151 posts
  • LocationMassachusetts

Posted 02 August 2006 - 09:24 AM

nope i dont think so

theres a difference in what you get when your info is posted via POST or REQUEST


I thought REQUEST was both GET & POST methods?

#7 BillyBoB

BillyBoB
  • Members
  • PipPipPip
  • Advanced Member
  • 630 posts

Posted 02 August 2006 - 09:30 AM

hmm i dont really know i havnt used REQUEST srry

#8 N30Cr0n

N30Cr0n
  • Members
  • PipPip
  • Member
  • 21 posts
  • LocationUK

Posted 02 August 2006 - 09:35 AM

I think the guys saying, change the part that says..



function update($team_id, $team, $played, $points)

to

function update($_REQUEST["team_id"], $_REQUEST["team"], $_REQUEST["played"], $_REQUEST["points"])



nope, tried that, and it buggered the page up completly, wouldn't load.

thanks you guys, for all your input, keep it coming
..:: [SG]N30Cr0n ::..
-= Co-Founder =-

Smokin Gunz [SG] Evolution

#9 DocSeuss

DocSeuss
  • Members
  • PipPipPip
  • Advanced Member
  • 73 posts
  • LocationTexas, USA

Posted 02 August 2006 - 09:39 AM

$_REQUEST is the superset of $_GET, $_POST, and $_COOKIE

to expand on my answer when variables are send via a form you can't access them directly by just using $varname

your have to use $_POST(assuming the method of your form was post) or you can use $_REQUEST which has access to all $_POST vars.

ahh you posted while I was typing, just want to verify you would make those changes on the line the CALLS the function not in the function definition you can keep those the same.


$_REQUEST['varname'] will yield the same result as $_POST['varname']

Now I'm sure there is an argument somewhere on all sides of the debate to say you should use one or the other but when it really comes down to it, it is just preference.

"Unlimited budget? Great, for $5,000,0000 we will run your ad banner for one year"  ----Rasmus Lerdorf

#10 N30Cr0n

N30Cr0n
  • Members
  • PipPip
  • Member
  • 21 posts
  • LocationUK

Posted 02 August 2006 - 09:48 AM

$_REQUEST is the superset of $_GET, $_POST, and $_COOKIE

to expand on my answer when variables are send via a form you can't access them directly by just using $varname

your have to use $_POST(assuming the method of your form was post) or you can use $_REQUEST which has access to all $_POST vars.

ahh you posted while I was typing, just want to verify you would make those changes on the line the CALLS the function not in the function definition you can keep those the same.


$_REQUEST['varname'] will yield the same result as $_POST['varname']

Now I'm sure there is an argument somewhere on all sides of the debate to say you should use one or the other but when it really comes down to it, it is just preference.


so, let me see if i have this straight;

i would change;

function update($team_id, $team, $played, $points) {
	global $user_prefix, $db, $module_name;
	
    $sql = "SELECT * FROM wcs_premtable LIMIT 0,30";
    $result = $db->sql_query($sql);
    $row = $db->sql_fetchrow($result);
	$db->sql_query("LOCK TABLES wcs_premtable WRITE");
	$db->sql_query("UPDATE wcs_premtable SET played = '$played', points = '$points' WHERE team_id = '$team_id'");
    }

   Header("Location: modules.php?name=$module_name");

switch($op) {

    case "update":
	update($team_id, $team, $played, $points);
	break;

}

to;

function update($team_id, $team, $played, $points) {
	global $user_prefix, $db, $module_name;
	
    $sql = "SELECT * FROM wcs_premtable LIMIT 0,30";
    $result = $db->sql_query($sql);
    $row = $db->sql_fetchrow($result);
	$db->sql_query("LOCK TABLES wcs_premtable WRITE");
	$db->sql_query("UPDATE wcs_premtable SET played = '$played', points = '$points' WHERE team_id = '$team_id'");
    }

   Header("Location: modules.php?name=$module_name");

switch($op) {

    case "update":
	update($_REQUEST['team_id'], $_REQUEST['team'], $_REQUEST['played'], $_REQUEST['points']);
	break;

}

Thanks guys, who ever solves this will deffo get a mention in the comments!! lol



..:: [SG]N30Cr0n ::..
-= Co-Founder =-

Smokin Gunz [SG] Evolution

#11 DocSeuss

DocSeuss
  • Members
  • PipPipPip
  • Advanced Member
  • 73 posts
  • LocationTexas, USA

Posted 02 August 2006 - 09:55 AM

If you have plugged that in and it still doesn't work then you have another problem that needs to be worked out. Unfortunatly if you are paying for a hosted site they may have error reporting turned off or down that you won't see the error you will just get a blank page.
"Unlimited budget? Great, for $5,000,0000 we will run your ad banner for one year"  ----Rasmus Lerdorf

#12 N30Cr0n

N30Cr0n
  • Members
  • PipPip
  • Member
  • 21 posts
  • LocationUK

Posted 02 August 2006 - 09:59 AM

If you have plugged that in and it still doesn't work then you have another problem that needs to be worked out. Unfortunatly if you are paying for a hosted site they may have error reporting turned off or down that you won't see the error you will just get a blank page.


well the page is still displaying, but the data isn't getting written back to the table within the database. after i press my update button, the page refreshes and the change i made haven't been stored.

but in answer to the other thing about error reporting, yeah i just get a blank page when i f**k up the code! no error or nothing, which is so helpful! lol
..:: [SG]N30Cr0n ::..
-= Co-Founder =-

Smokin Gunz [SG] Evolution

#13 DocSeuss

DocSeuss
  • Members
  • PipPipPip
  • Advanced Member
  • 73 posts
  • LocationTexas, USA

Posted 02 August 2006 - 10:06 AM

Throw this line in after each of your query calls

print mysql_error() . "<br />";
and see if you get any results

I just put the break in there for troubleshooting in production code you wouldn't wan't anything to print out including the newline if mysql_error() didn't print an error.
"Unlimited budget? Great, for $5,000,0000 we will run your ad banner for one year"  ----Rasmus Lerdorf

#14 N30Cr0n

N30Cr0n
  • Members
  • PipPip
  • Member
  • 21 posts
  • LocationUK

Posted 02 August 2006 - 10:17 AM

Throw this line in after each of your query calls

print mysql_error() . "<br />";
and see if you get any results

I just put the break in there for troubleshooting in production code you wouldn't wan't anything to print out including the newline if mysql_error() didn't print an error.


ok, you will have to forgive me on this, but where would i put that little bit of code?
..:: [SG]N30Cr0n ::..
-= Co-Founder =-

Smokin Gunz [SG] Evolution

#15 DocSeuss

DocSeuss
  • Members
  • PipPipPip
  • Advanced Member
  • 73 posts
  • LocationTexas, USA

Posted 02 August 2006 - 10:28 AM

on each of the lines after the lines containting a $db-> call  (just do the ones inside your function)
"Unlimited budget? Great, for $5,000,0000 we will run your ad banner for one year"  ----Rasmus Lerdorf

#16 N30Cr0n

N30Cr0n
  • Members
  • PipPip
  • Member
  • 21 posts
  • LocationUK

Posted 02 August 2006 - 10:37 AM

on each of the lines after the lines containting a $db-> call  (just do the ones inside your function)


ok, have done that, and it made little diference. no error was reported, but the data still aint been saved to the table.

DocSeuss, if you like i can PM you with the link to the page, and you can see what i wanting to do?

here is the latest version of the page code, complete with suggested changes from this topic;

<?php

if (!eregi("modules.php", $_SERVER['SCRIPT_NAME'])) {
    die ("You can't access this file directly...");
}

require_once("mainfile.php");
-$module_name = basename(dirname(__FILE__));
get_lang($module_name);
$pagetitle = "- Premier League Table Admin";

/**********************************/
/* Configuration                  */
/*                                */
/* $index = 0; (right side off)   */
/**********************************/
$index = 1;
$subject = "$sitename Premier League Table Admin";
/**********************************/

include("header.php");
global $prefix, $db;

$premadmin = $db->sql_query("SELECT team_id, team, played, points FROM wcs_premtable ORDER BY points DESC");

if ($db->sql_numrows($premadmin) == 0) {
   include("header.php");
   include("footer.php");
}

if ($db->sql_numrows($premadmin) > 0) {
    $r_options = "";
    if (isset($cookie[4])) { $r_options .= "&amp;mode=$cookie[4]"; }
    if (isset($cookie[5])) { $r_options .= "&amp;order=$cookie[5]"; }
    if (isset($cookie[6])) { $r_options .= "&amp;thold=$cookie[6]"; }
	
OpenTable();
	
	echo "<center><font class=\"title\">Premiership League Table Admin</font><br><br>"
	. "<font class=\"content\"><b>ADMIN ONLY:</b> Here you can alter the number of games played and the points scored for each team, and it will be directly reflected on the Premiership League Table.</font></center><br><br>";

	echo "<table class=forumline cellpadding=\"3\" border=\"0\" width='100%'>"
    ."<form name=\"PremAdmin\" action=\"modules.php?name=$module_name\" method=\"post\">";
	
	echo "<center><table border=\"0\" width=\"210\" align=\"center\" cellpadding=\"1\">"
    	. "<tr><td align=\"center\" width=\"100\" bgcolor=\"#6a6eff\"><b>Team</b></td><td align=\"center\" width=\"5\"> </td><td align=\"center\" width=\"50\" bgcolor=\"#6a6eff\"><b>Played</b></td><td align=\"center\" width=\"5\"> </td><td align=\"center\" width=\"50\" bgcolor=\"#6a6eff\"><b>Points</b></td></tr>";
    
	 	while ($row = $db->sql_fetchrow($premadmin)) {
			$team_id = stripslashes($row['team_id']);
	 	 	$team = stripslashes($row['team']);
	 	 	$played = stripslashes($row['played']);
	 	 	$points = stripslashes($row['points']);

		echo "<tr><td align=\"center\" width=\"100\" bgcolor=\"#d6d7ff\">$team</a></td><td align=\"center\" width=\"5\"> </td><td><input type=\"text\" name=\"played\" value=\"$played\" size=\"6\" maxlength=\"5\"></td><td align=\"center\" width=\"5\"> </td><td><input type=\"text\" name=\"points\" value=\"$points\" size=\"6\" maxlength=\"5\"></td></tr>";
        		
			}
		
		echo "</center></table>";
		
	}
		
	echo "<br>";

	echo "<input type=\"hidden\" name=\"team_id\" value=\"$team_id\">";
	echo "<input type=\"hidden\" name=\"team\" value=\"$team\">";
	echo "<input type=\"hidden\" name=\"played\" value=\"$played\">";
	echo "<input type=\"hidden\" name=\"points\" value=\"$points\">";
	echo "<input type=\"hidden\" name=\"op\" value=\"update\">";
	echo "<center><input class=button type=\"submit\" value=\"Update Table\"></center>";
	
	echo "</form>";
    
CloseTable();

include("footer.php");

function update($team_id, $team, $played, $points) {
	global $user_prefix, $db, $module_name;
	
    $sql = "SELECT * FROM wcs_premtable LIMIT 0,30";
    $result = $db->sql_query($sql);
	print mysql_error() . "<br />";
    $row = $db->sql_fetchrow($result);
	print mysql_error() . "<br />";
	$db->sql_query("LOCK TABLES wcs_premtable WRITE");
	print mysql_error() . "<br />";
	$db->sql_query("UPDATE wcs_premtable SET played = '$played', points = '$points' WHERE team_id = '$team_id'");
	print mysql_error() . "<br />";
    }

   Header("Location: modules.php?name=$module_name");

switch($op) {

    case "update":
	update($_REQUEST['team_id'], $_REQUEST['team'], $_REQUEST['played'], $_REQUEST['points']);
	break;

}

?>

thanks guys
..:: [SG]N30Cr0n ::..
-= Co-Founder =-

Smokin Gunz [SG] Evolution

#17 N30Cr0n

N30Cr0n
  • Members
  • PipPip
  • Member
  • 21 posts
  • LocationUK

Posted 04 August 2006 - 09:53 AM

ok after hours of friggin around, i have come up blank. the page displays, meaning i have no major errors (i think) and i can enter new data into the text area i have set out, but as soon as i click the Update button on the form, the section of code that should write it all back to the datbase, doesn't work, it just refreshes the page, and removes all the data i have jus entered, and resets it back to its original values.

i have changed a few things in the code, to clean it up and everything, and will post the new page code below.

BTW, i am using PHP-Nuke 7.8 CMS as a framework, if that makes any difference.

Thanks

<?php

if (!eregi("modules.php", $_SERVER['SCRIPT_NAME'])) {
    die ("You can't access this file directly...");
}

require_once("mainfile.php");
-$module_name = basename(dirname(__FILE__));
get_lang($module_name);
$pagetitle = "- Premier League Table Admin";

/**********************************/
/* Configuration                  */
/*                                */
/* $index = 0; (right side off)   */
/**********************************/
$index = 1;
$subject = "$sitename Premier League Table Admin";
/**********************************/

include("header.php");
global $prefix, $db, $premadmin;

$premadmin = $db->sql_query("SELECT team_id, team, played, points FROM wcs_premtable ORDER BY points DESC");

if ($db->sql_numrows($premadmin) == 0) {
   include("header.php");
   include("footer.php");
}

if ($db->sql_numrows($premadmin) > 0) {
    $r_options = "";
    if (isset($cookie[4])) { $r_options .= "&amp;mode=$cookie[4]"; }
    if (isset($cookie[5])) { $r_options .= "&amp;order=$cookie[5]"; }
    if (isset($cookie[6])) { $r_options .= "&amp;thold=$cookie[6]"; }
	
OpenTable();
	
	echo "<center><font class=\"title\">Premiership League Table Admin</font><br><br>"
	. "<font class=\"content\"><b>ADMIN ONLY:</b> Here you can alter the number of games played and the points scored for each team, and it will be directly reflected on the Premiership League Table.</font></center><br><br>";

	echo "<table cellpadding=\"1\" cellspacing=\"0\" border=\"0\" width='100%'>"
    ."<form name=\"PremAdmin\" action=\"modules.php?name=$module_name\" method=\"post\">";
	
	echo "<center><table border=\"0\" width=\"210\" align=\"center\" cellpadding=\"1\">"
    	. "<tr><td align=\"center\" width=\"100\" bgcolor=\"#6a6eff\"><b>Team</b></td><td align=\"center\" width=\"5\"> </td><td align=\"center\" width=\"50\" bgcolor=\"#6a6eff\"><b>Played</b></td><td align=\"center\" width=\"5\"> </td><td align=\"center\" width=\"50\" bgcolor=\"#6a6eff\"><b>Points</b></td></tr>";
    
	 	while ($row = $db->sql_fetchrow($premadmin)) {
			$team_id = stripslashes($row['team_id']);
	 	 	$team = stripslashes($row['team']);
	 	 	$played = stripslashes($row['played']);
	 	 	$points = stripslashes($row['points']);

		echo "<tr><td align=\"center\" width=\"100\" bgcolor=\"#d6d7ff\">$team</a></td><td align=\"center\" width=\"5\"> </td><td><input type=\"text\" name=\"tot_plyd\" value=\"$played\" size=\"6\" maxlength=\"5\"></td><td align=\"center\" width=\"5\"> </td><td><input type=\"text\" name=\"tot_pnts\" value=\"$points\" size=\"6\" maxlength=\"5\"></td></tr>";
        		
			}
		
	}
		
	echo "<tr><td colspan=\"5\" align=\"center\">";
	echo "<input type=\"hidden\" name=\"team_id\" value=\"$premadmin[team_id]\">";
	echo "<input type=\"hidden\" name=\"team\" value=\"$premadmin[team]\">";
	echo "<input type=\"hidden\" name=\"op\" value=\"premup\">";
	echo "<br><br><center><input class=button type=\"submit\" value=\"Update League\"></center>";
	
	echo "</form></td></tr></table></center>";
    
CloseTable();

include("footer.php");

function premup($team_id, $team, $played, $points) {
	global $user_prefix, $db, $module_name, $premadmin;

	$db->sql_query("LOCK TABLES wcs_premtable WRITE");
	print mysql_error() . "<br />";
	$db->sql_query("UPDATE wcs_premtable SET played = '".$_POST["tot_plyd"]."', points = '".$_POST["tot_pnts"]."' WHERE team_id = '".$_POST["team_id"]."'");
	print mysql_error() . "<br />";
	$db->sql_query("UNLOCK TABLES");
	print mysql_error() . "<br />";

   Header("Location: modules.php?name=$module_name");

}

switch($op) {

    case "premup":
	premup($team_id, $team, $played, $points);
	break;

}

?>

..:: [SG]N30Cr0n ::..
-= Co-Founder =-

Smokin Gunz [SG] Evolution




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users