Justafriend Posted September 12, 2014 Share Posted September 12, 2014 Heres the code of the line im trying to trim $r = mysql_query("insert into points values ('','${v[0]}', ${v[1]}, '$At')")or die (mysql_error()); mysql_query("insert ignore into toc values ('','$p1', '$At'),('','$p2', '$At'), ('','$p3', '$At'), ('','$p23', '$At')") or die (mysql_error()); if (!$r) die (mysql_error()); the line pertaining to the question is the insert into points . what that part of the code does istakes this preformatted text START POINTSMrsSpacely 1nanatravels 1Trust_Me_honey 2moratala 3xxBIGDAVExx 3Spring_Chicken 5tomrarose 5sitdownnplay 7_Antoinette_ 11Melyn 25mokan40 30dj_lia 10STOP POINTS And inserts it into the points table. Which works fine but if you put a space after Stop points or before any name it comes back with an error about check spaces, and doesnt post the whole thing what i need to do is trim all the spaces from the text before it tries to go into the table Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted September 12, 2014 Share Posted September 12, 2014 Then you should post your code where you create these values, are they from a form like $_POST? $_POST = array_map('trim', $_POST); Now the array named $_POST would all be trimmed, or in a foreach loop. foreach($_POST as $key => $val){ $_POST[$key] = trim($val); } Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted September 12, 2014 Share Posted September 12, 2014 Besides using outdated mysql_* versus PDO or mysqli_*... You should always sanitize/filter/escape all data saving to your database. Even doing checks for the proper data you expect. 1 Quote Link to comment Share on other sites More sharing options...
Justafriend Posted September 14, 2014 Author Share Posted September 14, 2014 Sorry for not getting back sooner but work had me on the road the form I am using takes a standard text area, and puts it into the database the trimming option comes when sometimes the hosts for instance have a space at the end of stop points or have a space before their player name here is the input . Nojoks's Tourney Bracket Tool Version 1.2.1.84 Tournament: ********jammin2-3 format Date: 09/10/2014 Day: Wednesday Scheduled Start: 3:00 PM PST Actual Start: 6:00:16 PM Closed: 6:12:32 PM Host: Habs4Stanley (lookingfor25) Number of Players: 21 1st place: safak 1st place email: 2nd place: DancingDice 2nd place email: 3rd place: _PITBULL_ 3rd place email: START POINTS Abradant_Eye_21 1 dorado11 1 IgotaBigSet 1 JimmyConway_ 1 MrsSpacely 1 Shambhala 1 TM7_crazykarma1 1 TM7_LuckyRoller 1 TM7_SassyChick 1 UBG_Steely1 1 chacal49 4 karlsbad70 4 PRO_VBL 4 UBG_Angel_D_8 4 UBG_Arielle 4 Benchthis 6 BigCube 7 havingtrouble 7 _PITBULL_ 15 DancingDice 22 safak 31 lookingfor25 10 STOP POINTS and this is the from top of where the code takes the result to the bottom of the form function not_2($var) { /*echo("<br><br>"); print_r($var);*/ if (count($var) != 2) return false; else return true; } if (isset($_POST["asfile"])) { //print_r($_POST); echo("<br>"); $file = $_POST["file"]; $points = strstr($file, "START POINTS"); //print_r($points); $_POST["Points"] = $points; $file = explode("\n", $file); foreach ($file as $k) { $lines[] = explode(":", $k, 2); } $lines = array_filter($lines, "not_2"); //print_r($lines); foreach ($lines as $value) { $value[0] = trim($value[0]); $value[1] = trim($value[1]); switch (strtolower($value[0])) { case "date": $_POST["At"] = $value[1]; break; case "scheduled start": $_POST["AtTime"] = $value[1]; break; case "host": $_POST["Host"] = $value[1]; break; case "number of players": $_POST["PC"] = $value[1]; break; case "1st place": $_POST["p1"] = $value[1]; break; case "1st place email": $_POST["e1"] = $value[1]; break; case "2nd place": $_POST["p2"] = $value[1]; break; case "2nd place email": $_POST["e2"] = $value[1]; break; case "3rd place": if (isset($_POST["p3"])) { $_POST["p23"] = $value[1]; } else { $_POST["p3"] = $value[1]; } break; case "3rd place email": if (isset($_POST["e3"])) $_POST["e23"] = $value[1]; else $_POST["e3"] = $value[1]; break; default: break; } $_POST["send"] = 1; } $vars = array("At", "AtTime", "Host", "PC", "p1", "e1", "p2", "e2", "p3", "e3", "p23", "e23"); foreach ($vars as $k) { if (!isset($_POST[$k])) $_POST[$k] = ""; } /* echo("Here:<br>"); print_r($_POST); echo("<br>");*/ } if (isset ($_POST["send"])) { foreach ($_POST as $k=>$v) { $$k = $v; } //$d = explode(" ", $At); $dat = explode ("/",$At); if (count($dat)!=3) die("Bad date format."); $tim = explode(":", $AtTime); if (count($tim)<2) die("Bad time format."); $plus = 0; if (strstr(strtoupper($tim[1]), "PM")) { $plus = 12; } $h = $tim[0]; $m = explode(" ", $tim[1]); $m = $m[0]; $h+= $plus; $At = "${dat[2]}-${dat[0]}-${dat[1]} $h:$m:00"; // echo("$At"); // $d = strtotime($At); //$At = date("Y-m-d H:i:s", $d); //echo("At: $At<br/>"); //echo("At: $At<br/>"); mysql_query("insert into tourne_report values ('$Host', '$At', $PC, '$p1', '$e1', '$p2', '$e2', '$p3', '$e3', '$p23', '$e23')") or die (mysql_error()); $Points = explode("\n", $Points); mysql_query("insert ignore into emails values ('$p1', '$e1'),('$p2', '$e2'), ('$p3', '$e3'), ('$p23', '$e23')") or die (mysql_error()); foreach ($Points as $v) { if ($v == "") continue; $v = explode (" ", $v); if (count($v) != 2) die("Incorrect values in POINTS input (check spaces!)<br>"); if (strstr(strtolower($v[1]), 'points')) continue; $r = mysql_query("insert into points values ('${v[0]}', ${v[1]}, '$At') on duplicate key update Points = Points + ${v[1]}"); mysql_query("insert ignore into toc values ('$p1', '$At'),('$p2', '$At'), ('$p3', '$At'), ('$p23', '$At')") or die (mysql_error()); if (!$r) die (mysql_error()); } //print_r($Points); } ?> <!-- <form method="POST" action="<?echo ($sn);?>"> <table> <tr> <td> Host:</td> <td><input type = "text" name = "Host" /></td><td>Host name</td> </tr> <!--<tr> <td> Nick:</td> <td> <input type = "text" name = "Nick" /></td><td>Nick //What is it?</td> --> <!-- <tr> <td> Date: </td> <td><input type = "text" name = "At" /></td><td>In "MM/DD/YYYY" format</td> <tr> <td> Time: </td> <td><input type = "text" name = "AtTime" /></td><td>In "HH:MM A" format where A is either AM or PM</td> <tr> <td> PlayersCount:</td> <td> <input type = "text" name = "PC" /></td><td>Integer value</td> <tr> <td> Winner:</td> <td> <input type = "text" name = "p1" /></td><td>Winner name</td> <tr> <td> Winner's email: </td> <td><input type = "text" name = "e1" /></td><td>Winner email</td> <tr> <td> 2nd place:</td> <td> <input type = "text" name = "p2" /></td><td>2nd name</td> <tr> <td> 2nd place email:</td> <td> <input type = "text" name = "e2" /></td><td>2nd email</td> <tr> <td> 3rd place:</td> <td> <input type = "text" name = "p3" /></td><td>3rd name</td> <tr> <td> 3rd place email:</td> <td> <input type = "text" name = "e3" /></td><td>3rd email</td> <tr> <td> 2_3rd place:</td> <td> <input type = "text" name = "p23" /></td><td>Second third place :-)</td> <tr> <td> 2_3rd place email:</td> <td> <input type = "text" name = "e23" /></td><td>Second third place email</td> </table> Points:<br> <textarea name="Points" cols="30" rows="7"></textarea> <br/> <input type = "submit" name="send" value="Send results"/> </form> --> <img src = "/Posting%20pOints.jpg" /> <form method="POST" action="<?echo ($sn);?>"> File contents:<br/> <textarea name="file" cols="100" rows="20"></textarea><br/> <input type="submit" name="asfile" value="Send a file" /> </form> Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted September 14, 2014 Share Posted September 14, 2014 That script is exploding many times and sometimes by spaces. There won't be an easy one time and all values get trimmed. Your best bet is to just wrap any variables directly before the insert query with trim() Additionally can also use mysql_real_escape_string() at the same time. I'll do the first one as an example, and didn't look for blank values or certain data types $c_Host = mysql_real_escape_string(trim($Host)); $c_At = mysql_real_escape_string(trim($At)); $c_PC = mysql_real_escape_string(trim($PC)); $c_p1 = mysql_real_escape_string(trim($p1)); $c_e1 = mysql_real_escape_string(trim($e1)); $c_p2 = mysql_real_escape_string(trim($p2)); $c_e2 = mysql_real_escape_string(trim($e2)); $c_p3 = mysql_real_escape_string(trim($p3)); $c_e3 = mysql_real_escape_string(trim($e3)); $c_p23 = mysql_real_escape_string(trim($p23)); $c_e23 = mysql_real_escape_string(trim($e23)); mysql_query("insert into tourne_report values ('$c_Host', '$c_At', $c_PC, '$c_p1', '$c_e1', '$c_p2', '$c_e2', '$c_p3', '$c_e3', '$c_p23', '$c_e23')") or die (mysql_error()); Quote Link to comment Share on other sites More sharing options...
Justafriend Posted September 14, 2014 Author Share Posted September 14, 2014 (edited) the variables you have put arent the problem the problem is the points input the part that comes from this section START POINTS Abradant_Eye_21 1 dorado11 1 IgotaBigSet 1 JimmyConway_ 1 MrsSpacely 1 Shambhala 1 TM7_crazykarma1 1 TM7_LuckyRoller 1 TM7_SassyChick 1 UBG_Steely1 1 chacal49 4 karlsbad70 4 PRO_VBL 4 UBG_Angel_D_8 4 UBG_Arielle 4 Benchthis 6 BigCube 7 havingtrouble 7 _PITBULL_ 15 DancingDice 22 safak 31 lookingfor25 10 STOP POINTS if stop points has a space after it , That causes a issue and says check spaces or if lookingfor25 has a space before it same thing Which why i just pasted the section relevant to points innitially but i do appreciate all your help. Edited September 14, 2014 by Justafriend Quote Link to comment Share on other sites More sharing options...
Barand Posted September 14, 2014 Share Posted September 14, 2014 (edited) Does this help? $points=array(); $arr = array_map('trim', explode("\n", $file)); foreach ($arr as $line) { list($n, $p) = sscanf($line, '%s %d'); if ($p) $points[$n] = $p; } arsort($points); echo '<pre>',print_r($points, true),'</pre>'; Edited September 14, 2014 by Barand Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted September 15, 2014 Share Posted September 15, 2014 (edited) So you are placing a pile of text into a form and trying to get all the relevant data. In some of those lines it may or may not contain multiple whitespace. This is the first place where I see you grab all the lines, so try this here Replaces any whitespace if more than one with a single whitespace. foreach ($file as $k) { $k = preg_replace('/\s+/', ' ',trim($k)); If still have problem areas add a trim as well. Is some questionable statements in this script if ($v == "") continue; what else would it do? $v = explode (" ", $v); now $v variable is $v array if (count($v) != 2) die("Incorrect values in POINTS input (check spaces!)<br>"); is it ever not 2? what about the rest of data when it dies? if (strstr(strtolower($v[1]), 'points')) continue; so checking if $v[1] equals points, will it ever not be? maybe should be checking all the $v array for what you want? lets say want to find if $v[0] is actually a number, then that is players points, otherwise is zero is_numeric() for numbers including decimals ctype_digit() for just numbers if(ctype_digit(trim($v[0]))){ $player_points = trim($v[0]); } else { $player_points = 0; } Maybe just grab the number from $v array and make it points if not sure is in 1st position? Be better to know exactly where and what is needed, but here it is anyway. foreach($v as $val){ $val = trim($val); if(ctype_digit($val)){ $player_points = $val; } else { $player_points = 0;//or handle this another way } } Edited September 15, 2014 by QuickOldCar Quote Link to comment Share on other sites More sharing options...
Justafriend Posted September 15, 2014 Author Share Posted September 15, 2014 ok gonna try it but to explain a little clearer a few years back i tried get a freelancer and at the time i ididnt have the time or patience to try to learn, so when the guy offered me to do the job cheap i figured php mysql if it does the job im laughing. (especially cause this is a volunteer kinda thing where we as hosts get 0 for it so everything is a loss that we spend) now i have spent the last bit of time trying to learn it cause i am spending alot of time going through the database and finding error after error. So now that brings me to last few weeks mainly because with my new job im gonna be on the road alot and wont be able to always fix it right away. I know im gonna have to totallly reqrite this script in either mysqli or pdo , but right now im just trying to put patches over the holes till i can get the new code in place. Now the sections of the report the only 2 spots that seem to have any issues is the last person in points the host of the tournaments player name who gets 10 pts cause that is manually entered in the tool and the space that sometimes shows itself when you copy and paste.. all of the other names are right cause we right click on their name and assignthem which automatically puts the names in the tool. at the end of the tournament the tool displays a popup in the formatting as above. from that tool when entered in the text area Posted Yesterday, 06:53 AM Sorry for not getting back sooner but work had me on the road the form I am using takes a standard text area, and puts it into the database the trimming option comes when sometimes the hosts for instance have a space at the end of stop points or have a space before their player name here is the input . Nojoks's Tourney Bracket Tool Version 1.2.1.84 Tournament: ********jammin2-3 format Date: 09/10/2014 Day: Wednesday Scheduled Start: 3:00 PM PST Actual Start: 6:00:16 PM Closed: 6:12:32 PM Host: Habs4Stanley (lookingfor25) Number of Players: 21 1st place: safak 1st place email: 2nd place: DancingDice 2nd place email: 3rd place: _PITBULL_ 3rd place email: it pulls the host date time and 1st 2nd 3rd place finishers (sometimes 2)into the tourney table date time (so i can just change the code to grab for current month) 1st 2nd and 3rd names are also put in toc table (tourney of chapions qualifiers) and emails is put into a email list so we can send emails to toc qualifiers then this section START POINTS Abradant_Eye_21 1 dorado11 1 IgotaBigSet 1 JimmyConway_ 1 MrsSpacely 1 Shambhala 1 TM7_crazykarma1 1 TM7_LuckyRoller 1 TM7_SassyChick 1 UBG_Steely1 1 chacal49 4 karlsbad70 4 PRO_VBL 4 UBG_Angel_D_8 4 UBG_Arielle 4 Benchthis 6 BigCube 7 havingtrouble 7 _PITBULL_ 15 DancingDice 22 safak 31 lookingfor25 10 STOP POINTS is placed in points table with date so i can get the points in a list with all points received in current month now to answer your questions keeping in mind im trying to learn this script as i work with it but the questions what else would it do i think that was a way that he could let it only post the top section in case something messed up keeping in mind im just looking at the code and learning now v variable is an array. i think thats what is was supposed to be inorder to insert repeatedly for that section of the box is it ever not 2? what about the rest of data when it dies? thiink this si where my issue is it kills the script after posting part of the points so if a host takes the format i listed above and has a break in it they see check spaces they look find it. now some players are double boints etc if (strstr(strtolower($v[1]), 'points')) continue; so checking if $v[1] equals points, will it ever not be? maybe should be checking all the $v array for what you want? think this is where he gets it to die at stop points cause stop points wont ever have points again if im wrong i am sorry but trying to learn as i go but i do appreciate the help Quote Link to comment Share on other sites More sharing options...
Justafriend Posted September 15, 2014 Author Share Posted September 15, 2014 Does this help? $points=array(); $arr = array_map('trim', explode("\n", $file)); foreach ($arr as $line) { list($n, $p) = sscanf($line, '%s %d'); if ($p) $points[$n] = $p; } arsort($points); echo '<pre>',print_r($points, true),'</pre>'; it helps to show me where the name has a space but then have to manually put it in to the tables afterwards here is what i get if i add a space before lookingfor25 Array ( [safak] => 31 [DancingDice] => 22 [_PITBULL_] => 15 [lookingfor25] => 10 [Date:] => 9 [bigCube] => 7 [havingtrouble] => 7 [Closed:] => 6 [benchthis] => 6 [chacal49] => 4 [karlsbad70] => 4 [uBG_Angel_D_8] => 4 [uBG_Arielle] => 4 [PRO_VBL] => 4 [uBG_Steely1] => 1 [igotaBigSet] => 1 [dorado11] => 1 [Abradant_Eye_21] => 1 [JimmyConway_] => 1 [MrsSpacely] => 1 [TM7_LuckyRoller] => 1 [TM7_crazykarma1] => 1 [shambhala] => 1 [TM7_SassyChick] => 1 ) Incorrect values in POINTS input (check spaces!) but it posts all the names there Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted September 15, 2014 Share Posted September 15, 2014 Incorrect values in POINTS input (check spaces!) you would need to post your current code responsible for forming and running the insert into points values ..., query, as it's likely it isn't using the $points array that Barand's code produces. Quote Link to comment Share on other sites More sharing options...
Justafriend Posted September 15, 2014 Author Share Posted September 15, 2014 i made a change into the way the date is entered as a temp solution to fix another issue i was having with dates and times from around the worrld so i am using a timestamp now instead of the date but here is the section that runs points as you can see i places his code before the die command for the spaces is as well as the list that posts the array is skipping the name with the space $Points = explode("\n", $Points); mysql_query("DELETE FROM emails WHERE email = '' OR email IS NULL"); mysql_query("insert ignore into emails values ('$p1', '$e1'),('$p2', '$e2'), ('$p3', '$e3'), ('$p23', '$e23')") or die (mysql_error()); $points=array(); $arr = array_map('trim', explode("\n", $file)); foreach ($arr as $line) { list($n, $p) = sscanf($line, '%s %d'); if ($p) $points[$n] = $p; } arsort($points); echo '<pre>',print_r($points, true),'</pre>'; foreach ($Points as $v) { if ($v == "") continue; $v = explode (" ", $v); if (count($v) != 2) die("Incorrect values in POINTS input (check spaces!)<br>"); if (strstr(strtolower($v[1]), 'points')) continue; $r = mysql_query("insert into points values ('','${v[0]}', ${v[1]}, NOW())"); Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted September 15, 2014 Share Posted September 15, 2014 you didn't even bother to try and change your code to use the $points data that Barand's code extracts. this is a copy/paste fail on your part. i suggest that you look at the print_r() output from his code so that you know what format the name/point value is in, then remove all your existing code for the insert into points values ... query, and actually WRITE some new code that uses the $points array, keys/value pairs, to form and run the insert into points values ... query. Quote Link to comment Share on other sites More sharing options...
Justafriend Posted September 16, 2014 Author Share Posted September 16, 2014 that was what i was saying his print readout was knocking out the names with the space in front of it i was trying to see if it would work with just the print out before i started with the rest of the code i have been tackling many different things in the coding i just used the code i had posted and put it into it before i started to tweak around it. Quote Link to comment Share on other sites More sharing options...
Justafriend Posted September 16, 2014 Author Share Posted September 16, 2014 i have6 php files each one im working a section of the kinks out such as dropping the date and time input as the date formats vary so changed it over to a timestamp and did it with all my tables as well as implementing a new piece of the code that drops empty fields before the script posts new to it . long and short is i didnt want to touch the file that he had done that with and then find out after it was a change i made that caused it to fail Quote Link to comment Share on other sites More sharing options...
Justafriend Posted September 16, 2014 Author Share Posted September 16, 2014 upon looking closer i see now the names are reordered and i do apologize for that i was comparing the bottom of the file and didnt see lookingfor25 i have since looked back and saw it up further in the list Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.