-
Posts
5,450 -
Joined
-
Days Won
175
Everything posted by mac_gyver
-
Checking mime types of two files in a string
mac_gyver replied to jupiter's topic in PHP Coding Help
your goal should be to NOT repeat code, but to reuse the same, identical code. also, by creating a series of numbered variables, you must now type out or copy/paste/overtype the code for each additional set of data. you should instead use arrays for sets of same meaning data that will be processed in the same way. the looping that hansford showed is for an array of uploaded files. the php.net upload handling documentation shows how to do this. see example #3 at this link - http://php.net/manual/en/features.file-upload.post-method.php as to the validation in the php code, since this is user submitted data, each possible error should be checked separately, for each file, and you should give the visitor as much information as possible about what file and what error occurred with that file. any validation error messages you produce should be added as elements to an array, so that you build a set of all the validation errors. you can then simply loop over this array of error messages at the point where you want to display them. short-answer, use arrays when dealing with sets of data, i.e. sets of input uploaded files, sets of validation error messages, ... -
a function/method will generally return data, that you can then assign to a variable or directly loop over. it may however make the data available as a class property. in either case, it would take looking at the documentation and/or code for the PesquisarPorDatas() method in order to find out what where, what, and what format the data is in.
-
Trying to use variables from another file out of memory
mac_gyver replied to Andrew789123's topic in PHP Coding Help
you don't. the class file is only the class definition. it gets included by your main code so that you can create an instance of the class to use in your main code, not the other way around. -
the "Mark Solved" button is in the lower right-hand corner of each post in the thread.
-
your code is not checking if an upload form was submitted and that the upload worked before trying to use the uploaded file information, which has resulted in you trying a bunch of code (all the isset() business) that isn't actually solving anything. because uploading files can cause the $_FILES and $_POST arrays to be empty, when you exceed the post_max_size setting, to test if a form was even submitted, you must test if $_SERVER['REQUEST_METHOD'] == "POST" (there are other ways of checking, but this way tells you that a correct method of form was used.) next, to detect if the upload worked without any errors, you must test if your expected $_FILES['video'] element exists (is set) and that the ['error'] element is a zero value (no errors.) if all the tests to this point are true, you know that the upload worked and that the ['name'], ['type'], ['size'], and ['tmp_name'] have values and can be used. In all other cases, these elements won't all have values and cannot be used. also, for each of these tests that have failed, you can determine what caused the upload to fail and tell the visitor about things he has control over, such as the size of the uploaded file(s), that he can alter and upload again. so, at this point, your form processing code, at a minimum, would look like - // check for a post method form if($_SERVER['REQUEST_METHOD'] == 'POST'){ // is the expected form field set if(isset($_FILES['video'])){ // a properly named field/variable exists, check for upload errors if($_FILES['video']['error'] == UPLOAD_ERR_OK){ // the upload worked, you can use the uploaded file information here echo "the upload worked, you can reference the ['name'], ['type'], ['size'], and ['tmp_name'] information at this point"; // all your form processing logic needs to go here... } else { // there was a detectable upload error, handle that condition here... // the php.net upload handling documentation lists all the error values and what they mean } } else { // the expected $_FILES['video'] element doesn't exist. if there are no coding errors, // this typically means the total size of the post data exceeded the post_max_size setting. // you can test the $_SERVER['CONTENT_LENGTH'] value to confirm if this is the case. } }
-
Trying to use variables from another file out of memory
mac_gyver replied to Andrew789123's topic in PHP Coding Help
your code apparently has a circular include/require (i'm not sure why php isn't failing with a redeclare error for the class definition.) your class definition file is including your register.php script, which is including your config.php file, which is including the class definition again. the only thing that should be in the class definition file is the class definition. the include "register.php"; line that you added, and in fact the display_errors line that the author of that software already had in that file, should not be in that file. your main file, which is register.php, is where you include/require things that it needs (and in the case of class files, you should eventually advance to the point of using an autoloader.) -
just put your form and form processing on the same page. in this case the only redirecting you will need is one to the exact same url/page after you have processed the form data, to cause a get request for that same url/page to cause the browser to not try and submit the same post data again.
-
to echo a variable, all you need is echo $sql; this is pretty basic stuff and would be covered in any beginning php book, tutorial, or class. have you read a php book or tutorial, or taken a class to learn the basics before trying to write your own code that does something? of the lines you posted, line #2 and line #3 would have displayed the content of the $sql variable (line #3 would have also displayed the extra dots and spaces within the double-quoted string. lines #1 and #4 would have literally displayed what is within the single-quotes. if nothing was displayed when you tried to echo the $sql variable, either you either have a php syntax error and the whole page of code isn't running or you put the echo statement at the incorrect point in your code or the code where you put it at isn't being executed. if you want anyone here to try and determine why the echo statement you put into your code didn't do what was expected, you would need to post your current code that contains that echo statement to give us something upon which to help you.
-
Prevent named sessions form overwriting each other
mac_gyver replied to NotionCommotion's topic in PHP Coding Help
i have a basic why question. why are you trying to have more than one session that you want to share data between? someone in a previous thread asked why you are doing this and your reply was a statement of what you were trying to make work, not why you are doing this. knowing your overall purpose, would perhaps help someone to give a more direct solution, since what you are trying to make work is uncommon. -
everyone that is reading this was new to php at one point in time. does that mean you are not going to echo the $sql variable holding the query statement, look at it to see if it even has values in it, and then look at your database table and make sure you have a row with those exact same values? you are the only person here who has access to your server and can do these things to pin down where the problem is at.
-
you need to actually debug and find out why your script is not matching the username/password. just randomly throwing a bunch of code against the wall to see what sticks isn't programming and will take forever to get something that works. you need to echo out the $sql query statement to see what exactly it is and then look in your database table, using your favorite database management tool, such as phpmyadmin, and see if the username and hashed password values, in the $sql variable that you echoed, exactly match the values in a row in your database table.
-
i'm not intentionally trying to give you a hard time, i promise. but the posted code is a copy/paste fail. you have code that's not accomplishing anything useful (a select query on the logs table), duplicate code (checking the session variable and running a select query on the users table, two different times), and code that's out of date and not secure (the magic quotes/addslashes business). rather than to follow along with the bad code that Dreamweaver produces, you need to actually learn to program and write your own code that does what you want it to do. you need to start with having a stated goal. do you want to log ALL log-in attempts, successful ones and incorrect ones, or just the successful ones? what data do you want to store (what data is available)? some suggestions - ip address, username, datetime, and a success/fail status. next, you would form and run the INSERT query at the correct point in your program logic. the code you have posted doesn't actually have any log-in logic in it to be able to help you. in fact, the posted code isn't using any form variables, so it's unlikely that this is your log-in authentication code at all. if you are logging just the successful log-in attempts, you would form and run the INSERT query inside the successful login code. if you are logging both successful and failed log-in attempts, you would just set up the success/fail status value in the code and then form and run the INSERT query after the point where you have determined the correct/failed log-in state.
-
this is wrong advice. the $_POST array is always set by php. all the super-global arrays are set, even if they are empty. to detect if a post method form has been submitted, you either need to test if a specific, non-optional/non-disabled, field in the form is set, which your code is doing with the if(isset($_POST['submit'])) {, provided you always click on the submit button to submit the form, or you can test if $_SERVER['REQUEST_METHOD'] == 'POST' if all you want to detect is if any post method form was submitted. once you have detected that a form has been submitted, you don't need to individually test if text/password/textarea fields are set, they will be if you don't have any coding errors in your form. and in fact, if you do have coding errors, you wouldn't want to use isset() to qualify the references to those type of fields as it would hide the fact that your form doesn't have fields by those expected names.
-
table not being sorted by primary key by default
mac_gyver replied to E_Leeder's topic in MySQL Help
there is NO default sorting on database tables. the ordering in a result set is the order that the rows have in the table. this is typically the order in which they were inserted, but as you delete, insert, update, index, backup/restore, ... the data, the order of the rows can get changed. if you want data retrieved in a specific known order, you must use an ORDER BY term in your query (note: a GROUP BY also performs an ORDER BY since the rows must be sorted before they can be grouped, but you can apply your own ORDER BY after the GROUP BY to order the resulting groups the way you want them to be retrieved.) -
Trying to use variables from another file out of memory
mac_gyver replied to Andrew789123's topic in PHP Coding Help
if you changed the memory limit and you are still getting memory errors, with the total memory mentioned in the error changing to match the new limit you set, your code has a logic error (usually a loop/array problem of some kind) and it will consume all available memory, no matter how much you make available. you must find what is causing the problem in order to fix it. if you cannot determine what is causing the problem youself, you will need to post all the relevant code, less any database credentials. -
since this code seemed familiar, i went looking through your previous threads, and found that i had helped with bits of it before. this code is responsible for retrieving and displaying the content of the shopping basket. you would do what maxxd has suggested above, and join the postage table with the existing basket/products join query to get the Post_Cost for each item in the basket. while you are looping over the result of that ONE query, you would detect and save the highest Post_Cost value. you could also, in the loop, store the Post_Cost values into an array, and just use php's max() function on that array to get the highest Post_Cost value. you, or who originally wrote this, easily have two times more php code that what is needed, resulting in it taking 5-10 times longer to find and fix any problems or just make changes to what the code is doing (and reducing the chance of someone helping with it as who wants to read through a wall of verbose and repetitive code that can be replaced with a couple of statements.) taking the time to reorganize and reduce the code, will actually save time overall, and make it easier to switch from using the depreciated mysql_ functions, since there will be less overall calls to the mysql_ functions and all the database logic that stores, updates, and retrieves the data for the page will (should) be grouped near the start of the code on the page. the html on the page will (should) be grouped at the end, with only simple php echo, conditional, and loop statements in it. edit: assuming you use a single joined query and that you have a Post_Cost associated with each item in the basket, the following pseudo logic is all you would need to find the highest Post_Cost value - $Post_cost = array(); while(...){ // your existing while loop $Post_cost[] = $row['Post_Cost']; // save each post cost in the array } // end of your existing while loop $highest_post_cost = max($Post_cost); // find the maximum value in the array and if you want the $postagerate = 15.00; amount to be the minimum postage value, just put that value into the $Post_cost array before the start of the while(){} loop.
-
string data values that are put into an sql query statement must be escaped, using your database library's string escape function (or use prepared queries) and numerical data values that are put into an sql query statement must be filtered/validated/cast as the appropriate numerical data type (or use prepared queries) in order to prevent sql errors and to prevent sql injection. btw - if you have that many fields being put into a query, you should be dynamically building the query using code, rather than typing out the whole query statement. this would also make it easier for your code to escape/filter/validate/cast values (or use prepared queries) since you would be looping over the fields/data values, rather than have each one hard-coded and individually written out.
-
in whatever sql query is being run right before the code you posted, just use MAX(Post_Cost) in the query to get the highest post cost value for the set of matching rows.
-
you would just make your series of numbered variables into an array with an index being the sensor number - $array[1] = array (7,8,9,10,11,12); $array[4] = array (13,14,15,16,17,18); $array[6] = array (1,2,3,4,5,6); your echo statement, inside the looping code, would simply be - echo $array[$i][$x]; or you could simply loop over the data directly using foreach($array as $sub_array){...}
-
if you are certain that the code reaches the if($this->_query->execute()) statement (it would be returning a false, rather than a null on an error) and you just want to debug why it is failing (rather than to implement the error handling i suggested above), you can use print_r($this->_query->errorInfo()); in your else { ... } logic to find out what error is occurring.
-
the $_FILES['userfile']['error'] element will be a UPLOAD_ERR_NO_FILE Value: 4, when no file was uploaded with the form.
-
PHP script is loading in plain text instead of processing
mac_gyver replied to ghurty's topic in PHP Coding Help
is the script using full opening <?php tags or just <? -
Trying to use variables from another file out of memory
mac_gyver replied to Andrew789123's topic in PHP Coding Help
that 134Mb of memory being consumed is not because of the code you have posted. that's just where the code was running at when memory was exhausted. something else on the page, either the total amount of code and static data, or the dynamic data leading up to the point where the error is being reported at is what is consuming that amount of memory. you need to profile the memory usage at various points in your code to find where the large amounts of memory are being used at. if you would like anyone here to try to see what things the code could be doing to consume large amounts of memory, you would need to post all the code (the problem could be code and static data present after the point where the error is occurring at) for that page, less any database credentials. if you are copying something you found on the web, be advised that most game code found on the web started life as a classroom exercise, to teach basic programming, and are not how you would write an actual browser based game because they typically don't have any proper memory management, don't have efficient code, and aren't written as general purpose code that is easy to configure/change, debug, or maintain. it is always best to just learn how to program first, then write, from scratch, your own code that does what you want, rather than copying things you may find on the web. -
a) you need to echo the actual sql query statement, echo $query; you need to do this before the msyql_query() line since that line is die'ing due to the error. b) you need to escape the $each value being put into your sql query statement to prevent sql injeciton and to prevent sql errors. c) it is much easier if you just put each of the keywords like '%term%' entries into an array and implode the array with an ' OR ' to produce the combined term (this will produce the correct result even if there is just one entry.)
-
your code that detects the change in the categoryname - if ($categoryname != $rows['categoryname']) { ... } is where you would do this. when the categoryname changes (your existing logic), if it is not the first category (if $categoryname is not equal to the initial '' value), you would output a closing </div> tag, then unconditionally output the <div class="category"> tag right before the <div class="h"><h2>Category Title</h2></div> bit. you would also output a final closing </div> after the end of the while(){} loop, if $categoryname is not equal to the initial '' value, meaning that some data has been output. your existing - if ($categoryname != $rows['categoryname']) { ... } would become - if ($categoryname != $rows['categoryname']) { if($categoryname != ''){ // a previous section exists, close it here... echo "</div>"; } $categoryname = $rows['categoryname']; echo"<div class='gallery'>"; echo"<div class='h'><h2>".$rows['categoryname']."</h2></div>"; } and after the end of the while(){} loop, add - if($categoryname != ''){ // a previous section exists, close it here... echo "</div>"; }
- 2 replies
-
- php
- while loop
-
(and 1 more)
Tagged with: