0

I'm trying to create a player edit system for an admin section of a football website. The process goes as follows:

Once a coach has logged in on 'coaches.php', they can then choose what coaching session they want to look at via dropdown, which then populates the 'player' dropdown (done via js below)

form on coach-home.php

            <form id="form1" name="form1" method="post" action="coach-player.php">
        <label>Activity :</label>
        <select name="activity" class="activity">
            <option selected="selected">--Select Activity Group--</option>
                <?php
                include('dbconnect.php');
                $sql=mysql_query("select activity from coaches where username='$coach'");
                    while($row=mysql_fetch_array($sql))
                  {
                      $activity2=explode(",",$row["activity"]);
                      foreach ($activity2 as $activity)
                     echo '<option value="'.$activity.'">'.$activity.'</option>';
                 } ?>
            </select> <br/><br/>
            <label>Player :</label> <select name="username" class="username">
            <option selected="selected">--Select Player--</option>
        </select>

       <input type="text" name="pid" class="pid" id="pid" value="<?php echo $pid; ?>" />
        <input type="submit" name="button" id="button" value="Log In" />

    </form>

JS request on coach-home.php

<script type="text/javascript">
$(document).ready(function()
{
$(".activity").click(function()
{
var activity=$(this).val();
var dataString = 'activity='+ activity;

$.ajax
({
type: "GET",
url: "username.php",
data: dataString,
cache: true,
success: function(html)
{
$(".username").html(html);
} 
});

});
});
</script>

username.php

<?php
if($_GET['activity'])
{
$activity=$_GET['activity'];
$sql=mysql_query("SELECT pid, username FROM stats WHERE activity='$activity'");

while($row=mysql_fetch_array($sql))
{
$pid=$row['pid'];
$username=$row['username'];
echo '<option value="'.$username.'">'.$username.'</option>';

}
}

?>

Once all of this is done, the coach submits the form, taking them to coachplayer.php. This is where the problem begins.

coachplayer.php is a template page, with empty fields filled with echo's, to echo the player details where necessary. A query runs to get the id of the selected player, bring up their details and fill the page. Instead, however, it echos what usually comes up if the query cannot find a matching result via $playerCount as shown below, saying "Player doesn't exist".

coach-player.php Query

<?php 
// Check to see the URL variable is set and that it exists in the database
if (isset($_GET['username'])) {
    // Connect to the MySQL database  
    $puser = preg_replace('#[^0-9]#i', '', $_GET['username']); 
    // Use this var to check to see if this ID exists, if yes then get the product 
    // details, if no then exit this script and give message why
    $sql = mysql_query("SELECT * FROM stats WHERE username='$puser' LIMIT 1");
    $productCount = mysql_num_rows($sql); // count the output amount
    if ($productCount > 0) {
        // get all the product details
        while($row = mysql_fetch_array($sql)){ 
             $username = $row["username"];
             $pid = $row["pid"];
             $position = $row["position"];
             $activity = $row["activity"];
             $agegroup = $row["agegroup"];
             $goals = $row["goals"];
             $assists = $row["assists"];
             $cleans = $row["cleans"];
             $motm = $row["motm"];
                      }

    } else {
        echo "That player does not exist.";
        exit();
    }

} else {
    echo "Data to render this page is missing.";
    exit();
}
?>

The issue here is that whilst it is defined in username.php, the pid does not get sent over and saved when the rest of the form on coach-home sends. I have tried changing from GET to POST with no avail. I have also just tried using the 'username' instead of 'pid' but I get "That player does not exist."; - meaning no variables outside of the ajax request is sending.

What is it that needs to be altered to save and post the data mentioned?

9
  • You should not be using a click event on a select element. Jan 4, 2015 at 19:50
  • where is the code of 'ajax_city.php' ? did you mean that it is the 'username.php' page but its a typo here? Jan 4, 2015 at 19:52
  • @AramTchekrekjian ah good spot, that was indeed a typo on here. Corrected now.
    – JioFreed
    Jan 4, 2015 at 19:56
  • @epascarello I thought the same at first, but after reading the tutorials (which is pretty much the extent of my knowledge w/ this) it seemed like the best way to link the two select boxes. Do you have a better method?
    – JioFreed
    Jan 4, 2015 at 19:57
  • did you try to var_dump($sql) in 'coach-player.php' and run the query directly within mysql ? Jan 4, 2015 at 20:03

2 Answers 2

0

Looking at your code the $username and $pid variables are not being passed to either coach-home.php or coach-player.php, thus when you try to write to the database the parameter $_GET['username'] or $_GET['pid'] is set (because you have provided an input field in your form), but it has no value! and thus there is no player that exists with an empty pid or username.

Also note that in the form you have specified the method as post, but in the php you are referencing the get hash. If you submit by post you access variables with $_POST, submit with get you access with $_GET.

My suggestion is to use the session hash to store the username and pid of the user.

When the user logs in:

$_SESSION['username'] = 'jonnysmith'
$_SESSION['pid'] = '45'

This will mean when you initiate the database query you will just reference the session value instead of the get value for the parameter.

Delete your input field for username/pid in the form.

Call session_start(); in your config.php file to enable the session hash.

Call session_destroy(); when the user logs out to clear the session hash.

Also you will need to logout and log back in for the changes to take effect and the value of username/pid to be stored in the session hash.

Happy hunting!

4
  • Thanks for this idea though I'm not sure this will work. There is already a user logged in - the coach - who then selects a name from the dropdown menu to edit. Wouldn't this mean that SESSION wouldn't work here as it's already being used for the coach's login.
    – JioFreed
    Jan 4, 2015 at 20:08
  • Ok so the pid/username is that of the user to be edited, not of the coach themselves?
    – David
    Jan 4, 2015 at 20:09
  • My mistake, I suggest then that you echo the value of $puser as a sanity check. Then look at your database and confirm that this does actually match. - also note my edit that the hash and the method need to be the same. (in the form you have specified the method as post, but in the php you are referencing the get hash. If you submit by post you access variables with $_POST, submit with get you access with $_GET.)
    – David
    Jan 4, 2015 at 20:13
  • Precisely! Otherwise your answer would be a great workaround.
    – JioFreed
    Jan 4, 2015 at 20:14
0

Your regex is actually replacing your whole username variable with ''

Based on your comment, I've done a test match with the name 'Radamel Falcao' and echo-ed $puser and I got empty string, so apparently, this regex is your problem.

$puser = preg_replace('#[^0-9]#i', '', $_GET['username']);

2
  • I don't see what is wrong with the regex, I followed a tutorial on it pretty much word for word... also, surely the fact that even on coach-home - before it reaches the regex - the values are blank when trying to call them outside of the ajax request/select field. What do you suggest?
    – JioFreed
    Jan 4, 2015 at 21:00
  • Try again to put the method to get on the 'coach-home.php', then var_dump the 'username' on the first line of your 'coach-player.php' Jan 5, 2015 at 0:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.