Page 4 of 5

Posted: Sun Mar 21, 2004 11:38 am
by tim
if your sending the vars page to page, you should use the POST method

Posted: Sun Mar 21, 2004 11:40 am
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.

Posted: Sun Mar 21, 2004 11:43 am
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.

Posted: Sun Mar 21, 2004 11:54 am
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>

Posted: Sun Mar 21, 2004 12:01 pm
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:

Posted: Sun Mar 21, 2004 12:08 pm
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.

Posted: Sun Mar 21, 2004 12:12 pm
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.

Posted: Sun Mar 21, 2004 12:13 pm
by bawla
you're a smart one

Posted: Sun Mar 21, 2004 12:17 pm
by bawla

Posted: Sun Mar 21, 2004 12:41 pm
by coreycollins
<form method="POST" action=="<?php echo $PHP_SELF?>"> may be your error. Only 1 =.

Posted: Sun Mar 21, 2004 12:48 pm
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>

Posted: Sun Mar 21, 2004 12:51 pm
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)

Posted: Sun Mar 21, 2004 12:54 pm
by bawla
http://www.krunkdesigns.com/gtthing/gtthing4.php

already had it up, never looked at it

Posted: Sun Mar 21, 2004 12:55 pm
by coreycollins
Remember to use stat not stats for your INSERT I just noticed that above.

Posted: Sun Mar 21, 2004 12:55 pm
by coreycollins
No, error checking for the Insert statement page ... you can do that error checking anywhere.