SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2001
    Location
    North East, England
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help with php, mysql

    Hi, I've been working with this for 3 days and realy need some help. Basicly it is part of a content management script where it is meant to select a players details from a db and alow you to edit them. The part where I have problems is where I update the new details.

    I am new to php but from what I can gather my main problem is the select box which alows you to choose a team. If I view the source code on the table the team select box option's values are the first letter of team. i.e. if the team name is Liverpool then the value is 'L'. I don't know why it is getting a string value when in the db table it is an integer. I hope that makes sense. Anyway, here is the code.
    Thanks
    Lee

    PHP Code:
    <!doctype html public "-//W3C//DTD HTML 4.0 //EN"> 
    <html>
    <head>
           <title>Edit Player</title>
    </head>
    <body>

    <h1 align=center>Edit Players Details</h1>
    <hr><br>

    <?php

       $dbcnx 
    mysql_connect("localhost""michaelsmith""linda") or
       die(
    "<p>Unable to connect to the database at this time. " .
       
    mysql_error(). "</p>");
       
    mysql_select_db("football");

       if(
    $submit): // Players Details have been updated
       
       
    $sql "update player set " .
       
    "player_name='$player_name', " .
       
    "position='$position', " .
       
    "value='$value', " .
       
    "tid='$tid' where id='$id'";
       
       if(
    mysql_query($sql)){
           echo(
    "<p>Players details updated</p>");
       }
       else{
           echo(
    "<p>Error updating players details: " .
           
    mysql_error() . "</p>");
       }
       
       
       
    ?>

    <p><a href="player.html">Back to player menu</a></p>

    <?php

      
    else: // Allow user to edit the author with id=$id
      
         
    $ok1 mysql_query("select player_name, position, value, tid, team from " .
         
    "player, team where player.id=\"$id\" and tid=team.id");
         
         if(!
    $ok1){
             echo(
    "<p>Error fetching player details: </p>" .
             
    mysql_error() . "</p>");
             exit();
         }
         
         
    $player mysql_fetch_array($ok1);
         
    $player_name=$player["player_name"];
         
    $position=$player["position"];
         
    $value=$player["value"];
         
    $tid=$player["tid"];
         
    $team=$player["team"];
         
         
    // add slashes to the database values for use as HTML atributes
         
    $player_name addslashes($player_name);
         
    $position addslashes($position);
         
    $value addslashes($value);
         
    $tid addslashes($tid);
         
    $team addslashes($team);
         
    ?>


    <FORM ACTION="<?php echo($PHP_SELF);?>" METHOD=POST>
    Edit Players Name:
    <INPUT TYPE="text" NAME="player_name" VALUE="<?php echo($player_name);?>" SIZE=20 MAXLENGTH=20><br>
    Edit Players Position:
    <INPUT TYPE="text" NAME="position" VALUE="<?php echo($position);?>" SIZE=8 MAXSIZE=8>

    <SELECT NAME="position" SIZE=1>
    <OPTION SELECTED VALUE="">Select One
    <OPTION VALUE="Goal Keeper">Goal Keeper
    <OPTION VALUE="Defender">Defender
    <OPTION VALUE="Midfielder">Midfielder
    <OPTION VALUE="Striker">Striker

    </SELECT><br>
           

    Edit Player Value: 
    <INPUT TYPE="text" NAME="value" VALUE="<?php echo($value);?>" SIZE=4 MAXLENGTH=4>Million<br>
    Edit Players Team:
    <INPUT TYPE="text" NAME="tid" VALUE="<?php echo($team);?>" SIZE=20 MAXLENGTH=30>

    <SELECT NAME="tid" SIZE=1>
    <OPTION SELECTED VALUE="<?php echo($tid);?>">Select A Team


    <?php

       $teams 
    mysql_query("select * from team");

       while(
    $team mysql_fetch_array($teams)){
           
    $team=$team["team"];
           
    $tid=$team["id"];
           echo(
    "<OPTION VALUE='$tid'>$team\n");
       }

    ?>

    </SELECT>

    <INPUT TYPE="hidden" NAME="id" VALUE="<?php echo($id);?>"><br><hr>
    <INPUT TYPE="hidden" NAME="tid" VALUE="<?php echo($tid);?>"><br>
    <INPUT TYPE="submit" NAME="submit" VALUE="UPDATE PLAYER">
    </FORM>

    <p><a href="player.html">Back to player menu</a></p>

    <?php endif ?>

    </body>
    </html>

  2. #2
    SitePoint Member
    Join Date
    Sep 2001
    Location
    Clifton Park, NY
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,
    The reason for this is right here:


    $teams = mysql_query("select * from team");

    while($team = mysql_fetch_array($teams)){
    $team=$team["team"]; <--- THIS LINE
    $tid=$team["id"];
    echo("<OPTION VALUE='$tid'>$team\n");
    }


    You are redefining $team when you are already using team as the array of the info extracted from your query. If you change the variablename to $team2 for example, then it should work correctly.

    -John Reyes
    Tech Support
    Http://HostRocket.Com
    The Future of Managed Hosting

  3. #3
    SitePoint Enthusiast
    Join Date
    Aug 2001
    Location
    North East, England
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks a lot I realy apreciciate it, and it works fine now.

    It's just the way that i have spent 3 days chewing on with this when it was something so staightforward.

    Still, that's one mistake I am not likely to make again. (Yeah right!)


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
  •