Jump to content

Expert eye need to to spot mistakes!! - PLEASE


N30Cr0n

Recommended Posts

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 [b]UPDATE[/b] syntax to, well, update the table! lol

ARGH!

[code]<?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;
}

?>[/code]

thanks guys
Link to comment
Share on other sites

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.)

Link to comment
Share on other sites

[quote author=DocSeuss link=topic=102683.msg407935#msg407935 date=1154509130]
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.)[/quote]

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
Link to comment
Share on other sites

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



[code]
function update($team_id, $team, $played, $points)
[/code]

to

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

Link to comment
Share on other sites

[quote author=BillyBoB link=topic=102683.msg407952#msg407952 date=1154510577]
nope i dont think so

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

I thought REQUEST was both GET & POST methods?
Link to comment
Share on other sites

[quote author=sw0o0sh link=topic=102683.msg407950#msg407950 date=1154510415]
I think the guys saying, change the part that says..



[code]
function update($team_id, $team, $played, $points)
[/code]

to

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


[/quote]

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

thanks you guys, for all your input, keep it coming
Link to comment
Share on other sites

$_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.
Link to comment
Share on other sites

[quote author=DocSeuss link=topic=102683.msg407963#msg407963 date=1154511592]
$_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.

[/quote]

so, let me see if i have this straight;

i would change;

[code]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;

}[/code]

to;

[code]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;

}[/code]

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


Link to comment
Share on other sites

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.
Link to comment
Share on other sites

[quote author=DocSeuss link=topic=102683.msg407971#msg407971 date=1154512507]
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.
[/quote]

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
Link to comment
Share on other sites

Throw this line in after each of your query calls

[code]
print mysql_error() . "<br />";
[/code]
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.
Link to comment
Share on other sites

[quote author=DocSeuss link=topic=102683.msg407979#msg407979 date=1154513177]
Throw this line in after each of your query calls

[code]
print mysql_error() . "<br />";
[/code]
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.
[/quote]

ok, you will have to forgive me on this, but where would i put that little bit of code?
Link to comment
Share on other sites

[quote author=DocSeuss link=topic=102683.msg407992#msg407992 date=1154514492]
on each of the lines after the lines containting a $db-> call  (just do the ones inside your function)
[/quote]

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;

[code]<?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;

}

?>[/code]

thanks guys
Link to comment
Share on other sites

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

[code]<?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;

}

?>[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.