Jump to content
MakeWebGames

Another Question...


MobTown

Recommended Posts

Probably a simply solution, but recently a friend of mine tried to help code on my site.

Since, then an error arises when trying to access a page.

 

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 '' at line 1

I don't know what this means in the slightest, so wondered if you people could help me out.

My page likes like this.

<?php
session_start();
require "global_func.php";
$userid=$_SESSION['userid'];
require "header.php";
$h = new headers;
$h->startheaders();
include "mysql.php";
global $c;
$is=mysql_query("SELECT u.*,us.* FROM users u LEFT JOIN userstats us ON u.userid=us.userid WHERE u.userid=$userid",$c) or die(mysql_error());
$ir=mysql_fetch_array($is);
check_level();
$fm=money_formatter($ir['money']);
$cm=money_formatter($ir['crystals'],'');
$lv=date('F j, Y, g:i a',$ir['laston']);
$h->userdata($ir,$lv,$fm,$cm);
$h->menuarea();
if (!$_GET['ID'])
{
die ("Get outta here!");
}
if($_GET['action'] == "cancel")
{
die ("You have cancelled your donation. Please donate later...");
}
else if($_GET['action'] == "done")
{
$id=$_GET['ID'];
$type=$_GET['type'];
if($_GET['type']==1)
{
mysql_query("INSERT INTO inventory VALUES ('','34','$id','5')",$c);
mysql_query("INSERT INTO dps_process VALUES ('$id',unix_timestamp(),'$type')",$c);
header("Location: donatorcomplete.php?type=1");
}
else if($_GET['type']==2)
{
mysql_query("INSERT INTO inventory VALUES ('','34','$id','30')",$c);
mysql_query("INSERT INTO dps_process VALUES ('$id',unix_timestamp(),'$type')",$c);
header("Location: donatorcomplete.php?type=2");
}
else if($_GET['type']==3)
{
mysql_query("INSERT INTO inventory VALUES ('','34','$id','60')",$c);
mysql_query("INSERT INTO dps_process VALUES ('$id',unix_timestamp(),'$type')",$c);
header("Location: donatorcomplete.php?type=3");
}
else if($_GET['type']==4)
{
mysql_query("UPDATE users SET crystals=crystals+250 WHERE userid=$id",$c);
mysql_query("INSERT INTO dps_process VALUES ('$id',unix_timestamp(),'$type')",$c);
header("Location: donatorcomplete.php?type=4");
}
else if($_GET['type']==5)
{
mysql_query("UPDATE users SET crystals=crystals+1500 WHERE userid=$id",$c);
mysql_query("INSERT INTO dps_process VALUES ('$id',unix_timestamp(),'$type')",$c);
header("Location: donatorcomplete.php?type=5");
}
else if($_GET['type']==6)
{
mysql_query("UPDATE users SET crystals=crystals+3000 WHERE userid=$id",$c);
mysql_query("INSERT INTO dps_process VALUES ('$id',unix_timestamp(),'$type')",$c);
header("Location: donatorcomplete.php?type=6");
}
else if($_GET['type']==7)
{
mysql_query("INSERT INTO inventory VALUES ('','34','$id','5')",$c);
mysql_query("UPDATE users SET money=money+10000, crystals=crystals+250, donatordays=donatordays+30 WHERE userid=$id",$c);
mysql_query("INSERT INTO dps_process VALUES ('$id',unix_timestamp(),'$type')",$c);
header("Location: donatorcomplete.php?type=7");
}
else if($_GET['type']==8)
{
mysql_query("INSERT INTO inventory VALUES ('','34','$id','30')",$c);
mysql_query("UPDATE users SET money=money+50000 crystals=crystals+1500, donatordays=donatordays+60 WHERE userid=$id",$c);
mysql_query("INSERT INTO dps_process VALUES ('$id',unix_timestamp(),'$type')",$c);
header("Location: donatorcomplete.php?type=8");
}
else if($_GET['type']==9)
{
mysql_query("INSERT INTO inventory VALUES ('','34','$id','60')",$c);
mysql_query("UPDATE users SET money=money+250000, crystals=crystals+3000, donatordays=donatordays+90 WHERE userid=$id",$c);
mysql_query("INSERT INTO dps_process VALUES ('$id',unix_timestamp(),'$type')",$c);
header("Location: donatorcomplete.php?type=9");
}
else if($_GET['type']==14)
{
mysql_query("UPDATE users SET donatordays=donatordays+30 WHERE userid=$id",$c);
mysql_query("INSERT INTO dps_process VALUES ('$id',unix_timestamp(),'$type')",$c);
header("Location: donatorcomplete.php?type=10");
}
else if($_GET['type']==15)
{
mysql_query("UPDATE users SET donatordays=donatordays+60 WHERE userid=$id",$c);
mysql_query("INSERT INTO dps_process VALUES ('$id',unix_timestamp(),'$type')",$c);
header("Location: donatorcomplete.php?type=11");
}
else if($_GET['type']==16)
{
mysql_query("UPDATE users SET donatordays=donatordays+90 WHERE userid=$id",$c);
mysql_query("INSERT INTO dps_process VALUES ('$id',unix_timestamp(),'$type')",$c);
header("Location: donatorcomplete.php?type=12");
}
else
{
die ("Don't even try it");
}
}
if ($id!==$userid)
{
mysql_query("INSERT INTO events VALUES ('',$id,unix_timestamp(),0,'You recieved a donation pack off $username($userid)')",$c);
}

