Jump to content

[SOLVED] Function - MySQL Queries Incorrect...


Recommended Posts

Evening folks, I have a function which is suppose to update the database depending on what "id" is passed in a $_POST, an Im having some strange results, my guess is I have a comma or double comma somewhere where it shouldnt or should be.

 

here's the code:

<?php
function supporterstatus() {

$con = mysql_connect("localhost","ace_ACE","shadow69");
if (!$con)
  {
die('Could not connect: ' . mysql_error());
  }
  
  mysql_select_db("ace_sl", $con);

// ---> Select Variables <--- 
$charactername_select = mysql_query("SELECT `charname` FROM `sl_users` WHERE id='".$_POST["custom"]."'");
$bank_select = mysql_query("SELECT `bank` FROM `sl_users` WHERE id='".$_POST["custom"]."'");
$maxmp_select = mysql_query("SELECT `maxmp` FROM `sl_users` WHERE id='".$_POST["custom"]."'");
$maxtp_select = mysql_query("SELECT `maxtp` FROM `sl_users` WHERE id='".$_POST["custom"]."'");
$maxhp_select = mysql_query("SELECT `maxhp` FROM `sl_users` WHERE id='".$_POST["custom"]."'");
$strength_select = mysql_query("SELECT `strength` FROM `sl_users` WHERE id='".$_POST["custom"]."'");
$dexterity_select = mysql_query("SELECT `dexterity` FROM `sl_users` WHERE id='".$_POST["custom"]."'");
$experience_select = mysql_query("SELECT `experience` FROM `sl_users` WHERE id='".$_POST["custom"]."'");
$auth_level = mysql_query("SELECT `authlevel` FROM `sl_users` WHERE id='".$_POST["custom"]."'");



if(substr($charactername_select, -4) == '~SS~'){
$redd = "";
}
else
{
$redd = " ~SS~";
}    

    $charn = $charactername_select;
    $bank = $bank_select;
    $maxmp = $maxmp_select;
    $maxtp = $maxtp_select;
    $maxhp = $maxhp_select;
$strengthh = $strength_select;
$dexterityy = $dexterity_select;
$experiencee = $experience_select;
// remove verify crap
    
        $charn = $charactername_select.$redd;
        $bank = ceil($bank_select+2500);
        $maxmp = ceil($maxmp_select+25);
        $maxtp = ceil($maxtp_select+25);
        $maxhp = ceil($maxhp_select+25);
	$strengthh = ceil($strength_select+25);
	$dexterityy = ceil($dexterity_select+25);
	$experiencee = ceil($experience_select+100);
      

    mysql_query("UPDATE `sl_users` SET `charname`='$charn' WHERE id='".$_POST["custom"]."'")
or die ("MYSQL ERROR: ".mysql_error()."");
mysql_query("UPDATE `sl_users` SET `bank`='$bank' WHERE id='".$_POST["custom"]."'")
or die ("MYSQL ERROR: ".mysql_error()."");
    mysql_query("UPDATE `sl_users` SET `maxmp`='$maxmp' WHERE id='".$_POST["custom"]."'")
or die ("MYSQL ERROR: ".mysql_error()."");
    mysql_query("UPDATE `sl_users` SET `maxtp`='$maxtp' WHERE id='".$_POST["custom"]."'")
or die ("MYSQL ERROR: ".mysql_error()."");
    mysql_query("UPDATE `sl_users` SET `maxhp`='$maxhp' WHERE id='".$_POST["custom"]."'")
or die ("MYSQL ERROR: ".mysql_error()."");
mysql_query("UPDATE `sl_users` SET `strength`='$strengthh' WHERE id='".$_POST["custom"]."'")
or die ("MYSQL ERROR: ".mysql_error()."");
mysql_query("UPDATE `sl_users` SET `dexterity`='$dexterityy' WHERE id='".$_POST["custom"]."'")
or die ("MYSQL ERROR: ".mysql_error()."");
mysql_query("UPDATE `sl_users` SET `experience`='$experiencee' WHERE id='".$_POST["custom"]."'")
or die ("MYSQL ERROR: ".mysql_error()."");
mysql_query("UPDATE `sl_users` SET `authlevel`='3' WHERE id='".$_POST["custom"]."'")
or die ("MYSQL ERROR: ".mysql_error()."");
}
?>

