-
Posts
5,450 -
Joined
-
Days Won
175
Everything posted by mac_gyver
-
that would be why your error handling should tell you everything possible about the error -
-
a) you cannot do that. each place-holder can only appear once in a query, with a bind statement for each. you would need to use something like :words1, :words2 but bind the same variable to each. b) when you don't post the actual code/query that is causing a problem, you just waste everyone's time.
-
i tried your query/code on my development system and it doesn't throw that error. are you sure the error isn't coming from some other query? if you are using exceptions and are not echoing the $sql statement and/or the line and file where the exception was thrown at, it could be from a completely different query statement. the error is typically when you have a different number of place-holders in the query than what you have bound. also, there's a slight chance that the actual value in $words could be causing such an error. what value are you testing with in $words?
-
define 'not working'? what symptom or error are you getting the leads you to believe it doesn't work? also, there may be a mysql version dependency that will prevent using any bound parameter in this type of comparison in a query statement and it may even be related to using emulated prepares (which is the default, unless you have specifically turned them off.)
-
the place-holder in the sql statement should not have single-quotes around it - $sql = "SELECT count(*) FROM posts WHERE MATCH (comments) AGAINST (:words IN BOOLEAN MODE)";
-
to do what you are trying - build, prepare, bind parameters, and run a sql query statement, you need to get each part correct. a good first step would be to build the sql query statement in a php variable, so that you can echo it to see what it actually is and so that the syntax of the sql query statement is separate from the php syntax that's preparing the query. another step would be to use a minimum of different syntax's in the query statement. don't use php functions if there is a mysql equivalent, do put php variables directly into the string without using concatenation, and don't put single-quotes around numbers. this is your existing query, built in a php variable, with a minimum of clutter in it - $query = " INSERT INTO $db_table_prefix.users (user_name, password, email, activation_token, last_activation_request, lost_password_request, active, title, sign_up_stamp, last_sign_in_stamp) VALUES (?, ?, ?, ?, ?, UNIX_TIMESTAMP(), 0, ?, 'New Member', UNIX_TIMESTAMP(), 0) "; from here, you need to do what ginerjm has written and make sure that your list of columns is correct, that the list of values matches those columns, and that for each place-holder ? in the sql query statement that the bind statement has the correct amount and type of data-type characters (the 'sssiiiisis' string) and the correct variables where the actual values are at.
-
that's because it's not entirely clear what you are stuck at doing or what your specific question is about. start by telling us or showing us what your input data is, what processing you need to perform on that data, and what result or output you expect from the processing of that data. to use an analogy, you are asking someone to draw you a map showing how to navigate between two locations. without knowing your exact starting location and destination, no accurate map between those two points can be drawn.
-
since search engines won't ever post data to your site, they should never get to the point of seeing one of your links containing an account verification token. it doesn't matter if the url is seo friendly or not. also, the token should be completely random and not tied to any user specific values. if you have the openssl_random_pseudo_bytes() function available, it is the current recommend method of generating a chosen length, random token.
-
I suggest making a do and don't section
mac_gyver replied to QuickOldCar's topic in PHPFreaks.com Website Feedback
there actually is a PHP FAQ/Code snippet repository - http://forums.phpfreaks.com/forum/32-faqcode-snippet-repository/ to the best of my knowledge, posts in it (edit: and in the current pinned faq topic in the php help forum) have only been linked to about a dozen times since i have been a member, the reason being is because a threaded forum is not a good way of organizing information. it would require being able to organize the information by category, alphabetically by title within each category, with a table of contents/navigation (edit: that doesn't require you to manually make links to the topics.) edit: and because the languages and clients/devices used on the web have evolved over time, you must also keep such information up to date. i just browsed through some of the existing information in that forum section, and some of it's out of date and in some cases was never good programming practices. -
you are using a mysql_fetch_array() statement with a mysqli_ database connection and query statement. you cannot mix mysql_ (no i) and mysqli_ (with an i) statements. if you had php's error_reporting set to E_ALL and display_errors set to ON, you would be getting an error at the mysql_fetch_array() statement alerting you to the mismatch.
-
What is wrong with my 'update' code for a CRUD system?
mac_gyver replied to jazza96's topic in PHP Coding Help
the only things positive about the tutorial you found are - 1) it has separated the business logic on each page from the presentation logic (but it should not have separate pages for each different type of action), 2) it is using php's filter function to validate the email (but it should be using similar functions to validate the other form fields), and 3) it is using PDO as the database library (but it is not using it very well.) the code is doing a bunch of things that are or can be problematic and should be avoided and not posted in a tutorial for others to repeat - 1) code that lets you alter data on the server should be secured so that only users with the proper permissions can change the data. 2) the code expects the id as a $_GET variable. it should only use $_GET['id'] to access the value. by using $_REQUEST, should the code where this is being used at ever add a $_POST['id'] or a $_COOKIE['id'] with a different meaning, these will overwrite the id that the code is expecting. 3) when there isn't a non empty id supplied to this code, it simply redirects back to the index.php page, without any indication of why. this will leave you wondering what the code did do. it is always best to display a specific error message for everything that your code detects that it didn't expect. the id is just another input value to the code on the page. it should be validated, with appropriate error message(s), the same as the $_POST data from the form. 4) the id is expected to be a positive integer. validating it and displaying an error message when it isn't an expected value will prevent nefarious use of your page (cross site scripting for example) and will help you when writing and debugging your code (if you have a coding error that didn't supply a proper id value.) 5) the header() redirects all need an exit; statement after them to prevent the remainder of the code after the header() statement from running. in the current code, if there isn't an id, but there is $_POST data, all that code will still run and attempt to update the row in the database, using a php null value as the row id (not sure, but that could throw query error/exception.) 6) the code should be using an array to hold the validation error messages. this will simplify the logic and would have prevented a problem that you introduced when you copy/pasted code to add the last name (you have added a line that set $valid = true;, that should have only been done once at the start of the code.) by using an array, such as $errors = array(); to hold the validation messages, you don't need to define individual variables for each error message and you don't need the $valid variable at all. you can just test if the array is empty or not to know if there are any validation errors. 7) in general, all external data should be trimmed before using it. the user may have accidentally entered white-space or non-printing characters before/after the value when typing it or copy/pasted it from somewhere and it could have white-space or non-printing characters before/after the value. the code creating a database connection and setting the database error mode should not be repeated in multiple places in the code - DRY (Don't Repeat Yourself.) in general, you should not have any repetition of code in your code. also, for pdo, the default is to emulate prepared queries. you should add a setAttribute() statement to set emulated prepares to false - $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES,false); this is a prime example of using DRY when programming. you should only have to find and make this change in one place in your code. the setAttribute() statements should actually be in the database class, right after you make the connection. 9) after successfully processing the submitted form data, the page should redirect back to the exact same url that the form submitted to, in order to cause a get request for that page. this will prevent the browser from attempting to resubmit the form data should you refresh the page or browse back to that same url. 10) any exceptions the pdo connect method or prepares/queries throw should be caught and handled so as to not expose the details contained in the php/mysql error messages to the visitors. 11) the database class shouldn't die() and display any connection errors (see item #10 above.) -
your questions are about application design, moving thread to that forum section...
-
Why is error checking being skipped upon post?
mac_gyver replied to greenace92's topic in PHP Coding Help
someone in the other php help forum showed how to reference the associative error array elements so that they won't produce php errors if the validation error isn't set - someone in the other php help forum even went to the trouble of taking the page layout you were shown and posted a working example using it, setting associative error array messages and displaying the errors next to each form field they apply to. why do you even have that line in your code? it wasn't in the original page layout you were shown. i'm pretty sure that someone else in the other help forum mentioned that the global keyword, the way you are using it, doesn't do anything (and in the case where it does do something, inside of a function definition, it shouldn't be used as it results in spaghetti code.) edit: lastly, why do you have an ob_start() statement in your code? all that typically results in hiding problems in code and messes with output your script tries to display. -
example #1 on the php.net documentation for the mysqli prepare statement shows a select query - http://php.net/manual/en/mysqli.prepare.php
-
PHP PDO variable not working in query but static value works
mac_gyver replied to ArshSingh's topic in PHP Coding Help
the symptom you are seeing is likely due to a page being requested twice, once with and once without the expected input data. and given that you are doing url rewriting, with your locahost/easy/u/5, this is even more likely. if you look in your web server's access log, i'm betting you have two requests for your page, one with and one without the correct get query string on it. you may even have a get query string that has added a space character as part of the value. if so, it's possibly due to a faulty url rewriting rule, or your page redirecting to itself a second time, or the browser requesting the page twice. however, ALL code should ALWAYS validate data before trying to use it. you need to make sure there's an expected value for the user id before ever calling the class method. making sure that the value contains all numerical characters (see ctype_digit()) and is not an empty string will at least verify that it contains a likely user id. also, why are you using a default $user_id = null call time parameter for a function that requires a value for that parameter to work (defining default parameter values are intended for optional parameters, where the function will still perform its intended purpose when the parameter is not present), assigning one variable to another ($user = $user_id;), and then looping over a result set that by definition will at most be one row? when we see code that contains extra lines and statements that don't accomplish anything useful, we wonder what you were thinking when you typed those things into your code? code should just contain those things that are necessary and that contribute to the goal that code is trying to accomplish. -
edit: pretty much says the same as kicken's post above the class code you started with was not designed very well. it is just some php functions stuck inside a class definition. the functions in it are for two separate things, the database connection/functions that use the connection and query results. all the code dealing with a query result should have been a separate class. the particular error you are getting is just a symptom of something that worked when using mysql functions, of getting the result resource id as an integer, that does not have any direct replacement of getting an object id as an integer (requires dumping the object handle and parsing the resulting string.) if the application code that makes use of this class expects it to maintain separate query results for multiple queries, there's no clean way of converting it that wouldn't also require making a corresponding change to the application code. if the application code that makes use of this class does not expect it to maintain separate/multiple query results, you can simply eliminate all the nonsense that's trying to use the result id/object id as an array index. if you do want/need to get the object id, see this link - http://www.sitepoint.com/forums/showthread.php?221775-PHP-5-and-Object-id
-
i'm going to guess that the symptom you got from your code was that it went to the thanks.html page when you clicked on the submit (link) and that it never actually went to the sign.php page? if so, the reason for that is because your form's submit button is incorrectly coded html. you have a mix of an <a> anchor tag (link) and a button, none of which is valid, and cannot submit a form without the aid of some javascript. it's not really possible to program without first learning the basics of what you are trying to do. you would need to research what the syntax of a form's submit button is before you can use it correctly in your form.
-
it needs to go immediately after the $dbh = new PDO(...) statement. it's likely that your create table query is failing and is interfering (a php bug perhaps) with the connection try/catch code. why do you even have a query inside your code that's responsible for making the database connection? also, your connection catch block needs to prevent the remainder of the database dependent code from running. if the connection failed. there's no point in trying to run any other database statements.
-
a) how big is this array of adjustments? b) is there a value in the array for every artist in the table or will there be some artists that don't have values in the adjustment array? c) i'm guessing the purpose of this is to produce a new rank that you will then want to replace the existing rank in the table? if so, telling us the final goal would let someone suggest a direct solution, rather than to sneak up on the problem one step at a time. just based on your description, create a temporary database table, insert the data id/adjustment, then join the temporary table with the existing database table using the id's, sum the rank and adjustment values in the now joined rows and use that in the order by term.
-
you would need to post that code as a starting point for us to have a chance at helping you with the problem. any chance you have code that opens a new/second database connection after your first connection, even if it is using the same php variable to hold the instance of the PDO class, but doesn't have the setAttribute() statements? any chance you are including a file with the database connection code in it, but the actual file being included is either an older file that doesn't have the setAttribute() statements in it or you have multiple files with the included file name at different paths and the wrong one is being included? short-answer: i can just about guarantee the problem is something your code is or is not doing and we would need to know as much about your code as you do in order to help you with the problem.
-
HTML5 Submit button not calling PHP file
mac_gyver replied to phpnewbieca's topic in PHP Coding Help
the actual code you posted for your form does not have the same html markup for the submit button that you supplied in the first post in this thread. your form is submitting (just tested) to the action page, but that page is likely redirecting back to your form since it doesn't see that there is any name='submit' field present in the form data. a little debugging on your part would have found that the test.php page is actually being requested. here's your free lecture to go along with your free programming help - programming is an exact science. every character you type in your code matters and often the letter-case of those characters matter. we only see the information you post and when you adulterate the information you post so that it doesn't match your actual code, we cannot possibly help you. the form page you posted contains over 250 html markup errors. you need to use css instead of all the obsolete elements and attributes. most of the markup errors were due to using obsolete styling, and the page is certainly not coded to the html5 specification. -
Small issue inserting and retrieving a row with php and ajax
mac_gyver replied to helloworld001's topic in PHP Coding Help
have you set the PDO error mode so that an error will throw an exception? id's in database tables are generally defined to be unique and trying to insert more than one row with the same id value could be (depending on your table definition) throwing an error. btw - in your ajax code, you need to prevent the default form action so that the form won't submit a second time. -
Using show/hide with multiple results.. one at a time
mac_gyver replied to SF23103's topic in Javascript Help
you need to use the this selector to reference the current element (to avoid having to manage id's for everything.) there's probably a better way of doing this, but here's one way - <div class="showmenu">Bob Smith <div class="menu" style="display: none;">Biography information goes here for bob Smith.</div> </div> <div class="showmenu">Jane Doe <div class="menu" style="display: none;">Biography information goes here for Jane Doe.</div> </div> $(document).ready(function() { $('.showmenu').click(function() { $('.menu',this).toggle("slide"); }); }); note: id's need to be unique, so i used a class for your showmenu. -
Saving data from all text boxes in one click
mac_gyver replied to harilalmn's topic in Application Design
you would just use the id value as the form element's array index value - name = 'utilTextbox[1]' when you loop over the submitted for data for that element, the array key is the id, the array value is the submitted value. -
Multiple queries issue when included in another file.
mac_gyver replied to SF23103's topic in PHP Coding Help
in addition to what Barand stated about not using global, you should not be trying to paste together web pages using php include/require statements. you should also not be making database connections/running queries inside of loops. displaying classes and the instructor information for each class, can be accomplished using one JOIN'ed query. you can even select the formatted date/time in the query. this will result in very little code - build and run ONE query, loop over the result from that query and display the result the way you want. i suspect the reason your existing code doesn't work is because the actual $row['Instructor'] value that is being used either contains some white-space as part of the data (in the ft_form_7 table) or the column name isn't exactly 'Instructor' (there would be a php undefined index error if you have php's error reporting turned on all the way.)- 3 replies
-
- multiple queries
- mysqli
-
(and 1 more)
Tagged with: