Mike Kristensen
asked on
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
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>
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.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
:-)
How and why we use quote marks in PHP...
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_12241-Quotation-Marks-in-PHP.html
How and why we use quote marks in PHP...
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_12241-Quotation-Marks-in-PHP.html
ASKER
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?
Can i get that number? That would make the "route" more clean?
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.
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.
ASKER
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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
After trying what was told, nothing worked. I later figured out it was a type mistake..... But good help.