SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Addict
    Join Date
    May 2008
    Location
    Missouri, USA
    Posts
    273
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Dynamic Form Submission

    I'm working on a form right now that is much more complex then anything i've done before. I think in theory it should be working but for some reason it is not. It prints the form correctly. However, when i press submit it simply reloads the form and there is no new information in the database tables. I've looked over it many many times and just can't figure out what it is. I'm sure i am just overlooking a simple php/mysql rule. I tried to add comments in so you would better understand what i was doing, let me know if you have any questions though.
    Also, there is a page before this in the process which is a simple form that sends a $_GET variable called players. It is simply a number to tell how many Player forms to print. I'm also having trouble validating that it always says its a non-numeric.
    PHP Code:
    <?php
    require("config.php");

    $validentry $_GET['players'];
                
            
    $db mysql_connect($dbhost$dbuser$dbpassword);
            
    mysql_select_db($dbdatabase$db);
            
            
    #Insert New Information
            
    if($_POST['submit']) 
            {
                
    #insert Information into Matches table
                
    $sql "INSERT INTO `Matches` ( `id` , `clan_id` , `map_id` , `opponent`, `our_score`, `op_score`, `dateposted` )
                        VALUES ('','" 
    $_POST['clan'] . "', '" $_POST['map'] . "', '" $_POST['opponent'] . "', '" $_POST['ourscore'] . "', '" $_POST['opponentscore'] . "', ' NOW( )')";
                    
                
    mysql_query($sql);
                
                
    #itterate through sets of players and insert into tables
                
    for($i=1$i<$validentry +1$i++)
                {    
                    
    $sql "INSERT INTO `Player` ( `id` , `user_id` , `clan_id` , `match_id`, `kills`, `deaths`, `roe` )
                            VALUES ('','" 
    $_POST["'id"$i "'"] . "', '" $_POST["'clan"$i "'"] . "', '" $matchid "', '" $_POST["'kills"$i "'"] . "', '" $_POST["'deaths".$i "'"] . "', '" $_POST["'roe".$i "'"] . "')";
                            
                    
    mysql_query($sql);            
                }
                
                
    header("Location: index.php?msg=1");
            }else 
            {
                require(
    "header.php");
                
                
    /*
                $db = mysql_connect($dbhost, $dbuser, $dbpassword);
                mysql_select_db($dbdatabase, $db);

                if(isset($_GET['players']) == TRUE) {
                    if(is_numeric($id) == FALSE) {
                        $error = 1;
                    }
        
                    if($error == 1) {
                        header("Location: " . $config_basedir);
                    }
                    else{
                        $validentry = $_GET['players'];
                    }
                }
                else{
                    $validentry = 0;
                }            

    */

        
    require("header.php");
    ?>

        <form name="signup" id="signup" method="post" action="#">
    <h3>Please Enter Match Info Below</h3>
     <table>
          <tr>
              <td colspan="2" class="labelcell"><label for="map">Map</label></td>
                <td class="fieldcell">
                     <select name="map" id="map" tabindex="5">
                     <!-- Itterate through map database and insert all maps -->
                    <?php
                        $sql 
    "SELECT AAMaps.*
                                FROM AAMaps;"
    ;
                        
    $result mysql_query($sql) or die("DB connect failed: " mysql_error());
                        
    $numrows mysql_num_rows($result);
                        if(
    $numrows == 0
                        {
                            echo 
    "<option value=''>No Maps In Database.</option>";
                        }
                        else 
                        {
                            
    #Display All maps from the database
                            
    while($row mysql_fetch_assoc($result)) 
                               {
                                echo 
    "<option value='" $row['id'] ."' >" $row['name'] . "</option>";
                            }
                        }
                    
    ?>
             
        
               </select>
            </td>
       </tr>
       <tr>  
           <!-- Basic Match Information -->
         <td colspan="2" class="labelcell"><label for="opponent">Clan ID</label></td>
         <td colspan="2" class="fieldcell"> <input type="text" name="clan" id="clan" tabindex="2" /></td>
       </tr>  
       <tr>  
         <td colspan="2" class="labelcell"><label for="opponent">Opponent</label></td>
         <td colspan="2" class="fieldcell"> <input type="text" name="opponent" id="opponent" tabindex="2" /></td>
       </tr>
       <tr>  
         <td colspan="2" class="labelcell"><label for="ourscore">Our Score</label></td>
         <td colspan="2" class="fieldcell"> <input type="text" name="ourscore" id="ourscore" tabindex="3" /></td>
       </tr>
       <tr>  
         <td colspan="2" class="labelcell"><label for="opponentscore">Opponent Score</label></td>
         <td colspan="2" class="fieldcell"> <input type="text" name="opponentscore" id="opponentscore" tabindex="4" /></td>
       </tr>
       <?php 
               
    #Take number from previous page and print that many form fields
               
    for($i=1$i<$validentry +1$i++)
            {
            
    ?>
                <tr>
                    <!-- Number players to make entering procss easier -->
                    <td><label for="players">Player #<?php echo $i?></label></td>
                </tr>
                <tr>
                     <td colspan="2" class="labelcell"><label for="players">Player ID</label></td>
                     <td colspan="2" class="fieldcell"> <input type="text" name="id<?php echo $i?>" id="id<?php echo $i?>" tabindex="2" />        </td>
                   </tr>
                   <tr>
                        <td colspan="2" class="labelcell"><label for="map">Kills</label></td>
                        <td colspan="2" class="fieldcell"> <input type="text" name="kills<?php echo $i?>" id="kills<?php echo $i?>" tabindex="2" /></td>
                   </tr>
                   <tr>  
                     <td colspan="2" class="labelcell"><label for="deaths">Deaths</label></td>
                      <td colspan="2" class="fieldcell"> <input type="text" name="deaths<?php echo $i?>" id="deaths<?php echo $i?>" tabindex="2" /></td>
                   </tr>
                   <tr>  
                       <td colspan="2" class="labelcell"><label for="roe">ROE</label></td>
                     <td colspan="2" class="fieldcell"> <input type="text" name="roe<?php echo $i?>" id="roe<?php echo $i?>" tabindex="3" /></td>
                   </tr>
       <?php
               
    #end for loop
            
    }
       
    ?>
                   <tr>  
                     <td colspan="4"><input class="button" type="submit" name="Submit" value="Submit" tabindex="8" /></td>
                  </tr>
         </table>
    </form>
    ?>

    <?php
        
    }
        require(
    "footer.php");
    ?>
    Last edited by bar338; Jun 19, 2008 at 15:53.

  2. #2
    SitePoint Addict
    Join Date
    May 2008
    Location
    Missouri, USA
    Posts
    273
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh here is the actual form if it helps to see it.
    Form

  3. #3
    SitePoint Addict lmasi02's Avatar
    Join Date
    Aug 2004
    Location
    Zambia
    Posts
    257
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i did something like this and used arrays and for loop. it worked well for me.
    Power of Knowledge

  4. #4
    SitePoint Addict silentcollision's Avatar
    Join Date
    Jun 2006
    Location
    New Zealand
    Posts
    388
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    if(isset($_POST['Submit'])) 
    Should work, although you should probably use $_SERVER['REQUEST_METHOD'] I think.

    By the way, you should really look into sanitising your data. You're currently vulnerable to SQL Injection.

  5. #5
    SitePoint Addict
    Join Date
    May 2008
    Location
    Missouri, USA
    Posts
    273
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ya i haven't done any validation yet. I tried to check some of the data but it was working for some reason:
    Also, there is a page before this in the process which is a simple form that sends a $_GET variable called players. It is simply a number to tell how many Player forms to print. I'm also having trouble validating that it always says its a non-numeric.
    if(isset($_POST['Submit']))


    Should work, although you should probably use $_SERVER['REQUEST_METHOD'] I think.
    I tried that and when ever it try to open the form it automatically redirected to the index like the form was already completed.

    Any suggestions on why this is not being inserted into the database?
    Last edited by bar338; Jun 19, 2008 at 15:56.

  6. #6
    SitePoint Addict lmasi02's Avatar
    Join Date
    Aug 2004
    Location
    Zambia
    Posts
    257
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try to look the code and hopefully you will get somthing.
    PHP Code:
    <?php 
        
    if(isset($_POST['submit']){
            for(
    $i 0$i 4$i++){
                
    $input1 input1[$i]
                
    $input2 input2[$i]
                
    $input3 input3[$i]
                
    //insert the datainto db
            
    }
        }
        
        echo 
    "<form  method=\"post\">";
        echo(
    "<select><option >select data</option></select>    <br>");
        for($
    1=$i <$i++){
            echo 
    "Player #".$i+"<br>";
            echo(
    "input 1<input type=\"text\"  name='input1[]' size=\"20\">    <br>");
            echo(
    "input 1<input type=\"text\"  name='input2[]' size=\"20\">    <br>");
            echo(
    "input 1<input type=\"text\"  name='input3[]' size=\"20\">    <br>");
        }
        echo 
    "<input name='submit' type='submit' id='submit' value='submit'>";
    ?>
    Power of Knowledge

  7. #7
    SitePoint Addict lmasi02's Avatar
    Join Date
    Aug 2004
    Location
    Zambia
    Posts
    257
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    what am trying to say is, you might need to have te for in submit close as well.
    Power of Knowledge

  8. #8
    SitePoint Addict
    Join Date
    May 2008
    Location
    Missouri, USA
    Posts
    273
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I just saw something else. I put the variable $matchid in my sql statment earlier b/c I wasn't sure what else to do and was going to come back and fix it. I need to have the id from the insert to Matches before I can insert to players so I can uphold the relationship. id is set to auto-increment. Is there any special way to do this? Or should i just call the most recent match (after inputting it) and grab the id that way?

    PHP Code:
                #insert Information into Matches table
                
    $sql "INSERT INTO `Matches` ( `id` , `clan_id` , `map_id` , `opponent`, `our_score`, `op_score`, `dateposted` )
                        VALUES ('','" 
    $_POST['clan'] . "', '" $_POST['map'] . "', '" $_POST['opponent'] . "', '" $_POST['ourscore'] . "', '" $_POST['opponentscore'] . "', ' NOW( )')";
                    
                
    mysql_query($sql);
                
                
    #itterate through sets of players and insert into tables
                
    for($i=1$i<$validentry +1$i++)
                {    
                    
    $sql "INSERT INTO `Player` ( `id` , `user_id` , `clan_id` , `match_id`, `kills`, `deaths`, `roe` )
                            VALUES ('','" 
    $_POST["'id"$i "'"] . "', '" $_POST["'clan"$i "'"] . "', '" $matchid "', '" $_POST["'kills"$i "'"] . "', '" $_POST["'deaths".$i "'"] . "', '" $_POST["'roe".$i "'"] . "')";
                            
                    
    mysql_query($sql);            
                } 

  9. #9
    SitePoint Guru
    Join Date
    Jul 2005
    Location
    Orlando
    Posts
    634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your submit button is named "Submit" with a capital 'S'. Your if statement is checking for a lower case S. Change one of them.

  10. #10
    SitePoint Zealot
    Join Date
    Jun 2008
    Location
    Australia
    Posts
    164
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    use array.....it will work....

    UFTimmy is right....try to use lowercase s in submit.....

  11. #11
    SitePoint Addict silentcollision's Avatar
    Join Date
    Jun 2006
    Location
    New Zealand
    Posts
    388
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I already mentioned that, and he said that it still didn't enter the data.

  12. #12
    SitePoint Addict
    Join Date
    May 2008
    Location
    Missouri, USA
    Posts
    273
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've got it working now, it was a combination of the submit button and 2 other small typos. Thanks for your help.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •