PHP problems with adding a number taken from PHPmyadmin SQLI server/database

Hi all.

I got a wierd problem.

I want to do something as simple as adding 2 numbers, taken from a database.
But the one number will just NOT Work. Instead. It is like multiplying or something. :S

The exact problem is this: $_POST[score_2]
When i use that line, it wont Work.

Here is the formel: $b=$_POST[score_2];
                                  $c=$a + $b;

If i instead put in a number instead of $_POST[score_2];, it will do just fine.
Or if i use any other $_POST[]; it also Works fine.

But something is wrong with "score_2" in my database.

LOOK AT LINE 49 and 50....

Hope you can read the code and gief me a hint. I think the problem is much wierd.

Best regards Mike

<LINK href="design_info_t.css" rel="stylesheet" type="text/css">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<div class="wrapper">
<div class="tavle">

if (isset($_SESSION['navn'])){

require 'login_to_mysqli.php';
require 'static_menu_info_t.php';


/*-------------------Ved opdatering af brugerens score--------------------*/

/*-------------------Opdater uden ny score--------------------*/	
	if($_POST[nyScore] == ''){
		$UpdateQuery = "UPDATE tavlen SET navn='$_POST[navn]', alias='$_POST[alias]' WHERE ID='$_POST[ID]'";
		mysqli_query($link, $UpdateQuery);
/*-------------------Opdater med ny score--------------------*/	
		$z=(($x + $y) / 2);

/*-------------------Guldkugler i år--------------------*/			
		if($z < 24 ){$a=7;}
		else if($z < 25 ){$a=6;}
		else if($z < 26 ){$a=5;}
		else if($z < 28 ){$a=4;}
		else if($z < 30 ){$a=3;}
		else if($z < 34 ){$a=2;}
		else if($z < 38 ){$a=1;}
		$c=$a + $b;
		$UpdateQuery = "UPDATE tavlen SET navn='$_POST[navn]', alias='$_POST[alias]', score='$z', score_1='$a', score_3='$c' WHERE ID='$_POST[ID]'";
		mysqli_query($link, $UpdateQuery);


/*-------------------Nulstil tavlen til 40 for alle. WARNING.--------------------*/
$UpdateQuery = "UPDATE tavlen SET score_2='40' WHERE score_2!='40'";
mysqli_query($link, $UpdateQuery);
/*-------------------Slet brugere--------------------*/
$DeleteQuery = "DELETE FROM tavlen WHERE ID='$_POST[ID]'";
mysqli_query($link, $DeleteQuery);
/*-------------------Tilføj nye brugere--------------------*/
$AddQuery = "INSERT INTO tavlen (navn, alias, score) VALUES ('$_POST[nyNavn]','$_POST[nyAlias]','40')";
mysqli_query($link, $AddQuery);

$query = "SELECT * FROM tavlen ORDER BY navn" or die("Error in the consult.." . mysqli_error($link));
$myData = $link->query($query);

echo "<br><h1>Ratingtavle</h1>";
echo "<table>";
$form = <<<ENDFORM
<form action=elo.php method=post>
<td><b>Opret ny Ratingtavle deltager:</b> </td>
<td><input type="text"  placeholder="Navn" name="nyNavn"  	/> </td>
<td><input type="text"  placeholder="Alias" name="nyAlias" 	/> </td>
<td><input type=submit name=tilføj value='Opret' > </td>
echo $form;
echo "</table><br><br><br>";

echo "<table><tr><th></th><th>Navn</th><th>Alias</th><th>Score</th><th>Sidste resultat</th><th></th><th></th><th>Guldkugler i år</th><th>Guldkugler sidste år</th><th>Guldkugler ialt</th><th>Efter endt sæson, skal Guldkugler ialt, blive til Guldkugler sidste år.<br> Dette gøres ved at ændre navnet i databasen.</th></tr>";

while($record = $myData->fetch_object())
$form = <<<ENDFORM
<form action=elo.php method=post>
<td><input type="hidden"  name="ID"  value="$record->ID"/> </td>
<td><input type="text"  name="navn"  value="$record->navn"/> </td>
<td><input type="text"  name="alias"  value="$record->alias"/> </td>
<td><input type="text"  name="score"  value="$record->score"/> </td>
<td><input type="text"  name="nyScore"  value=""/> </td>
<td><input type=submit name=opdater value='Opdater' > </td>
<td><input type="text"  name="score_1"  value="$record->score_1"/> </td>
<td><input type="text"  name="score_2"  value="$record->score_2"/> </td>
<td><input type="text"  name="score_2"  value="$record->score_3"/> </td>
<td>-------------------------------------------------------------------------------------------------------<input type=submit name=slet value='X' style='color:#c00;' > </td>
echo $form;
echo "</table><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>";

/*-------------------Nulstil tavlen til 40 for alle. WARNING.--------------------*/
$form = <<<ENDFORM
<form action=elo.php method=post>
<td><input type=submit name="reset" value="Sæt Score til 40 for alle. Dette ødelægger HELE tavlen." style='color:#c00;'></td>
echo $form;
echo "</form>";

Open in new window

Mike KristensenIT administratorAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
For starters, right after line 14, add error_reporting(E_ALL) to your PHP script and fix the issues that you find.  Then let's revisit the problem with 100% valid PHP code.
Dave BaldwinFixer of ProblemsCommented:
For even more basic... in $_POST[score_2], score_2 will be first considered a Constant.  Put it in quotes to be an index name like $_POST['score_2'].  In addition, $_POST variables will come from a form, not a database, and they will all actually be in text format.  On occasions where I have to be sure of the type, I will multiply by 1 or add 0 which will 'force' PHP to consider it a number.
$score2 = $_POST['score_2'];
$score2 = $score2 * 1;

Open in new window

Ray PaseurCommented:
If you're new to PHP, this article may help you find some dependable learning resources.
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Brian TaoSenior Business Solutions ConsultantCommented:
In addition to Ray's and Dave's comment, usually what I will do is to force a POSTED variable into a number with the following:
    $c=$a + $b;

Open in new window

Mike KristensenIT administratorAuthor Commented:
What if i want to use a number from the database? Right now im reading from a form, but actually its just a number from the database.

Can i get that number? That would make the "route" more clean?
Dave BaldwinFixer of ProblemsCommented:
No, everything we have said would still apply.  Data from a database comes back in text form in an array, just like a POST does.  You still need to quote the indexes and possibly multiply by 1 to tell PHP it is a number.
Ray PaseurCommented:
What if i want to use a number from the database? ... Can i get that number?
Yes.  PHP is a loosely typed language.  You can use the number as a number or a character string.  PHP will convert it automatically for you depending on the context in use at the time.
Mike KristensenIT administratorAuthor Commented:
Im trying to do the Things you show me.

But i have no mistakes. And if i change:
$c=$a + $b;

            $c=$a + $b;

it will do just fine. :S And score_1 vs. score_2 are EXACTLY the same setup in the database.

Seems so wierd.
Ray PaseurCommented:
$_POST is an external variable.  It contains the contents of the HTTP request variables.  These references may help:

You can use var_dump($_POST) to see what is in the request.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mike KristensenIT administratorAuthor Commented:
After trying what was told, nothing worked. I later figured out it was a type mistake..... But good help.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Search Engine Optimization (SEO)

From novice to tech pro — start learning today.