Web Analytics Made Easy -
StatCounter how to sub strin a posted report before sending to my DB - CodingForum

Announcement

Collapse
No announcement yet.

how to sub strin a posted report before sending to my DB

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • how to sub strin a posted report before sending to my DB

    Hi I have been working on a problem in my php and each time I think I have finally worked it out I realise there is another issue to think about so now I throw it out to you guys o see if your experience can speed my work for me ,
    basically what I have and am trying to do is:
    on my site I have a very simple form in which my users will submit a report of statistics based on points awarded from playing in a tournament,
    the report looks like my quote;

    Nojoks's Tourney Bracket Tool Version 1.2.1.84
    Tournament: 3/5 Backgammon 1:00pm
    Date: 06/03/2016
    Day: Friday
    Scheduled Start: 1.00pm PST
    Actual Start: 21:01:10
    Closed: 21:11:10
    Host: Waiter ()
    Number of Players: 13

    1st place: MC_Vicky
    1st place email: [email protected]
    2nd place: TM7_CharrM5_
    2nd place email: [email protected]
    3rd place: devinm21
    3rd place email:
    3rd place: poppop
    3rd place email: [email protected]

    START POINTS
    Alberto 5
    Brs_Edu2 5
    CarminhaCCR 5
    Chessykatz 5
    da_terminator_x 5
    IFeelADouble6 5
    MRC_Melisa_ 5
    UBG_Angel_D_8 5
    victor54 5
    devinm21 60
    poppop 60
    TM7_CharrM5_ 90
    MC_Vicky 120
    STOP POINTS

    what I need to do with this report is simple if you know how HAHA,
    in the report where is says "START POINTS" after that is a list of player names and the awarded points per player
    I need to break this down to insert this into my DB table
    so I need a player name in the first column the corresponding points in the second column etc etc,
    I have been looking at the substr method on php.net however the issue there is the player names are not known until the posts are made so I can not determine how long each substr will be in advance,
    so I am asking if anyone can help with how I can break this down I can use a substr to get to the start o the first player name but how do I figure out where to stop the string??

  • #2
    You would use either explode() or preg_split() to break the string at the white-space character(s) between the player name and the points value.
    Finding out HOW to do something is called research, i.e. keep searching until you find the answer. After you attempt to do something and cannot solve a problem with it yourself, would be when you ask others for help.

    Comment


    • #3
      there it is spot on the mark with the explode() function thank you so much

      Comment


      • #4
        can anyone please tell me what I have managed to do wrong here in this code block please,
        if I take out the query section and in comment the echo's under there I get the page working correct so I guess I messed up the sql insert,
        when I run this code in my page I get this error,

        "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '5"
        and a part post displayed back

        <?PHP
        //explode the post into an array
        $points = $_POST['points'];
        $player_name = explode(" ", $points);
        //get the INT points value
        $player_points = str_split($player_name, -3);
        // count the array length
        $player_count = count($player_name);
        for($i = 0; $i<$player_count; $i++) {
        $query = "INSERT INTO bg_points (
        player_name, points
        ) VALUES (
        '{$player_name}', {$points}
        )";
        $result = mysql_query($query, $connection);
        if ($result) {
        // Success!
        echo $player_name[$i];
        echo $player_points;
        } else {
        // Display error message.
        echo "<p>Failed To Post Your Points Please Correct The Errors And Try Again.</p>";
        echo "<p>" . mysql_error() . "</p>";
        }

        //echo $player_name[$i] . "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" . $player_points;
        //echo "<br>";

        }
        ?>

        Comment


        • #5
          You are using mysql calls that were deleted from PHP back in December 2015. You should be using either mySQLi or PDO (both of which were introduced as replacements in July 2004).

          Also you are not validating your inputs and making copies of potentially invalid data into additional fields for no reason.
          Stephen
          Learn Modern JavaScript - http://javascriptexample.net/
          Helping others to solve their computer problem at http://www.felgall.com/

          Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

          Comment

          Working...
          X