fritz.fx Posted July 19, 2008 Share Posted July 19, 2008 I hate asking questions, I prefer to figure it out by myself, but the ol' age thing gets in the way now, so after a few hours of hair pulling, I come back here. first query This one should be simple, I just can't see it... I have a table named "last_check" and a column named "username" I have a script which, once a $username clicks a submit button which 'includes' another script.. within that 'included' script I'm trying to write the command to update that users $username into "username on the table "last check" I have this: mysql_query("UPDATE last_check SET username = $username"); which isn't working. Basically as an example >> If Fred clicks submit then update "username" on "last_check" to Fred. Then, if Mary then clicks the submit button >> overwrite Fred with Mary !! Second query My script connects to an FTP server and copies files from that server to another and checks the size while it's at it, if the filesize is under a certain size it echo's a message, which works, only problem is, it copies two different files. a *.png and a .htm. How can I set it so that if the filesize is greater than the .htm (lets say 20480bytes) but less than the .png (lets say 50000bytes)?? This is what I have so far, but only detects the "less than" part of it all. $goforit = ftp_get($conn_id, $file, $file, FTP_BINARY); if (!$download) { echo "$file could not be loaded!<br>"; } else { echo "$file has been successfully updated!<br>"; } if (ftp_size($conn_id, "./pb0*.png") < 20480) { echo "$file was a black Screenshot (this is experimental atm)<br>"; Quote Link to comment https://forums.phpfreaks.com/topic/115606-solved-2-questions-1gtsimple-php-mysql-update-amp-2gtgreater-but-less-than-filesize/ Share on other sites More sharing options...
ignace Posted July 19, 2008 Share Posted July 19, 2008 first query: mysql_query("UPDATE last_check SET username = '$username'"); // note the ' // preferably you validate and filter $username against sql injection, imagine my $username being '; DROP TABLE last_check; -- second query: please post the full code Quote Link to comment https://forums.phpfreaks.com/topic/115606-solved-2-questions-1gtsimple-php-mysql-update-amp-2gtgreater-but-less-than-filesize/#findComment-594281 Share on other sites More sharing options...
fritz.fx Posted July 19, 2008 Author Share Posted July 19, 2008 Thanks ignace but I've tried that Not too worried about SQL injection, as only registered users would be using the script, and that is restricted to only a group of users that I know (http://www.phpfreaks.com/forums/index.php/topic,207458.0.html) my .htaccess file does the rest. here is the full code (including your suggestion (line 4)) query 2 problem is after "//Download files" // Connect to Database mysql_connect("localhost", "DB username", "DB password") or die(mysql_error()); mysql_select_db("DB name") or die(mysql_error()); //insert into DB the last member who checked screenies mysql_query("UPDATE last_check SET username = '$username'"); //mysql_query($query) or die('Error, query failed'); set_time_limit(0); include_once("config.php"); error_reporting(E_ALL); echo "Update process<br>"; // Connection $conn_id = ftp_connect($ftp_server); // Login with Username and Passwort $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); ftp_pasv($conn_id, true); // Connectivity if ((!$conn_id) || (!$login_result)) { echo "FTP connection has failed!<br>"; echo "connection to $ftp_server with Username $ftp_user_name trying.<br>"; exit; } else { echo "connecting to server \n<br>"; } // Ins directory if (ftp_chdir($conn_id, $dir)) { // echo "Current Directory: " . ftp_pwd($conn_id) . "\n<br>"; } else { echo "Directory exchange failed.\n<br>"; } // Read the folder while ($nlist = ftp_nlist($conn_id, "./pb0*.*")) { foreach ($nlist as $file) { //Download files $goforit = ftp_get($conn_id, $file, $file, FTP_BINARY); if (!$goforit) { echo "$file could not be loaded!<br>"; } else { echo "$file has been successfully updated!<br>"; } if (ftp_size($conn_id, "./pb0*.png") < 20480) { echo "$file was a black Screenshot (this is experimental atm)<br>"; } //Deleting files from the source if ($file == "pbsvss.htm") { } else { $delete = ftp_delete ($conn_id, $file); } if (!$delete) { echo "$file was not deleted!<br>"; } else { //echo "$file has been successfully removed!<br>"; } } } $download1 = ftp_get($conn_id, "pbsvss.htm", "pbsvss.htm", FTP_BINARY); if (!$download1) { echo "pbsvss.htm could not be loaded!<br>"; } else { echo "<strong>Screenshots have been successfully updated!</strong><br>"; //echo "Pbsvss.htm file has been successfully downloaded!<br>"; } //close ftp_close($conn_id); echo "Update Completed<br>"; $filename = "pbsvss.htm"; $file = fopen($filename, "r"); $filesize = filesize($filename); $text = fread($file, $filesize); fclose($file); $text = str_replace(".htm",'.png', $text); echo($text); Quote Link to comment https://forums.phpfreaks.com/topic/115606-solved-2-questions-1gtsimple-php-mysql-update-amp-2gtgreater-but-less-than-filesize/#findComment-594315 Share on other sites More sharing options...
ignace Posted July 19, 2008 Share Posted July 19, 2008 line 4 is a comment Quote Link to comment https://forums.phpfreaks.com/topic/115606-solved-2-questions-1gtsimple-php-mysql-update-amp-2gtgreater-but-less-than-filesize/#findComment-594327 Share on other sites More sharing options...
fritz.fx Posted July 19, 2008 Author Share Posted July 19, 2008 LOL... 4 + 1 Quote Link to comment https://forums.phpfreaks.com/topic/115606-solved-2-questions-1gtsimple-php-mysql-update-amp-2gtgreater-but-less-than-filesize/#findComment-594335 Share on other sites More sharing options...
ignace Posted July 19, 2008 Share Posted July 19, 2008 oh didn't know you were using zero-indexing the problem would be that $username is empty at the moment you perform your query or do you include something before your create the db connection? $goforit = ftp_get($conn_id, $file, $file, FTP_BINARY); your remote file is the same as your local file? Quote Link to comment https://forums.phpfreaks.com/topic/115606-solved-2-questions-1gtsimple-php-mysql-update-amp-2gtgreater-but-less-than-filesize/#findComment-594339 Share on other sites More sharing options...
fritz.fx Posted July 20, 2008 Author Share Posted July 20, 2008 I've worked out the first problem, by creating a new column named "ID4" and giving it a value of "ID" then used this code : $previous = mysql_query("SELECT username FROM last_check WHERE ID4 = 'ID'"); $row = mysql_fetch_array($previous); $previous_user = $row['username']; //Update DB with the new username mysql_query("UPDATE last_check SET username = '$username' WHERE username = '$previous_user'"); As for the second problem, I might have to work out how just count the number of files on the local server that are greater than but less than a certain size. Quote Link to comment https://forums.phpfreaks.com/topic/115606-solved-2-questions-1gtsimple-php-mysql-update-amp-2gtgreater-but-less-than-filesize/#findComment-594690 Share on other sites More sharing options...
ignace Posted July 20, 2008 Share Posted July 20, 2008 You just want to validate the filesize of the file on the remote against the size of the local file? $filesize1 = filesize($file1); $filesize2 = filesize($file2); if ($filesize1 !== $filesize2) { if ($filesize1 < $filesize2) { /* second is bigger */ } else { /* first is bigger */ } } btw, are your sure this is correct? $goforit = ftp_get($conn_id, $file, $file, FTP_BINARY); // should translate to ftp_get(1, "c:/users/file.bla", "c:/users/file.bla", 2); notice the bold markings Quote Link to comment https://forums.phpfreaks.com/topic/115606-solved-2-questions-1gtsimple-php-mysql-update-amp-2gtgreater-but-less-than-filesize/#findComment-594742 Share on other sites More sharing options...
fritz.fx Posted July 22, 2008 Author Share Posted July 22, 2008 OK with a little help from another forum (sorry, don't shoot me) I've resolved my filesize problem by making it simpler to sort out, it is: (find .png files smaller than 50kb and NOT copying the 2kb *other* files from the remote FTP server (which weren't needed after all)) $count = 0; foreach(glob('*.png') as $file) if(filesize($file) < (50 * 1024)) $count++; echo $count From that I've added my own little twist: (including a TOTAL count of .png files) (this is what I have currently, obviously needs a cleanup) $total_jpg_file=count(glob("*.png")); echo "Number of screenshots currently hosted = ".$total_jpg_file; //The next 4 lines count the number of screenshots (.png) that are currently hosted LOCALLY $count = 0; foreach(glob('*.png') as $file) if(filesize($file) < (50 * 1024)) $count++; // The line below converts that count (above) into a percentage and assigns a variable to it ($pc) $pc = floor ($count / $total_jpg_file * 100); echo ' <br />'; //This next section checks the above percentage of black screenshots(variable $pc) and then does some cool shit to it before it displays it to the screen. // if there is more than 50% of black screenies it will display it as RED and BLINKING text (taking into account the CSS or style type) echo "Number of Black Screenshots = ".$count; // echo $count ; echo " <blink><span class=\"style2\"> ("; echo $pc; echo "%)"; echo "</span></blink>"; echo ' <br />'; //............if the percentage is between 25 and 50% it will display it as orange text. (taking into account the CSS or style type) } elseif($pc >= 25) { echo "Number of Black Screenshots = "; echo $count ; echo "<span class=\"style3\">("; echo $pc; echo "%)"; echo "</span>"; echo ' <br />'; //if the percentage is between 1 and 25% it will display it as green text. (taking into account the CSS or style type) } elseif ($pc >= 1) { echo "Number of Black Screenshots = "; echo $count ; echo "<span class=\"style4\"> ("; echo $pc; echo "%)"; echo "</span>"; echo ' <br />'; } else { //If shit happens then it will display the below message. echo "Unable to determine number of Black screenshots at this time"; } As for this little bit your concerned about Ignace $goforit = ftp_get($conn_id, $file, $file, FTP_BINARY); NO, I'm NOT sure that it is correct at all (Still a PHP newbie) BUT, It does do exactly what I want it to do, so out of fear of screwing up a script that currently works, I'm leaving it alone. (your remote file is the same as your local file?) my remote file overwrites my local file of the same name) Oh by the way oh didn't know you were using zero-indexing Smiley the problem would be that $username is empty at the moment you perform your query or do you include something before your create the db connection? I did realise this while I was at work, and thought to myself " you idiot!!" turns out that that was the problem. moved the command down a few lines after declaring the $username and it worked. thanks Thanks for your time My hair-pulling is over for the time being.. Problem resolved. Cheers Quote Link to comment https://forums.phpfreaks.com/topic/115606-solved-2-questions-1gtsimple-php-mysql-update-amp-2gtgreater-but-less-than-filesize/#findComment-596442 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.