PHP results list

Discussion in 'PHP' started by kostas11, Dec 12, 2007.

  1. Brewster

    Brewster Active Member

    Messages:
    489
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    60
    #21
    I think that 'SET' is only used when updating a record - I may be wrong though.

    $sql = "insert into results set username = \"" . addslashes(strip_tags(htmlspecialchars($_POST["username"]))) . "\", whattime = \"" . time() . "\"";
    PHP:
    I replaced the statement with this:

    $sql = "insert into results (username,whattime) VALUES ('" . mysql_real_escape_string($_POST["username"]) . "','" . time() . "')";
    PHP:
    I also put some more error checking in so you can see where the error comes from.

    Brew
     
    Brewster, Dec 13, 2007 IP
  2. Gawk

    Gawk Peon

    Messages:
    427
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    0
    #22
    Using SET on insert's is valid and extremely useful, the insert string is far easier to read and debug. Try it sometime.

    Thanks for putting in some debug although I notice you removed my cleansing of the $_POST["username"], I always scrub clean anything a user sends ;)
     
    Gawk, Dec 13, 2007 IP
  3. kostas11

    kostas11 Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #23
    YOU ARE A GOD. It WORKS!

    One final question how can I arrange the list so that the latest entry appears at the bottom of the list and the earliest at the top?

    oh and how do I delete all the current usernames because they were used just for testing

    I will use all the links (of your company etc) in the website. Thank you
     
    kostas11, Dec 13, 2007 IP
  4. Brewster

    Brewster Active Member

    Messages:
    489
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    60
    #24
    I'll try it! Thanks for the tip. Have a look at the function mysql_real_escape_string - you may find it is a better alternative than add_slashes.

    Brew
     
    Brewster, Dec 13, 2007 IP
  5. Gawk

    Gawk Peon

    Messages:
    427
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    0
    #25
    heh heh - GOD, that is a promotion ;)

    This will reverse your results, not sure it is the most efficient way but it works...

    if ($_POST["action"] == "record_time") {
       $sql = "insert into results set username = \"" . addslashes(strip_tags(htmlspecialchars($_POST["username"]))) . "\", whattime = \"" . time() . "\"";
       $update = mysql_query($sql);
       	echo "<br><br>";
    	echo "Thank you " . addslashes(strip_tags(htmlspecialchars($_POST["username"]))) . " we have recorded your result.";
    	echo "<br><br>";
    	echo "Please <a href=\"record.php\"></a>click here</a> to see the latest results";
    }
    else {
        $get_res = mysql_query("select * from results order by whattime desc limit 10");
    	$backloop = mysql_num_rows($get_res);
    	if ($backloop > 0) {
    	    echo "<table>";
    		echo "<th>Username</th>";
    		echo "<th>Date & Time</th>";
    		for($i=$backloop-1;$i>=0;$i--) {
    			echo "<tr>";
    			echo "<td>" . mysql_result($get_res,$i,1) . "</td>";
    			echo "<td>" . date("d/m/Y H:i",mysql_result($get_res,$i,2)) . "</td>";
    			echo "</tr>";
    		}
    		#while ($row = mysql_fetch_array($get_res)) {
    		#	$resarray[] = "<tr>";
    		#	echo "<td>" . $row["username"] . "</td>";
    		#	echo "<td>" . date("d/m/Y H:i",$row["whattime"]) . "</td>";
    		#	echo "</tr>";
    		#}
    		echo "</table>";
    	}
    	else {
    		echo "No results to show";
    	}
    }
    PHP:
     
    Gawk, Dec 13, 2007 IP
  6. kostas11

    kostas11 Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #26
    It works but there is a small problem with this, each time a new entry is added, the top (earliest) entry is erased.
    The purpose should be to keep just the 10 best times, earliest at the top, later entries should not be at the top 10.


    for example the database has currently stored 20 entries. Only the top 10 (earliest) should appear.
    Is there a way to show just the 10 earliest times?

    Thank you
     
    kostas11, Dec 13, 2007 IP
  7. Gawk

    Gawk Peon

    Messages:
    427
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    0
    #27
    Simply change "order by whattime desc" to "order by whattime" - remove the "desc"
     
    Gawk, Dec 13, 2007 IP
  8. kostas11

    kostas11 Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #28
    I must buy you a present for all your great help.

    It is working perfectly.

    Thank you, Thank you, Thank you!
     
    kostas11, Dec 13, 2007 IP