<?php

echo "Custom: ".$_POST["custom"];

if($_POST["custom"] == '60') {


echo "<br><br><b>Function Executed Successfully</b><br><br>";

echo "Running Function...<br>";
supporterstatus();
echo "Function Complete!<br><br>";

echo "<u>Variables - Before</u><br>";
echo '$charactername_select: '.$charactername_select."<br>";
echo '$bank_select: '.$bank_select."<br>";
echo '$maxmp_select: '.$maxmp_select."<br>";
echo '$maxtp_select: '.$maxtp_select."<br>";
echo '$maxhp_select: '.$maxhp_select."<br>";
echo '$strength_select: '.$strength_select."<br>";
echo '$dexterity_select: '.$dexterity_select."<br>";
echo '$experience_select: '.$experience_select."<br>";
echo '$auth_level: '.$auth_level."<br>";

echo "<hr>";

echo "<u>Variables - After</u><br>";
echo '$charn: '.$charn."<br>";
echo '$bank: '.$bank."<br>";
echo '$maxmp: '.$maxmp."<br>";
echo '$maxtp: '.$maxtp."<br>";
echo '$maxhp: '.$maxhp."<br>";
echo '$strengthh: '.$strengthh."<br>";
echo '$dexterityy: '.$dexterityy."<br>";
echo '$experiencee: '.$experiencee."<br>";
echo '$auth_level: '.$auth_level."<br><br>";

echo "Running Function...<br>";
supporterstatus();
echo "Function Complete!";

}
else {

echo "<br><br><b>Function Failed</b>";

}

?>

 

here's what it displays:

Custom: 60

 

Function Executed Successfully

 

Running Function...

Function Complete!

 

Variables - Before

$charactername_select:

$bank_select:

$maxmp_select:

$maxtp_select:

$maxhp_select:

$strength_select:

$dexterity_select:

$experience_select:

$auth_level:

Variables - After

$charn:

$bank:

$maxmp:

$maxtp:

$maxhp:

$strengthh:

$dexterityy:

$experiencee:

$auth_level:

 

Running Function...

Function Complete!

 

Regards ACE

Link to comment
https://forums.phpfreaks.com/topic/70128-solved-function-mysql-queries-incorrect/
Share on other sites

Hi

 

You have some dodgy mysql code - you need to use mysql_fetch_row/array etc:

 

<?php
// ....
$charactername_select = mysql_query("SELECT `charname` FROM `sl_users` WHERE id='".$_POST["custom"]."'");
$tmparr = mysql_fetch_row($charactername_select);
$charn = $tmparr[0];

 

or better yet - use one query

 

<?php

$result = mysql_query("SELECT charname,bank..... FROM sl_users WHERE id='{$_POST["custom"]}'");
$tmparr = mysql_fetch_array($result);
$charn = $tmparr["charname"];
$bank = $tmparr["bank"];

//....

mysql_query("UPDATE sl_users SET charname='$charn',bank='$bank'..... WHERE id='{$_POST["custom"]}'");

//....
$bank

 

... you following me?

That works nicely, but the database has been updated at a higher amount then it was suppose to, something to do with the ceil() function, what I have in my script, has actually doubled and been put into the database.

 

this section here:

   
<?php
     $bank = ceil($bank_select+2500);
        $maxmp = ceil($maxmp_select+25);
        $maxtp = ceil($maxtp_select+25);
        $maxhp = ceil($maxhp_select+25);
	$strength = ceil($strengthh_select+25);
	$dexterity = ceil($dexterityy_select+25);
	$experience = ceil($experiencee_select+100);
?>

 

HP, MP and TP were increased by 50 instead of 25, and experience was increased by 200 instead of 100, and bank was increased by 5000 instead of 2500. Any ideas why?

 

EDIT:

I've just noticed the ceil function rounds up integers and floats, so what function should I be using to just increase the current value by a certain number?

Hi

 

you should be able to just:

 

<?php
//...

$bank = $bank + 25;

 

... or even going back to my first answer, you can add them straight after reading from the db

 

<?php
//...
$result = mysql_query("SELECT charname,bank..... FROM sl_users WHERE id='{$_POST["custom"]}'");
$tmparr = mysql_fetch_array($result);
$charn = $tmparr["charname"];
$bank = $tmparr["bank"] + 25;

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.