Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.

Messages - C. Bob

Pages: [1]
1
General PHP Help / Re: PHP/MySQL Form submission troubles
« on: January 03, 2012, 06:07:22 AM »
...and it's now functioning brilliantly!

My heartfelt congratulations to you; your work is essential to powering the site I had in mind, and it looks like it should be a painless way for anyone, at any level of experience to reproduce something like the Kitten War page.

Thanks very much for all the time you spent on this, and although this might not mean much, I'll make sure you receive proper credit everywhere my copy of your code is used.

2
General PHP Help / Re: PHP/MySQL Form submission troubles
« on: January 02, 2012, 07:07:34 PM »
Curiously, it doesn't seem like much has changed:

PHP Code: [Select]
Array ( [0] => [id] => [1] => [kitten_one] => [2] => [kitten_two] => [3] => -[winner] => -[4] => 192.168.1.64 [ip] => 192.168.1.64 Void in the matrix!

Is there any other debugging technique I can try, to see if I can get more information? I tried var_dump along with print_r, but it gave the same stuff that print_r does, just in a slightly different format.

3
General PHP Help / Re: PHP/MySQL Form submission troubles
« on: January 02, 2012, 06:17:40 PM »
I hate to bog down the debugging and admit my ignorance, but by the database containing the correct IP address, are you referring to the IP addresses that are being stored in the "games" table, or should I look somewhere else?

PHP Code: [Select]

Array ( [0] => [id] => [1] => [kitten_one] => [2] => [kitten_two] => [3] => [winner] => [4] => 192.168.1.64 [ip] => 192.168.1.64 Void in the matrix!


The result looks fine to me, except that it isn't recording the winner here. The IP address is correct in that that is my local address.

4
General PHP Help / Re: PHP/MySQL Form submission troubles
« on: January 02, 2012, 01:53:11 PM »
(I intended to highlight the code, but didn't realize it wouldn't happen; please disregard the bulletin board in the last post.)

5
General PHP Help / Re: PHP/MySQL Form submission troubles
« on: January 02, 2012, 01:51:10 PM »
Bit of a breakthrough here.

Making the following change to the code made the page begin generating games:

PHP Code: [Select]
$game_query "INSERT INTO `games` (`kitten_one`, `kitten_two`, `winner`, `ip`) VALUES ('" $random_kitten_one "', '" $random_kitten_two "', '', '" $_SERVER['REMOTE_ADDR'] . "')";
[
b]$result2 mysql_query($game_query);[/b]


The page now generates a game every time new kittens are selected, as it is supposed to, but pressing the submit button returns a "Void in the matrix" error.

6
General PHP Help / Re: PHP/MySQL Form submission troubles
« on: December 31, 2011, 09:11:38 PM »
It seems to be, yes -- according to Sequel Pro, my MySQL GUI client, this is the current create syntax for the "games" table:

Code: [Select]
CREATE TABLE `games` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `kitten_one` int(10) DEFAULT NULL,
  `kitten_two` int(10) DEFAULT NULL,
  `winner` int(10) DEFAULT NULL,
  `ip` varchar(15) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

7
General PHP Help / Re: PHP/MySQL Form submission troubles
« on: December 31, 2011, 08:19:46 PM »
I added ten, and they do show up in the kittens.php source in the browser, but the game ID always returns 0:

Code: [Select]
<form action="?" method="post"><input type="hidden" name="[b]game_id[/b]" value="[b]0[/b]"><input type="hidden" name="kitten_id" value="2"><input type="submit" value="Vote 4 Kitten One"></form><form action="?" method="post"><input type="hidden" name="game_id" value="0"><input type="hidden" name="kitten_id" value="3"><input type="submit" value="Vote 4 Kitten Two"></form>
"auto_increment" is set to 1 on the "games" table, so that shouldn't be a problem, but I'm not sure.

8
General PHP Help / Re: PHP/MySQL Form submission troubles
« on: December 31, 2011, 02:24:03 PM »
Thanks very much for the help, work, and attention.

I've set to work on trying to get this implemented, creating the tables to match the new code requirements. I created a "kittens" InnoDB table with a single default id field, and a "games" InnoDB table with the fields outlined in the  comment.

The submit buttons show up, but so far they consistently bring up the "Kitten fail" error. When I temporarily commented out some of the security measures to see if I could get closer to the problem, I believe I ended up with the "Could not find game" error. I'm not sure why this is; I'm pretty sure I created the table correctly, and from the little I can tell the code looks fine.

9
General PHP Help / Re: PHP/MySQL Form submission troubles
« on: December 30, 2011, 07:38:31 PM »
The first three steps are just as you guessed; after that, things get a tad bit different; I'll try to break the whole thing down as much as possible. I'm not sure this is quite the format you asked for, but I hope it will still be understandable and helpful.

"Step 1" is very complicated, and I don't think it will make sense in its own right without knowing what happens afterwards, so I would recommend you start reading at "Step 4", then go back to the top after you've read to step 6.

Step 1.
The user visits the page, "numbertest.php". The page logs in to the server and selects the database we'll be using for this, "test".
PHP Code: [Select]
  <?php 
   mysql_connect
("localhost""user""password") or die(mysql_error());
 echo 
"Initial connection to the server was successful!<br/>";
 
mysql_select_db("test") or die(mysql_error());


Step 2.
Having selected the "test" database, it selects the first and second items in the "storage" table and records their values for future use in the "$storage" variables.
PHP Code: [Select]
$result mysql_query("SELECT * FROM storage WHERE ID = '1'"); 
$row mysql_fetch_assoc($result);
 
$storage $row['numbers'];


Step 3.
It now updates the item in the "game" table that has an ID that is equal to the number that is stored in the first item in "storage". The "game" table item's "point" total is increased by 1.
PHP Code: [Select]
   $result mysql_query("UPDATE game SET Points=points + 1 WHERE ID = '$storage'");

The other, parallel code shown in my first post works the same way, except that it decreases the "game" table item's "point" total, instead of increasing it.

Step 4.
The two random numbers are generated, as you mentioned. The numbers are equal to IDs in the "game" table, and it displays those items' contents. Two forms appear, each displaying only a single submit button.

Step 5.
This is where things start to get a little bit complicated (everything else you said up to this point was correct).

The user clicks one of the submit buttons (we'll say it's the first button, as their code functions almost exactly the same). Button #1 has this code:

PHP Code: [Select]
<?php 
  
echo '<form action="numbertest.php" method="POST"><table>';
  echo 
'<tr><td><input type="submit" name="SubmitForm" value="$test"></td></tr>';
  echo 
'<form>';

  if (!isset(
$_POST['SubmitForm'])) {
} else {
  
$result mysql_query("UPDATE storage SET numbers='$test' WHERE id = '1'");
$result mysql_query("UPDATE storage SET numbers='$test2' WHERE id = '2'");
  
  }
  
  
?>

Button #1 submits "SubmitForm" through POST. As soon as "SubmitForm" is set, then it updates the first and second items in the "storage" table, filling the contents of the first item in "storage" with the first random number (the one that was generated in step 1), and putting the second number in the second item.

... or at least, that is what it is supposed to do. There's a glitch in this process, which I will attempt to elaborate on after I've finished talking about the rest of the code.

Step 6.
The page (apparently) refreshes. Go to Step 1.



The Glitch

So far, the code looks workable (to me), but there's a major hangup during step 5 of the process.

Button #1 (or #2) does submit two numbers through POST, but they don't seem to be the right ones; sometimes it gives the server the opposite numbers.

After further study, I have concluded that my original explanation of the problem was wrong. In fact, the problem seems to come up every time the user switches buttons -- as long as you keep pressing the same button, it will always work as intended, but when you switch from one button to the other, it initially gives an incorrect result (but it will begin to work correctly if you keep pressing it as the numbers change). Thus, you can consistently get the buttons to work incorrectly by alternating between them.

I know this is a convoluted explanation. I created a set of pictures to try to explain them, but unfortunately it seems that images in posts aren't allowed on the forum right now. I'll try to recreate them in text-based form so that they might still help you. The things in parentheses are the buttons.

Quote
Initial connection to the server was successful!
ID: 2, Points:0
ID: 1, Points:0
($test)
($test2)

ID 2 is on top, 1 is on the bottom. I'll click the top button (for the top ID). I have the server start out at 0 in the "storage" table items' contents, so it takes one click before the server seems to start recording user clicks.

Quote
Initial connection to the server was successful!
ID: 2, Points:0
ID: 2, Points:0
($test)
($test2)
An annoying artifact of my random number generation system is that it sometimes gives me the same IDs twice in a row. I haven't fixed this yet, and it bogs down the testing process. Hitting the top button again.

Quote
Initial connection to the server was successful!
ID: 2, Points:0
ID: 1, Points:0
($test)
($test2)
Back to square one here; clicking top button.

Quote
Initial connection to the server was successful!
ID: 1, Points:-1
ID: 2, Points:1
($test)
($test2)
As you can see, the top button actually works here. However, when I click the bottom button, it will produce the opposite of its intended effect and bring both numbers back down to 0.

Quote
Initial connection to the server was successful!
ID: 1, Points:0
ID: 3, Points:0
($test)
($test2)
ID 1 is now back at 0 points. If this code functioned properly, it would be at -2. When I click the top button, it will increment ID 3 by 1 point, though that's the opposite of what I intended.

Quote
Initial connection to the server was successful!
ID: 3, Points:1
ID: 1, Points:-1
($test)
($test2)

So it does seem I now have a better understanding of how the form submissions are going haywire. The only question is why it does this, and that's one I can't answer.

I hope this new post was able to shed a little light on the situation. I know my writing is often difficult to read, so if there's anything that doesn't make sense, please ask and I'll try to explain it.

Thanks very much for your attention.

10
General PHP Help / PHP/MySQL Form submission troubles
« on: December 30, 2011, 10:51:21 AM »
I recently began a project to make an automated rating system for a radio series' episodes. The premise is that it should work similarly to the "Kittenwars" page. As a prelude to this, I've started to try to implement a test system that uses all the same parts that will be necessary for the final completed page.

Here's essentially how it is (supposed) to work:

2) PHP generates two psuedo-random numbers.
3) PHP contacts the server and selects two random items from the "game" table on the "test" database. Their IDs correspond with the previously picked random numbers.
4) PHP contacts the server and echos the previously selected random items' "points" for the user.
5) PHP generates two forms. Each one, when sent, contacts the server and updates two numbers in the "storage" table. The first number sent by each form is the "winning" ID, the second number is the "losing" ID.
6) The user clicks on one of the forms mentioned in the previous step.
1) Before any of the above code has technically happened yet, PHP connects to the server. It selects the two numbers from "storage", and updates the rows that the numbers correspond to in the "game" table -- thus completing the cycle (since the "game" table is the one the user intends to influence).

