Jump to content

Script broken, need some help


Acridic

Recommended Posts

Okay so I am still a baby at PHP, I got the PHP 5 book for dummies but I haven't finished it all yet. Maybe I messed up something in mysql database or maybe it is just the php but the short story is this is my first time running a website.

 

Anyways here is the script:

<?php



include("functions/functions.php");
include("functions/functions_users.php");
include("functions/functions_adopts.php");
include("inc/lang.php");



//***************//
// START SCRIPT //
//***************//



$id = $_GET["id"];
$promocode = $_GET["promocode"];
$name = $_GET["name"];



if($isloggedin == "yes"){



$stmt = $adopts->query ($query = "SELECT * FROM {$prefix}adoptables, {$prefix}"."owned_adoptables WHERE owner='".$loggedinname."' AND currentlevel<'6' AND isfrozen='no'");
$data = $stmt->fetchAll();
$num = count($data);



if($num >= 7){ 
$canadopt = "no"; 
$article_title = "Too many eggs!"; $article_content = "You already have too many eggs to take care of! Come back once you have hatched an egg!"; 
}
else 
if($_SESSION["allow"] != 1){ 


$article_title = $err_idnoexist;
$article_content = $err_idnoexist_text;
// The adoptable ID appears to be valid, so we need to double check that it is valid by pulling up the adoptable in the DB


$query = "SELECT * FROM {$prefix}adoptables, {$prefix}adoptables_conditions 
WHERE {$prefix}adoptables.id='{$id}' 
AND {$prefix}adoptables_conditions.id = {$prefix}adoptables.id";
$stmt = $adopts->query($query);
$row = $stmt->fetchObject();



if($id == $row->id){
// The ID submitted matches an existing adoptable type
$canadopt = canadopt($row->id, "adopting", $promocode, $row);



// If we can adopt this creature, do the adoption
if($canadopt == "yes") {
if (changecash(-$row->cost, $GLOBALS['loggedinname'], $GLOBALS['money'])==true) {
// BEGIN the actual adoption process



// First we see if we have a custom name; if not, we use the default name
if($name == "") $name = $row->type;



// Now we determine if we are using alternate images or not



$alts = getaltstatus($id, 0, 0);



// We need a unique code for the adoptable so we can show it to the user when we're done here...



$code = codegen(10, 0);
$genders = array('f', 'm');
$rand = rand(0,1);


$query = "INSERT INTO {$prefix}owned_adoptables (aid, type, name, owner, currentlevel, totalclicks, code, imageurl, usealternates, tradestatus, isfrozen, gender, lastbred)
VALUES ('', '{$row->type}', '{$name}','{$loggedinname}','0','0', '{$code}', '','{$alts}','fortrade','no', '{$genders[$rand]}','0')";
$adopts->query($query);


if(!empty($promocode)){
// The promo code has been used, needs to update it before information is leaked out!
$newpromocode = codegen(15,0);
$query = "UPDATE {$prefix}adoptables_conditions SET promocode='{$newpromocode}' WHERE type='{$row->type}'";
$adopts->query($query);
}


// Adoption complete, show the user a confirmation screen...



$stmt = $adopts->query("SELECT * FROM {$prefix}owned_adoptables WHERE code='{$code}' and owner='{$loggedinname}'");
$owned_adoptable = $stmt->fetchObject();
$id = $owned_adoptable->aid;



$article_title = $name." adopted successfully";
$article_content = "<img src='{$row->eggimage}'><br>{$congrats1} {$name}. You can now manage {$name} on the 
<a href='myadopts.php'>My Adopts</a> page.<br><br><b><a href='myadopts.php?act=manage&id={$id}'>Click Here to Manage {$name}</a><br>
<a href='myadopts.php?act=bbcode&id={$id}'>Click Here to get BBCodes / HTML Codes for {$name}</a></b><br><br>
Be sure and <a href='levelup.php?id={$id}'>feed</a> {$name} with clicks so that they grow!";
unset($_SESSION["allow"]);
// END the actual adoption process
}
else {
$article_title = "Not enough money.";
$article_content = "You don't have enough {$GLOBALS['settings']['cost']} to buy this adoptable. Earn some money and then try again.";
}
}
else {
$article_title = $accden;
$article_content = $adoptnoper;
}
} // End the if for if $id == $aid
else {
// Adoptable does not exist, show an error.



$article_title = $err_idnoexist;
$article_content = $err_idnoexist_text;
} // End the else for if $id == $aid
} // End the valid ID input else test statement (bulk of code goes above here)
}
} // End the log in check IF
else {
// Guests cannot adopt pets, so why bother...
$article_title = $guesttitleerror;
$article_content = $guesterror;
} // End the log in check ELSE 



//***************//
// OUTPUT PAGE //
//***************//



echo showpage($article_title, $article_content, $date);



?>

 

Ive tried everything from staring at it to poking it around but it doesn't change anything. I want to make it so people can pick out up to 7 eggs before they have to wait for them to reach a certain level and then they can pick up more eggs. I thought changing

 

if($num >= 7)

To 7 would work, but it still limits them to only 2 eggs. I really don't know what else to do.

Link to comment
Share on other sites

You have several conditions in your logic that must be true before the INSERT query is executed. To pin down the problem, you must determine exactly at what point your code and data are doing what you expect and at what point they are not.

 

Echo unique progress messages before and after conditional tests to determine what the execution path is. Use echo/print_r/var_dump statements to display what data values are.

Link to comment
Share on other sites

I would also advise reviewing your logic in general. There are a ton of nested if conditions in that code. That makes it very difficult to debug and maintain your code. If you have an if/else condition for an error - create the condition to check for the error - not the success - so you can put the error logic right after the condition. Otherwise the code is disconnected

 

This is poor format, in my opinion

if(SUCCESS_CONDITION_1)
{
  if(SUCCESS_CONDITION_2)
  {
     //Success logic
  } else {
     //Error condition 2 logic
  }
} else {
  //Error condition 1 logic
}

 

This is better/cleaner

if(ERROR_CONDITION_1)
{
  //Error condition 1 logic
}
elseif(ERROR_CONDITION_2)
{
  //Error condition 2 logic
}
else
{
  //Success logic
}

Edited by Psycho
Link to comment
Share on other sites

I've never seen this before - is this even legit - defining a variable inside a call to run a query?

$stmt = $adopts->query($query = "SELECT * FROM {$prefix}adoptables, {$prefix}"."owned_adoptables WHERE owner='".$loggedinname."' AND currentlevel<'6' AND isfrozen='no'");

Edited by Psycho
Link to comment
Share on other sites

I was given the code & the whole script for free, it does run I think it was ment for PHP4? But for what ever reason I can't get the PHP 5 script to run.

 

Heres the original doadopt file before I was given the egg lock code (not sure if that will help or not ^-^).

 

<?php


include("functions/functions.php");
include("functions/functions_users.php");
include("functions/functions_adopts.php");
include("inc/lang.php");


//***************//
// START SCRIPT //
//***************//


$id = $_GET["id"];
$promocode = $_GET["promocode"];
$name = $_GET["name"];


if($isloggedin == "yes"){
if($_SESSION["allow"] != 1){

$article_title = $err_idnoexist;
$article_content = $err_idnoexist_text;
}
elseif($_SESSION["allow"] == 1){

// I guess the first thing to do is see if we have a valid adoptable ID submitted...
if($id == "" or !is_numeric($id)){
$article_title = $err_idnoexist;
$article_content = $err_idnoexist_text;
}
else{
// The adoptable ID appears to be valid, so we need to double check that it is valid by pulling up the adoptable in the DB




$query = "SELECT * FROM {$prefix}adoptables, {$prefix}adoptables_conditions
WHERE {$prefix}adoptables.id='{$id}'
		 AND {$prefix}adoptables_conditions.id = {$prefix}adoptables.id";
$stmt = $adopts->query($query);
$row = $stmt->fetchObject();


if($id == $row->id){
// The ID submitted matches an existing adoptable type
$canadopt = canadopt($row->id, "adopting", $promocode, $row);


// If we can adopt this creature, do the adoption
if($canadopt == "yes") {
 if (changecash(-$row->cost, $GLOBALS['loggedinname'], $GLOBALS['money'])==true) {
 // BEGIN the actual adoption process


 // First we see if we have a custom name; if not, we use the default name
 if($name == "") $name = $row->type;


 // Now we determine if we are using alternate images or not


 $alts = getaltstatus($id, 0, 0);


 // We need a unique code for the adoptable so we can show it to the user when we're done here...


 $code = codegen(10, 0);
 $genders = array('f', 'm');
 $rand = rand(0,1);

$query = "INSERT INTO {$prefix}owned_adoptables (aid, type, name, owner, currentlevel, totalclicks, code, imageurl, usealternates, tradestatus, isfrozen, gender, lastbred)
 VALUES ('', '{$row->type}', '{$name}','{$loggedinname}','0','0', '{$code}', '','{$alts}','fortrade','no', '{$genders[$rand]}','0')";
 $adopts->query($query);

if(!empty($promocode)){
 // The promo code has been used, needs to update it before information is leaked out!
 $newpromocode = codegen(15,0);
 $query = "UPDATE {$prefix}adoptables_conditions SET promocode='{$newpromocode}' WHERE type='{$row->type}'";
$adopts->query($query);
 }

 // Adoption complete, show the user a confirmation screen...


$stmt = $adopts->query("SELECT * FROM {$prefix}owned_adoptables WHERE code='{$code}' and owner='{$loggedinname}'");
$owned_adoptable = $stmt->fetchObject();
$id = $owned_adoptable->aid;


 $article_title = $name." adopted successfully";
 $article_content = "<img src='{$row->eggimage}'><br>{$congrats1} {$name}. You can now manage {$name} on the
 <a href='myadopts.php'>My Adopts</a> page.<br><br><b><a href='myadopts.php?act=manage&id={$id}'>Click Here to Manage {$name}</a><br>
 <a href='myadopts.php?act=bbcode&id={$id}'>Click Here to get BBCodes / HTML Codes for {$name}</a></b><br><br>
 Be sure and <a href='levelup.php?id={$id}'>feed</a> {$name} with clicks so that they grow!";
 unset($_SESSION["allow"]);
 // END the actual adoption process
 }
 else {
 $article_title = "Not enough money.";
 $article_content = "You don't have enough {$GLOBALS['settings']['cost']} to buy this adoptable. Earn some money and then try again.";
 }
}
else {
 $article_title = $accden;
 $article_content = $adoptnoper;
}
} // End the if for if $id == $aid
else {
// Adoptable does not exist, show an error.


$article_title = $err_idnoexist;
$article_content = $err_idnoexist_text;
} // End the else for if $id == $aid
} // End the valid ID input else test statement (bulk of code goes above here)
}
} // End the log in check IF
else {
// Guests cannot adopt pets, so why bother...
$article_title = $guesttitleerror;
$article_content = $guesterror;
} // End the log in check ELSE


//***************//
// OUTPUT PAGE //
//***************//


echo showpage($article_title, $article_content, $date);


?>

 

Edit: I have to go run to the bank and deal with some stuff, when I get back I will look up what you said PFMaBiSmAd

Edited by Acridic
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.