Jump to content


Photo

still some trouble..


  • Please log in to reply
1 reply to this topic

#1 jktress

jktress
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 02 May 2006 - 04:48 AM

I am creating a fantasy formula 1 game. Sanfly helped me out, but I can't seem to get the right values inserted into my table. Instead, this code inserts blank rows into my table (20 or so of them) I can't seem to figure out the trouble.

I select a race date and the variable is sent in the URL to race.php. Then I enter appropriate driver points into a table called driverPoints in a mySQL database.
<?php require_once('Connections/formula1.php');
//select a database
@mysql_select_db($database_formula1) or die( "Unable to select database");
 //the query is set to a variable
 $q = "SELECT * FROM circuits ORDER BY race_date";
//the result of the query is set to a variable
$r = mysql_query($q) or die(mysql_error());
//the fetch_array loop is looking through the results of my query and returning all the rows! It looks like it is storing them in two variables
   while($row = mysql_fetch_array($r)){
      $race_name = $row['circuitName'];
      $race_id = $row['race_id'];
      $race_date = $row['race_date'];
         echo "$race_name - $race_date (<a href=\"races.php?action=addpoints&rid=$race_id\">Add Points</a>)<br>";
   }
?>

Here is the form where I enter the points the drivers earned during the race.
<?php
//get the race id and set it to a variable
$rId = $_GET['rId'];
//double check that you are coming from the post page
if($_GET['action'] = "addpoints" && $_GET['rId'] == $rId){ ?>

    <form method="post" action="races.php?action=addpoints2">
        <table>
            <tr>
                <td>Driver name</td>
                    <td>Points</td>
            </tr>
<? require('Connections/formula1.php');
@mysql_select_db($database_formula1) or die( "Unable to select database");
$q = "SELECT * FROM drivers";
    $r = mysql_query($q) or die(mysql_error());
           while($row = mysql_fetch_array($r)){
              $driver_id = $row['driver_id'];
              $driver_name = $row['nameD']; ?>
             <tr>
              
                <td><?=$driver_name?> <input type="hidden" name="rp_driver[]" value="<?=$driver_id?>"></td>
                    <td><input type="text" name="rp_points[]" size="10"></td>
            </tr>
         <?    } ?>
            <tr>
                <td colspan="2">
                    <input type="hidden" name="rp_race" value="<?=$rId?>">
                    <input type="submit" value="Update the Points">
                </td>
            </tr>
        
        </table>
    </form>

<?php    
}
if($_GET['action'] = "addpoints2"){
    // Get Posted Variables
    $rp_race = $_POST['rp_race'];
    $rp_driver = $_POST['rp_driver'];
    $rp_points = $_POST['rp_points'];
    
    $dp = array();
    $dp['driver'] = $rp_driver;
    $dp['points'] = $rp_points;
    $num = count($dp);
    for($i = 0; $i < $num; $i ++){
        $dp_driver = $dp['driver'][$i];
        $dp_points = $dp['points'][$i];
        
        require('Connections/formula1.php');
        @mysql_select_db($database_formula1) or die( "Unable to select database");
        $q = "INSERT INTO driverPoints (rp_race, rp_driver, rp_points) VALUES ('$rp_race', '$rp_driver', '$rp_points')";
        $r = mysql_query($q) or die(mysql_error());
    }
    
    echo "done, link to whatever here";
}?>

I have gone through the code a few times and still can't seem to figure out the problem. Any ideas?

#2 sasa

sasa
  • Staff Alumni
  • Advanced Member
  • 2,804 posts
  • LocationHrvatska

Posted 02 May 2006 - 07:17 PM

change lines
$num = count($dp);
...
$q = "INSERT INTO driverPoints (rp_race, rp_driver, rp_points) VALUES ('$rp_race', '$rp_driver', '$rp_points')";
to
$num = count($rp_driver);
...
$q = "INSERT INTO driverPoints (rp_race, rp_driver, rp_points) VALUES ('$rp_race', '$dp_driver', '$dp_points')";





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users