Unfortunately, I've encountered a hurdle in development. When the user clicks on the submit button, the numbers the forms send to the server are *not* the numbers that the user saw, but instead the next numbers -- the ones that are generated after the user clicks on one of the forms. This problem actually came up earlier in development, so I added the "storage" table with the idea that I could store the numbers it was supposed to have in the server (before they get wiped out by the next php rand action), and call them up for use after the page has been refreshed, but this hasn't helped me at all.

I'm sorry if this description of the problem doesn't suffice; I'm willing to provide any information on the MySQL database that's necessary, and/or pictures of any stage of the process that y'all may want.

Here's the PHP code (sloppy as it is) below. I don't have any prior experience, so I have to admit that most of this comes from combining and changing tutorial code fragments. Thanks in advance for any help anyone can give; I'm at a dead end right now.

PHP Code: [Select]
  <?php 
   mysql_connect
("localhost""user""password") or die(mysql_error());
 echo 
"Initial connection to the server was successful!<br/>";
 
mysql_select_db("test") or die(mysql_error());
$result mysql_query("SELECT * FROM storage WHERE ID = '1'"); 
$row mysql_fetch_assoc($result);
 
$storage $row['numbers'];
 
$result mysql_query("SELECT * FROM storage WHERE ID = '2'"); 
 
