MySQL Error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

if your sending the vars page to page, you should use the POST method
coreycollins
Forum Commoner
Posts: 67
Joined: Sun Feb 01, 2004 1:04 pm
Location: Michigan

Post by coreycollins »

It really doesn't matter if he uses a get or post. One is just more secure.

If you're sending the data to that different page then the INSERT statement should be on that page.
coreycollins
Forum Commoner
Posts: 67
Joined: Sun Feb 01, 2004 1:04 pm
Location: Michigan

Post by coreycollins »

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.
User avatar
bawla
Forum Contributor
Posts: 116
Joined: Fri Mar 19, 2004 9:15 am

Post by bawla »

I get the division by 0 error

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>
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

coreycollins wrote:It really doesn't matter if he uses a get or post. One is just more secure.
notice I said "should" i didnt say he "needed" too. settle down :evil:
coreycollins
Forum Commoner
Posts: 67
Joined: Sun Feb 01, 2004 1:04 pm
Location: Michigan

Post by coreycollins »

Here's what I would do:

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; 
?>
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.
coreycollins
Forum Commoner
Posts: 67
Joined: Sun Feb 01, 2004 1:04 pm
Location: Michigan

Post by coreycollins »

My edited version of your code:

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); 
  } 
} 

?>
Just remember, value4 has to be set as of right now.
User avatar
bawla
Forum Contributor
Posts: 116
Joined: Fri Mar 19, 2004 9:15 am

Post by bawla »

you're a smart one
User avatar
bawla
Forum Contributor
Posts: 116
Joined: Fri Mar 19, 2004 9:15 am

Post by bawla »

coreycollins
Forum Commoner
Posts: 67
Joined: Sun Feb 01, 2004 1:04 pm
Location: Michigan

Post by coreycollins »

<form method="POST" action=="<?php echo $PHP_SELF?>"> may be your error. Only 1 =.
User avatar
bawla
Forum Contributor
Posts: 116
Joined: Fri Mar 19, 2004 9:15 am

Post by bawla »

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

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:
and this is wut im using to get the data

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

Post by coreycollins »

Your insert still isn't working. Refer to the previous pages of this topic for the error checking stuff. (or die(mysql_error)
User avatar
bawla
Forum Contributor
Posts: 116
Joined: Fri Mar 19, 2004 9:15 am

Post by bawla »

http://www.krunkdesigns.com/gtthing/gtthing4.php

already had it up, never looked at it
coreycollins
Forum Commoner
Posts: 67
Joined: Sun Feb 01, 2004 1:04 pm
Location: Michigan

Post by coreycollins »

Remember to use stat not stats for your INSERT I just noticed that above.
coreycollins
Forum Commoner
Posts: 67
Joined: Sun Feb 01, 2004 1:04 pm
Location: Michigan

Post by coreycollins »

No, error checking for the Insert statement page ... you can do that error checking anywhere.
Post Reply