?>

 

Any other suggestions for improvement would also obviously be appreciated.

 

 

Please use the [CODE]...[/CODE] tags

Link to comment
Share on other sites

Re: Another Question...

Oh man, there's a ton of queries in there.

My suggestion: Figure out which query is in error first. To do so, you'll need some basic debugging work done.

 

Add this into your script before the second query:

echo "<h1>We made it this far.</h1>";

 

And if you see that come up on the screen before the error message, then move that line of code to just before the next query.

 

Once the message appears AFTER the ERROR MESSAGE, then the query just before that message is the one in error.

When you've figured out which query is in error, post back here and let us know which one it is.

Then we can help you out.

I'd never look through a ton of lines of code for a small error when this debug step will help locate the error, saving us time, which will then help ensure that you get a better answer for your problem :)

Link to comment
Share on other sites

Guest Anonymous

Re: Another Question...

Yay - brute force debugging -- :D

I have to admit to using this method from time to time, although I normally use a little gizmo which really helps debugging -- an error-stack handler.

Grab it from my own forums code repository (I think they are up and running)

Link to comment
Share on other sites

Re: Another Question...

lol yeah, that's true.

A slightly more sophisticated solution is to use a custom mysql_query function which automatically handles a mysql error.

for instance, without this, a query might look like:

 

mysql_query('select blah from foo where bar = 1');

 

and with a custom query function:

 

function query($query) {
$query = mysql_query($query);
if (!$query) {
echo $query . '<hr>';
die(mysql_error());
}
return $query;
}

query('select blah from foo where bar = 1');

 

What's the difference you ask?

Each time you run a query using the query function, you automatically get a mysql_error echoed out to you, and the script execution stops, IF you get an error.

This can be helpful when you're debugging because since the script execution stops, you may be able to see just where the script execution ended. In addition to this, you also get to see the entire query you executed so that you know exactly which one was in error.

But in the case of the TC, I assumed a "lesser" approach would be easier and simpler and doable by someone without coding experience.

Link to comment
Share on other sites

Re: Another Question...

The debug_backtrace function is very useful to point out exactly where the error occurred.

I myself also have a little "gizmo" to point out the error, it might now be perfect, but it does the job.

I think I still found the base of this on php.net, one of the many comments.

 

<?php
// the following line is required to declare another error handler
set_exception_handler('handle_error');

function handle_error($errno, $errstr, $errfile, $errline) {
$errno = $errno & error_reporting();
if($errno == 0) return;
if(!defined('E_STRICT'))            define('E_STRICT', 2048);
if(!defined('E_RECOVERABLE_ERROR')) define('E_RECOVERABLE_ERROR', 4096);
   	$error_string = '[b]';
   	switch($errno){
       		case E_ERROR:               $error_string .= 'Error';      			   break;
       		case E_WARNING:             $error_string .= 'Warning';                break;
       		case E_PARSE:               $error_string .= 'Parse Error';            break;
       		case E_NOTICE:              $error_string .= 'Notice';                 break;
       		case E_CORE_ERROR:          $error_string .= 'Core Error';             break;
       		case E_CORE_WARNING:        $error_string .= 'Core Warning';           break;
       		case E_COMPILE_ERROR:       $error_string .= 'Compile Error';          break;
       		case E_COMPILE_WARNING:     $error_string .= 'Compile Warning';        break;
       		case E_USER_ERROR:          $error_string .= 'User Error';             break;
       		case E_USER_WARNING:        $error_string .= 'User Warning';           break;
       		case E_USER_NOTICE:         $error_string .= 'User Notice';            break;
       		case E_STRICT:              $error_string .= 'Strict Notice';          break;
       		case E_RECOVERABLE_ERROR:   $error_string .= 'Recoverable Error';      break;
       		default:                    $error_string .= 'Unknown error (' . $errno . ')'; break;
   	}
   	$error_string .= ':[/b] [i]' . $errstr . '[/i] in [b]' . $errfile . 
   		'[/b] on line [b]' . $errline . '[/b]';
   	if(function_exists('debug_backtrace')){
       		$backtrace = debug_backtrace();
       		array_shift($backtrace);
       		foreach($backtrace as $i=>$l){
           			$error_add .= '
[' . $i . '] in function [b]' . $l['class'] . 
           			$l['type'] . $l['function'] . '[/b]';
           			if($l['file']) $error_add .= ' in [b]' . $l['file'] . '[/b]';
           			if($l['line']) $error_add .= ' on line [b]' . $l['line'] . '[/b]';
       		}
   	}
          else {
                     $error_add .= 'None';
          }
   	if(isset($GLOBALS['error_fatal']) && $GLOBALS['error_fatal'] & $errno) exit('fatal');

   	echo '<div class="error" id="error">
   	[b]An Error Has Occured[/b]
' . 
   	nl2br($error_string). '

Additional Information about this error:

   	Trace: '.nl2br($error_add).'
   	</div>';
   	return false;
}

?>

 

Just giving another way to do this...

Link to comment
Share on other sites

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.   Paste as plain text instead

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