Jump to content

mac_gyver

Staff Alumni
  • Posts

    5,514
  • Joined

  • Days Won

    185

Everything posted by mac_gyver

  1. you actually need separate if() statements, for the if(!$skin)... tests. the value you are testing is dependent on the previous if() statement, not exclusive of it. an if/elseif/else string of test are exclusive of each other.
  2. my supposition about different databases/connections was just one of the many possible things that could be causing the problem. i/we could probably list a dozen different possible things, but won't, because your setup could be doing something different than what i/we have in mind, and the actual problem could be the 13th item that no one thought of. you need to 'close the loop' by providing some investigation and feedback in order to narrow down the possibilities. solving this will require that you actually add the things that i mentioned to the code in order to pin down where and what is actually happening.
  3. the following lines from that code are the relevant ones that you would need to call with the three different sets of $iWidth and $iHeight values, along with the appropriate path where you want the imagejpeg() to save the file - $vDstImg = @imagecreatetruecolor( $iWidth, $iHeight ); // copy and resize part of an image with resampling imagecopyresampled($vDstImg, $vImg, 0, 0, (int)$_POST['x1'], (int)$_POST['y1'], $iWidth, $iHeight, (int)$_POST['w'], (int)$_POST['h']); // define a result image filename $sResultFileName = $sTempFileName . $sExt; // output image to file imagejpeg($vDstImg, $sResultFileName, $iJpgQuality);
  4. you would dynamically build the WHERE part of the $sql = " ... "; string using php code. for something like (no pun intended) a repeated like term, it is easiest to build each term as entries in an array, then just implode the array with the OR keyword. you should have an array with the possible choices that you are using to both build the form and to build the WHERE part of the query (blindly looping over the submitted form data is not a good idea as hackers can submit hundreds of form fields.) as you loop over the defining array, if that particular form input isset(), to indicate the checkbox was checked, add the `manufacturer` LIKE '%abc' portion of the query into an array. when you get done looping over the choices, if the array is not empty, just implode it to form that part of the query (note: implode will produce the correct result even if there is only one entry in the array.) edit: for the example you posted, the code would look like this - $cars[] = 'BMW'; $cars[] = 'Audi'; $terms = array(); if(!empty($_POST['check_list'])) { foreach($cars as $choice){ if(isset($_POST['check_list'][$choice])){ $terms[] = "`manufacturer` LIKE '%$choice'"; } } } $where_clause = ''; if(!empty($terms)){ $where_clause = "WHERE ".implode(' OR ',$terms); } $sql = "SELECT * FROM `Cars` $where_clause ORDER BY `tier` ASC";
  5. sorry to jump in here, but this example code you found is poorly written. it's only claim to fame is it shows up on the first page of web search results or it's being suggested by php programming courses. using it, assuming you can even get it to work, isn't going to help you create web pages or learn how to program. you should just learn to use the underlying framework and write you own code. in short, if you don't have the programming skills to troubleshoot why this example code doesn't work and it doesn't have code already in it to tell you why it isn't working, you need to contact the author of the code to have him/her troubleshoot and fix the problems in it.
  6. in the non-working case, you need to actually pin down what the code and data is doing, before you can even locate what needs to be fixed. you could for example have some broken html on the page for the id = 4 case that's causing the form to be invalid markup and it does not submit any data. the data itself may be invalid (or empty) and is resulting in query errors or a query that's not matching anything. your code could even be running twice for some reason and updating the data correctly, but is also updating it again back to the original values. you need to determine exactly what code is running, what values are being used by the code, and what is being returned from statements. 1. make sure that the code where the update queries are at is actually running. you should be forming the sql statements in a php variable (always), then just use that variable as input to the function that's running the query. you can then echo/log the contents of that variable for debugging purposes so that you will know that the queries are being ran and what data they actually contain. 2. make sure that all the queries are running without any errors. you should (always) test the returned value from a query for errors. an UPDATE query will return a false value when there is a query error. you can echo/log the mysql_error() information to let you know what the error is. and as a side note: you are using both the tep_db_query() and mysql_query() statements to perform queries. you could have a case where there are multiple database connections (even a default one set up by the php installation) and each of those methods of running a query could be operating on different databases. perhaps the database connections for one id = value are all using the same database and the code works, whereas for the other id = value, there are errors or missing data due to multiple database connections being used in the code.
  7. that may be WHAT doesn't work, but exactly what about it doesn't work. are the values not displayed for picking, does the page just 'refresh' without apparently doing anything when the form is submitted, is the form data not present in the form processing code after the form was submitted, are there php errors, query errors, is the data just not updated in the database table, or is the data actually present in the database table but it is not having an affect when it's being used? in short, what symptom do you see in front of you, since we're are sitting there with you to see it, that leads you to believe 'it doesn't work'? there's very little backwardly incompatible differences between most of the php versions, and these things are generally obscure things that most code doesn't use (they are documented in the php.net documentation, appendix.) most problems like this are due to php configuration differences. in fact, only in the php5.4 version were a number of obsolete things finally removed (your use of $HTTP_GET_VARS for example aren't present as of php5.4+). edit: you also mentioned that the problem is associated with - vendors2_id = 16 works, vendors2_id = 4 does not work. the posted code only has vendors2_id hard coded as a 16. is the posted code the actual code that doesn't work and if not, why do you have different code that only differs in a value that's has hard coded that value rather than using one instance of the code and using a variable for the, well, variable value in it? if the code with a vendors2_id = 4 in it doesn't work, perhaps that's the code you need to post.
  8. you should store the raw data for each week/game/player as a separate row in a 'result' table, similar to what you do with a bank account statement. then simply do a sum() in a query to produce a score for any player(s) when needed. this will do two things - 1) keep a specific record of who, what, when, and where something occurred 2) prevent duplicate entries/processing from corrupting the data.
  9. sorry, but no you haven't. look at and compare the second line of code in both of the pieces of code in my reply #8 and in your reply #11. you should be able to notice what's logically different about those lines. in the line that's from your previous thread, you are assigning the result form the ->prepare() method call to a variable, $stmt, that then gets used in the bindParam() and the execute() method calls. programming is an exact science. the computer only does exactly what your code tells it to do. if a statement returns a value that you must then use in the following dependent statements, you must be able to 'get' that kind of information when you read the documentation and examples.
  10. user preferences (and privileges) are usually stored in a database table and retrieved on each page request. this allows them to be easily modifiable by site moderators/admins and they take effect immediately (on the next page request.) storing them in session variables means that only the visitor that the session belongs to can easily modify them or you must add a lot of unneeded complexity to make the session data find-able and editable by site moderators/admins. a 'remember me' login is usually accomplished by generating a unique and hard to guess token, that's not a fixed value tied to any user information, storing that token in a cookie and storing it in the user row in a database table. in this case, the logged in/logged out state is also stored in the user row in the database table so that the only way that someone who's logged out can become logged in is for them to submit their username/password.
  11. your program logic will default to the catch{...} block for all database errors, so even though the message being output says, "Connection failed: ..., you actually have an error at one of the query statements. i recommend that you change your code to consistently use one variable name to hold the sql query statements, something like $sql. then, in the catch{...} block, if $sql is empty, you know you reached that point due to a connection error. if $sql is not empty, you know you reached that point due to a query error. this logic will let you echo the $sql query statement as part of the error so that you can look to see what might be wrong with it. change your catch{...} block to something like the following - } catch(PDOException $e) { $status = empty($sql) ? 'Connection failed':" Query failed: $sql"; echo "$status, Error: {$e->getMessage()}, File: {$e->getFile()}, Line: {$e->getLine()}"; }
  12. There's probably 20,000 examples of what you are asking posted on the web. in fact that's what the web was created for, publishing and sharing information. your first stop should be to do a web search and try to accomplish this assignment yourself. programming help forums are for helping those that already have code and have tried.
  13. here's another hint. the following is your SELECT ... query from the previous thread, with the correct bind usage - $sql = "SELECT ...."; $stmt = $handler->prepare($sql); $stmt->bindParam(':username', $username); $stmt->bindParam(':password', $hash); $stmt->execute();these are the corresponding lines of code from this thread - $sql_string = "INSERT INTO users SET username = :a, password = :b"; $sql->prepare($sql_string); $sql->bindParam(':a', $username); $sql->bindParam(':b', $hash); $sql->execute();lines 2-5 of both of these pieces of code should be logically the same and in fact should be identical for consistency reasons (why keep writing/changing code that's performing the same actions.)
  14. we are really trying to help you, but when you don't bother to read the php.net documentation and its examples for the statements you are trying to use and really learn what each statement does, and in this case learn what each statement returns as a value, it's not possible for you to write code that does anything. you are operating in an uncontrolled random trial (and mostly error) mode, where you are not using the documentation as an input to determine the correct way of using any statement. i'll give you a hint: bindParam() is a method of the PDOStatement class. it is not a method of the PDO class. the following is the php.net documentation of the return value from the ->prepare() method - all of this information, including examples, can be found at in the php.net documentation. in your last thread, you were correctly doing everything to prepare and execute the query, except you were not using the proper bind statement. the code you posted above in this thread is nothing like what you used in the last thread, which says you didn't learn anything from what you were doing in the last thread. just going through the motion of copy/pasting lines of code, isn't programming and isn't learning. you must be able to generalize and make use of what you 'learned' to prepare and execute one type of query to do the same steps for any other type of query.
  15. i'm going to guess that you have a redirect-loop or similar that's requesting the page multiple times. the symptom you are seeing of random numbers other than a 1 causing the query to update is just the last random value being echoed, but in fact the code has ran several times, one of which had a 1 that caused the query to run. you may also have some other code that's always running the same query or have a logic error that's updating that value when it should be resetting it. at this point, you have a page that doesn't do what you expect. it will likely take seeing all the code on the page that reproduces the problem (less any database credentials) in order to help you.
  16. unfortunately, the OP is using the PDO library, based on the error and his connection code posted in the last thread on this forum, but isn't actually learning how to use it, and is therefore getting stuck on the basic steps over and over. @Tom10, the task in this thread is similar to your previous thread. you are trying to form and run a query, an insert query in this case, but are not using all the statements correctly. the only way to get all the statements to work together and correctly is to learn what each of the statements do, so that you will know how they are supposed to go together. in the last thread you were not using the correct bind statement that is part of the PDO library of functions. in this thread, you have a mix of code that is/was running a non prepared query using the pdo->query() method, then added a couple of lines of code trying to turn that into a prepared query, but not converting the sql statement to a prepared query, not using the correct pdo bind statement, and still leaving in the previous call to the pdo query() method. the reason i didn't post any fixed code or link to any php.net documentation in your previous thread, is because you are missing the basic understanding of what these statements and lines of code do and the only way you can gain that understanding is if you actually go and research, internalize, and learn this information. once you know how to use the pdo statements to prepare a query, bind input parameters, execute the query, and retrieve any results, you can then use that knowledge to form and run any kind of query.
  17. what are the actual $encounter values when you log/echo them right before the if(){} conditional statement and are you sure that's the actual code that's being ran on the server?
  18. it sounds like what you are trying to do isn't what session variables are intended for. the session is just a container for server-side variables that persist between page requests. it's called a session because it's intended to only last one browser session. it's actually not normal to extend the session cookie lifetime. perhaps if you state what some of these different values will be used for, someone can tell you the best way of handling each of them.
  19. no one stated there was a problem with your database connection, so i don't know why you posted the above code. the problem is that there is no pdo bind_param() method. to use the PDO database statements correctly, you must read the php.net documentation for what you are doing. just coping things you have have seen someplace, without knowing what they mean and what they do, isn't going to work.
  20. your prepare() statements are failing, probably because you haven't selected a database, and your code isn't doing anything when a prepare fails. your code keeps running and tries to use the result from the queries that have failed. your if(){} conditional test for the first two prepare() statements needs to do something when there's a failure, such as reporting that there is an error, prevent the rest of the code from running, and during development display all the information there is about the error. the third prepare() statement doesn't even have any logic around it to make sure it is working before trying to call the bind_param() method. the or trigger_error($mysqli->error); statement you have on the end of the $query = "..." string isn't doing anything (a string cannot produce a mysqli error.) perhaps you meant to have that in an else {} clause for the if(){} conditional test around the prepare() statements?
  21. you are making this much harder than it needs to be. you should also post in the javascrpt forum section if you are going to be using js/ajax. the reason your session variable isn't working is due to a logic error. there's no code setting or modifying the session variable. see the following sample code for how you could manage the process using php - <?php /* Create a new session to hold the step values */ session_start(); /* Force start on step 0 when page load, via session */ if(!isset($_SESSION['step'])){ $_SESSION['step'] = 0; } // form processing code if($_SERVER['REQUEST_METHOD'] == "POST"){ $errors = array(); // store any errors in this array $data = array_map('trim',$_POST); // get a trimmed copy of the form data /* Check WHICH form processing code to run */ switch($_SESSION['step']){ case 1: // step 1 form processing code // validate the form data //$errors[] = 'empty host name'; // dummy error for demo purposes // if no validation errors, use the form data if(empty($errors)){ // do something with the submitted data from this step } break; // processing code for the remaining steps would go here case 2: break; } // if there are no errors at this point, advance to the next step and do a header() redirect to the exact same url of this page to cause a get request if(empty($errors)){ $_SESSION['step']++; $host = $_SERVER['HTTP_HOST']; $uri = $_SERVER['REQUEST_URI']; header("Location: http://$host$uri"); exit; } // if there are errors, the code will continue and display the html document, where you would // display any errors, redisplay the current step's form, with any previous values } // any get method code would go here to get/produce data that the page needs, // such as if you are editing existing settings, to retrieve them and set the $data array with them // if the $data array doesn't already exist ?> <!DOCTYPE html> <html> <head> <title>CMS Website : Installer</title> <link rel="stylesheet" type="text/css" href="css/styles.css" /> <link href='http://fonts.googleapis.com/css?family=Open+Sans:400,300,700' rel='stylesheet' type='text/css'> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> </head> <body> <div id="form-box"> <?php // display any errors if(!empty($errors)){ echo "Correct the following errors:<br>"; foreach($errors as $error){ echo "$error<br>"; } } switch($_SESSION['step']){ case 0: echo ' <h3>CMS Installer</h3> <p>You <small>MUST</small> install the website for it to function correctly.</p> <form method="post"> <div align="center"><input type="submit" value="Start Install" style="cursor: pointer;" /></div> </form> '; break; case 1: // note: to display any initial/existing form field data, test if the corresponding field // in the $data array isset() and echo it as needed to populate the form fields // this would be much easier if you dynamically built (and processed) each form by having a definition in an array that you simply step through echo ' <h3>CMS Installer ~ Step 1</h3> <p>Please fill in the form with the appropriate details</p> <form method="post"> <input type="text" name="db-host" placeholder="Database Host" /> <input type="text" name="db-username" placeholder="Database Username" /> <input type="password" name="db-password" placeholder="Database Password" /> <input type="text" name="db" placeholder="Database Name" /> <input type="submit" value="Submit Database" /> </form> '; break; // display code for the remaining steps would go here case 2: break; } ?> </div> </body> </html>
  22. you are using the PDO statements for your database connection. all of the rest of your database statements must also be PDO statements. the bind_parm() statement you are using is a mysqli statement. you cannot mix the different types of database statements. i recommend that you use the php.net documentation as a reference source to avoid confusion like this.
  23. web servers can handle several 100's of requests per minute. just using the timer/ajax-request method will work for a casual chat system. you would want to make each request/response as brief as possible and make the server side code as efficient as possible, off loading as much formatting/processing onto the client as possible. the client side request, which should be a GET request btw, would include the id of the last message that has been displayed for that user. the server would just query for and retrieve any new messages with id's greater than that id. at a minimum, the message id column in the database table would be indexed. if there's no new messages, the server should return a simple status value to tell the client side code it doesn't need to do anything, perhaps just an empty json encoded array. if there are new messages, just return the raw message data, leave any formatting/display to the client side code. make sure that the database server has query caching turned on as well. when data in the database table hasn't changed, the same database query being made from multiple clients will return data from the cache rather than retrieving it from the database table. you can have 100's of clients all waiting for a new message and they will keep getting the result from the cache that there's no new messages until there actually is one that was stored into the database table, altering it, which causes the cache to be cleared so that it will then cache the new message(s) for the next series of update requests.
  24. some suggestions that will help you - 1) set php's error_reporting to E_ALL and display_errors to ON in the php.ini on your development system to get php to help you by reporting and displaying all the errors it detects. you will save a ton of time. 2) all the database statements must be from the same library of functions. use all mysqli_ statements. the mysql_error() and mysql_insert_id() statements you have now are not working and are probably throwing php errors (see item #1 in this list.) 3) DRY - (Don't Repeat Yourself). you should not repeat code. factor out the common code and only put the code/data that's different in the conditional statement. this will result in less code that you have to type, test, and change. 4) don't store the cart total in a database table. this is derived information and should be calculated when needed. 5) all external data cannot be trusted and can be anything. external values you put into any sql query statement must be handled correctly to prevent sql injection and to prevent sql errors if the data contains sql special characters. edit: 6) the semi-colon ; does not need to be on the end of sql query statements. 7) you can put php variables inside a double-quoted php string without using concatenation. this will result in less typing and typo errors. associative array variables used this way need to be enclosed in - { } inside the string. all of your add to cart processing code should inside the if(isset($_POST['submit'])){ ... } conditional. If the form hasn't been submitted, there's no point in running any of the processing code. once you complete items #1 and #2, you will likely be getting meaningful errors that will point to why the query is not working.
  25. the second parameter of session_set_cookie_params() isn't where the session data is saved on the server, it's the path on your site that the session cookie will match - session_save_path() controls where the session data is saved on the server.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.