MySQL Error
Moderator: General Moderators
-
coreycollins
- Forum Commoner
- Posts: 67
- Joined: Sun Feb 01, 2004 1:04 pm
- Location: Michigan
-
coreycollins
- Forum Commoner
- Posts: 67
- Joined: Sun Feb 01, 2004 1:04 pm
- Location: Michigan
On another note, if you're sending data to be entered in your database you may what to use a post. The reason being, users can modify the values in the string and resend the data. If it is a post the users can't see that values.
If you do change it, you need to change the $_GET to $_POST in your code.
If you do change it, you need to change the $_GET to $_POST in your code.
I get the division by 0 error
http://www.krunkdesigns.com/gtthing/gtthing2.php
this is wut my code looks like now
http://www.krunkdesigns.com/gtthing/gtthing2.php
this is wut my code looks like now
Code: Select all
<?php
if (isset($_POST["action"]))
{
if ($_POST["action"] == "submit")
{
$db = mysql_connect("localhost", "krunk_gtstat", "gtstat");
mysql_select_db("krunk_gtstat",$db);
$sql = "INSERT INTO stats (username,percent) VALUES ('$username','$percent')";
$result = mysql_query($sql);
}
}
?>
<?php
$value1=$_POST['value1'];
$value2=$_POST['value2'];
$value3=$_POST['value3'];
$value4=$_POST['value4'];
$username=$_POST['username'];
$total=(($value1 + $value2 + $value3) / $value4);
$percent=$total * 10;
?>
<form method="POST" action=="<?php echo $PHP_SELF?>">
<font size=1 face="verdana" color=#000000>
Username:<br>
<input type"text" name="username" size="20" style="font-family: Arial; font-size: 8pt; color: #000000"><br>
Games Won<br>
<input type"text" name="value1" size="20" style="font-family: Arial; font-size: 8pt; color: #000000"><br>
Games lost by someone reaching 100 mojo<br>
<input type"text" name="value2" size="20" style="font-family: Arial; font-size: 8pt; color: #000000"><br>
Games lost by zero<br>
<input type"text" name="value3" size="20" style="font-family: Arial; font-size: 8pt; color: #000000"><br>
Games played<br>
<input type"text" name="value4" size="20" style="font-family: Arial; font-size: 8pt; color: #000000"><br>
<input type="reset" value="Reset Form" name="reset" style="font-family: verdana; font-size: 8pt; color: #000000">
<input type="submit" value="Submit" name="send" style="font-family: verdana; font-size: 8pt; color: #000000">
<input type="hidden" name="action" value="submit">
</form>
</font>-
coreycollins
- Forum Commoner
- Posts: 67
- Joined: Sun Feb 01, 2004 1:04 pm
- Location: Michigan
Here's what I would do:
Move
inside the area where you check if action is set and if action=submit. This code is only going to make sense if the user has submitted data else not of those are going to be set anyway.
If value4 if never entered in your form then you are going to get a divide by 0 error.
It doesn't look like you need to insert percent into the table. Well at least where you're doing it now. I would move the insert below the code where you do the math if you want the percent to be added to the table.
Move
Code: Select all
<?php
$value1=$_POST['value1'];
$value2=$_POST['value2'];
$value3=$_POST['value3'];
$value4=$_POST['value4'];
$username=$_POST['username'];
$total=(($value1 + $value2 + $value3) / $value4);
$percent=$total * 10;
?>If value4 if never entered in your form then you are going to get a divide by 0 error.
It doesn't look like you need to insert percent into the table. Well at least where you're doing it now. I would move the insert below the code where you do the math if you want the percent to be added to the table.
-
coreycollins
- Forum Commoner
- Posts: 67
- Joined: Sun Feb 01, 2004 1:04 pm
- Location: Michigan
My edited version of your code:
Just remember, value4 has to be set as of right now.
Code: Select all
if (isset($_POST["action"]))
{
if ($_POST["action"] == "submit")
{
$value1=$_POST['value1'];
$value2=$_POST['value2'];
$value3=$_POST['value3'];
$value4=$_POST['value4'];
$username=$_POST['username'];
$total=(($value1 + $value2 + $value3) / $value4);
$percent=$total * 10;
$db = mysql_connect("localhost", "krunk_gtstat", "gtstat");
mysql_select_db("krunk_gtstat",$db);
$sql = "INSERT INTO stats (username,percent) VALUES ('$username','$percent')";
$result = mysql_query($sql);
}
}
?>-
coreycollins
- Forum Commoner
- Posts: 67
- Joined: Sun Feb 01, 2004 1:04 pm
- Location: Michigan
thanks, i didnt realize i had 2 = signs in there, but now when i try to connect to the database and show it, i get an error and when i go to phpmyadmin it still says there is 0 rows in the table
heres the error
and this is wut im using to get the data
heres the error
Code: Select all
Warning: mysql_result(): Unable to jump to row 0 on MySQL result index 3 in /home/krunk/public_html/gtthing/gtthing3.php on line 25
Username:
Warning: mysql_result(): Unable to jump to row 0 on MySQL result index 3 in /home/krunk/public_html/gtthing/gtthing3.php on line 29
Percentage:Code: Select all
<?php
$db = mysql_connect("localhost", "krunk_gtstat", "gtstat");
mysql_select_db("krunk_gtstat",$db);
$result = mysql_query("SELECT * FROM stat",$db);
printf("Username: %s<br>\n", mysql_result($result,0,"username"));
printf("Percentage: %s<br>\n", mysql_result($result,0,"percent"));
?>
</body>-
coreycollins
- Forum Commoner
- Posts: 67
- Joined: Sun Feb 01, 2004 1:04 pm
- Location: Michigan
-
coreycollins
- Forum Commoner
- Posts: 67
- Joined: Sun Feb 01, 2004 1:04 pm
- Location: Michigan
-
coreycollins
- Forum Commoner
- Posts: 67
- Joined: Sun Feb 01, 2004 1:04 pm
- Location: Michigan