$row mysql_fetch_assoc($result);
 
$storage2 $row['numbers'];
   
$result mysql_query("UPDATE game SET Points=points + 1 WHERE ID = '$storage'");
$result mysql_query("UPDATE game SET Points=points - 1 WHERE ID = '$storage2'");
?>

 <?php
$test 
rand(13);
$test2 rand(13); ?>

    <?php
$result 
mysql_query("SELECT * FROM game WHERE ID = '$test'"); 
  
while(
$row mysql_fetch_assoc($result)){
 echo 
"ID: ".$row['id'].", Points:".$row['points']."<br/>";
 
    }
 
 
$result mysql_query("SELECT * FROM game WHERE ID = '$test2'"); 

while(
$row mysql_fetch_assoc($result)){
 echo 
"ID: ".$row['id'].", Points:".$row['points']."<br/>";
 
   }
   
?>

<?php 
  
echo '<form action="numbertest.php" method="POST"><table>';
  echo 
'<tr><td><input type="submit" name="SubmitForm" value="$test"></td></tr>';
  echo 
'<form>';

  if (!isset(
$_POST['SubmitForm'])) {
} else {
  
$result mysql_query("UPDATE storage SET numbers='$test' WHERE id = '1'");
$result mysql_query("UPDATE storage SET numbers='$test2' WHERE id = '2'");
  
  }
  
  
?>
 
<?php 
  
echo '<form action="numbertest.php" method="POST"><table>';
  echo 
'<tr><td><input type="submit" name="SubmitForm2" value="$test2"></td></tr>';
  echo 
'<form>';

  if (!isset(
$_POST['SubmitForm2'])) {
} else {
  
$result mysql_query("UPDATE storage SET numbers='$test2' WHERE ID = '1'");
$result mysql_query("UPDATE storage SET numbers='$test' WHERE ID = '2'");
  
  }
  
  
?>

Pages: [1]