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

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Ratingtavle</title>
<LINK href="design_info_t.css" rel="stylesheet" type="text/css">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<div class="wrapper">
<div class="tavle">



<?php
if (isset($_SESSION['navn'])){

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




/*-------------------------------------------------------------------------Ratingtavle-------------------------------------------------------------*/

/*-------------------Ved opdatering af brugerens score--------------------*/
if(isset($_POST['opdater'])){	

/*-------------------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--------------------*/	
	else{
		$x=$_POST[score];
		$y=$_POST[nyScore];
		$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;}
		
	    
		$b=$_POST[score_2];
		$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.--------------------*/
if(isset($_POST['reset'])){
$UpdateQuery = "UPDATE tavlen SET score_2='40' WHERE score_2!='40'";
mysqli_query($link, $UpdateQuery);
};
/*-------------------Slet brugere--------------------*/
if(isset($_POST['slet'])){
$DeleteQuery = "DELETE FROM tavlen WHERE ID='$_POST[ID]'";
mysqli_query($link, $DeleteQuery);
}
/*-------------------Tilføj nye brugere--------------------*/
if(isset($_POST['tilføj'])){
$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>
<tr>
<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>
</tr>
</form>
ENDFORM;
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>
<tr>
<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>------------------------------------</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>
</tr>
</form>
ENDFORM;
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>
ENDFORM;
echo $form;
echo "</form>";
/*--------------------------------------------------------------------------/Ratingtavle---------------------------------------------------------------------------*/
mysqli_close($link);
}
?>
</div>
</div>
</body>
</html>

Open in new window

Mike KristensenIT administratorAsked:
Who is Participating?
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.
0
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

0
Ray PaseurCommented:
If you're new to PHP, this article may help you find some dependable learning resources.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy 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:
    $b=doubleval($_POST["score_2"]);
    $c=$a + $b;

Open in new window

0
Ray PaseurCommented:
0
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?
0
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.
0
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.
0
Mike KristensenIT administratorAuthor Commented:
Im trying to do the Things you show me.

But i have no mistakes. And if i change:
$b=($_POST["score_2"]);
$c=$a + $b;

to:
$b=($_POST["score_1"]);
            $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.
0
Ray PaseurCommented:
$_POST is an external variable.  It contains the contents of the HTTP request variables.  These references may help:
http://php.net/manual/en/reserved.variables.post.php
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/A_11271-Understanding-Client-Server-Protocols-and-Web-Applications.html

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

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

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.