Jump to content

mac_gyver

Staff Alumni
  • Posts

    5,536
  • Joined

  • Days Won

    192

Everything posted by mac_gyver

  1. the database user you are using for the php connection probably doesn't have permission to create events. have you set the error mode to exceptions on your pdo connection so that any errors will throw exceptions, and are you either catching the exceptions/providing your own exception handler and doing something with the error information OR you have php's error_reporting set to E_ALL and display_errors set to ON so that any uncaught exceptions would be reported and displayed?
  2. if would be a little conditional logic - // example data $data[] = array('date'=>'2015-12-10','date_from'=>'','date_to'=>'','name'=>'event1'); $data[] = array('date'=>'2015-12-11','date_from'=>'','date_to'=>'','name'=>'event2'); $data[] = array('date'=>'2015-12-11','date_from'=>'','date_to'=>'','name'=>'event3'); $data[] = array('date'=>'','date_from'=>'2015-12-09','date_to'=>'2015-12-10','name'=>'event4'); $data[] = array('date'=>'','date_from'=>'2015-12-20','date_to'=>'2015-12-29','name'=>'event5'); $last_value = null; // use to detect date change for multiple events on a date $first_pass = true; // used to output a one time label foreach($data as $row){ // loop over the data. you can either fetch all your rows into an array named $data, or replace this line of code with a while() loop that loops over the result set from your query if($row['date']){ // there is a specific date, do the specific date handling // if the date changes or is the first one, output a new heading if($last_value != $row['date']){ // save the new date as the last value $last_value = $row['date']; // format the date for display $date = date_create($row['date']); echo date_format($date, 'F j') . '<br>'; } // output the data under the heading echo $row['name'] . '<br>'; } else { // there is not a specific date, do the general date handling // output a one time label for this section if($first_pass){ echo "Ongoing Events -<br>"; $first_pass = false; } // format the dates for display $date1 = date_create($row['date_from']); $date2 = date_create($row['date_to']); echo date_format($date1, 'F j') . ' to ' . date_format($date2, 'F j') . ' ' . $row['name'] . '<br>'; } } the ORDER BY term you have in your query should produce the correct output. you would want the rows, having a date, first in the result set.
  3. you have got to be kidding - either upgrade your php version to at least V5.5 or use standard array syntax, that works with all php versions.
  4. the syntax you are using on line 13 is only available in php5.5 and above.
  5. because you are using the IGNORE keyword in the INSERT query, you are probably triggering some unique index/key error. you would only use the IGNORE keyword when you want to silently ignore duplicate data and the index/key errors they cause. however, just about everything your code is doing is has a problem. the biggest problem is your food database table should NOT have columns like - eggs_price, bread_price. the data for each item, eggs, bread, ... should be a separate row in the table, with an item_id column that identifies which item the price is for. another BIG problem is the massive amount of logic you have (or are going to write) that only differs in the the values being tested/used. if you use an array to hold the weekday/hour data, all that $weekday/switch($hour) logic can be replaced with a few lines of code. lastly, what is the overall goal of doing this? you are apparently calculating prices for each hour for each day of the week a date falls on. this is derived data and you will end up with a massive amount of stored data as the number of dates increases. in general, you would not do it this way, but store the weekday/hour data in a data structure (database table, array) and calculate the price as needed.
  6. string data that may (in this case does) contain sql special characters (characters that have meaning in the sql syntax), such as NULL (ASCII 0), new-line, carriage-return, \, ', ", and Control-Z, must be escaped (or use prepared queries) so that the sql special characters don't break the sql syntax. for the mysql database, the escape character is a \. if you are manually typing the sql query statement in your code, you can just add the \ before each of the the single-quotes that are inside the sql query statement. if this is not static data that's been typed in your code, you should either use the escape_string function/method for the php database api you are using or use prepared queries, which separate the sql syntax from the data values being used in the query.
  7. the php error on line 8 is because the name='...' attribute for your <textarea> is 'text' the php error on line 12 is/was because of the repeated $name = assignments. i recommend that you first get your form and your form processing code to work correctly, then add any javascript validation and using ajax to submit the form data. edit: the only thing changing the code testing $message is doing is hiding the fact that your form field isn't named 'message.' the $message variable is set, because your code is setting it. !isset() will never cause your 'please provide a message' to ever be output.
  8. you are also getting php error messages being sent back. primarily because there are no $_POST['message'] and $_POST['honeypot'] in the data you are sending to the php code. you can find these if you look at the response sent back to the browser's developer tools -
  9. for ease of use and code readability, you need to end up with the language strings either in defined constants or an array, with the defined constant name or the array index indicating the meaning of the string. you can dynamically produce the defined constants/array from wherever the data is actually stored at. for these two examples, the Italian language was selected. only the building of the defined constant/array is dependent on the language choice. the actual usage in the code is the same regardless of the language choice. if you use the defined constant method - // definition define('LANG_contact', 'Contatto'); // the define() statements can be dynamically called if the data is stored in a database table. // usage echo LANG_contact; if you use the array method - // definition $lang['contact'] = 'Contatto'; // the array can be build dynamically if the data is stored in a database table. // usage echo $lang['contact'];
  10. your alter table query is using the php mysql function to run the query. you are however apparently using either the mysqli or the PDO classes for your connection and the rest of your code. as was stated in one of your previous threads, you cannot mix the different php database functions together for a single connection. if your mysql based statements appear to be running without producing database errors, it's because most of the all-in-one xAMP packages set up default mysql database credentials that allow mysql statements to make a database connection if there isn't already one. also, please do NOT suppress any errors in your code. remove any @ error suppressors and make sure that php's error_reporting is set to E_ALL and display_errors is set to ON in the php.ini on your development system. if you are tyring to learn php or learn anything new in php, developing php code, or trying to debug problems in php code, you want ALL THE HELP YOU CAN GET from php. you don't want to hide any of the php errors.
  11. the part between the (...) is.
  12. the error means that the list of columns in an INSERT query doesn't match the list of values you are supplying in the INSERT query. either the list of columns is wrong or the list of values is wrong or there's something wrong with the syntax so that the number of columns or values is ambiguous.
  13. there are two possibilities, either the ->get_result() method is not available (your php version is right, but the driver that php uses must also be the mysqlnd driver) or your code has some non-printing/character-encoded characters as part of that statement. 1) for the first possibility, does the output from a phpinfo() statement have a main section for the mysqlnd driver? the msyqlnd name and version number should also show up under the msyqli main section as part of the Client API library version information. 2) if you copy/pasted the code or are typing code on a non-English keyboard/operating system, i would delete and retype the entire line of code where the error is occurring at, making sure that only ASCII characters are being used. as to PDO examples, your previous code using PDO would be where to look. all you are doing is preparing the sql statement, binding any input data, executing the query, and fetching any result.
  14. the information you just posted is for your mysql server. what matters, and what i asked about, is your php version.
  15. what php version are you using? and for the the code in post #3, is that code being ran on the same server as the current problem code and are you sure it is actually taking the if ($result) { execution path? i can think of at least one case where the INSERT query in the code in post #3 is failing due to an error, when there is already a row in that database table due to previous code-version/testing, and it isn't trying to run the code where the ->get_result() method call is at. are you open to using the PDO database class instead or the mysqli class? PDO is much more constant and easy to use, especially with prepared queries. edit: i just looked at your previous threads. you are/were using PDO. why did you take a step backwards with this code and use mysqli? edit2: and the Call to undefined method mysqli_stmt::get_result() error is exactly the same error you were getting in a thread on November the 2nd. so, i doubt your code in post #3 in this thread is actually doing what you think or if it is running, it's not running on the same server as the current code.
  16. see the three different 'version A, B, C' examples in this code - <form action='inc_artist_search_box.php' method='get'> <p>Search for your favorite artist, song, or label</p> <input type='text' name='userSearch' value='<?php echo $_GET['userSearch']; ?>' /> <input type='submit' value='Search' /> </form> <hr /> <?php // make the database connection first. a database connection is required for the mysqli_real_escape_string() function that's being used when the sql search term is being built $servername = '127.0.0.1'; $username = 'root'; $password = 'Conquest1'; // Create connection $conn = new mysqli($servername, $username, $password); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } echo "Connected successfully"; //select database if(mysqli_select_db($conn,"Artist")) echo "connection successful"; else echo "connection failed"; $k= isset($_GET['userSearch']) ? trim($_GET['userSearch']) : ''; //get user search term // your code implies that a search term is required (an empty WHERE in the sql statement would result in a query error) // if you instead want to match all rows when there isn't a search term, the WHERE keyword in the sql statement would need to be added only if there is a where term or you would need to supply a true value after the empty WHERE keyword if($k == ''){ echo 'A search term is required'; } else { $terms = array(); $search=explode(" ", $k);//user search term into array //loop through search term and find terms that are like query request // if the columns to include in the search is selectable (checkboxes?) see the 'version C example code' below // you would build the $search_columns variable at this point in the code with whatever columns you want // if you are just asking to always search a list of columns, you can either use the 'version B example code' below // or you could build the $search_columns variable at this point with the columns hard-coded in it and use the 'version C example code' below foreach($search as $each){ $each = trim($each); if($each != ''){ $each = $conn->real_escape_string($each); // if not using a prepared query, escape the string data // **** version A example code - duplicates what you posted at the start of the thread $terms[] = "first_name LIKE '%$each%'"; // **** version B example code - // to unconditionally search multiple columns // replace the above line of code with this - $terms[] = "CONCAT_WS(' ',first_name, last_name, groups) LIKE '%$each%'"; // **** version C example code - // if the columns to be included in the search are picked via some section process (checkboxes?), // you would build the list of column names in a php variable before the start of this loop, // then just use that variable in this code. // note: do NOT put raw external user data into a query. if the column names are coming directly from user supplied data, you MUST validate that they are exactly and only permitted column names. no amount of escaping the values will protect against sql injection because these are not string data values. // replace the above line of code with this - $terms[] = "CONCAT_WS(' ',$search_columns) LIKE '%$each%'"; } } $query = "Select first_name, last_name, groups, era FROM artist_table WHERE " . implode(' OR ', $terms); $query=$conn->query($query);//perform the query $numrows=mysqli_num_rows($query);//number of rows in query results found //loop throw rows and extra data if($numrows > 0){ while($row=mysqli_fetch_assoc($query)){ $first_name=$row['first_name']; $last_name=$row['last_name']; $group=$row['group']; $era=$row['era']; echo $first_name; echo $last_name; echo $group; echo $era; } } else echo"No search results found for \"<b>$k</b>\""; }
  17. the <head></head> section of your html document doesn't have anything to do with this. this concerns the http protocol and what gets sent back and forth between the browser and the server before you send any character of data between the two.
  18. that's because you are asking the query to search for either anything that contains a space - %space here% or anything %%. you should probably trim() the $each value and only use it if it is not an empty string, which is different than using empty(), which would treat a search for a 0 (zero) as empty. the coding suggestions that have been made will actually reduce and simply the amount of code it takes to build the sql statement.
  19. if you are going to be doing this in the sql statement, here's another tip that will simplify the sql and may improve the performance of the query (your LIKE term, with a leading % isn't using an index anyway.) you can concatenate the database columns you are searching, and use one LIKE comparison for each of the search keywords (the following would be what gets imploded/joined with the ' OR ' between them.) the sql would look like - CONCAT_WS(' ',first_name, last_name, groups) LIKE '%$each%' in this case, you would also dynamically build the list of column names to put into the CONCATE_WS(' ',...) syntax based on whatever inputs you are testing.
  20. as you are testing the inputs to determine what terms should be part of the sql statement, you need to add each OR'ed term to an array, then just implode/join the array elements with the ' OR ' condition between them. this will simplify your code (you won't have to test if any term is the first one.) you should also use a prepared query, rather than to put your $each search term directly into the sql statement. see the following thread - http://forums.phpfreaks.com/topic/299482-php-pdo-how-to-bind-values-to-varibale-contains-concatenated-string/
  21. this is a very common error. if you had searched for it (there's even a thread from this time yesterday with the same error - http://forums.phpfreaks.com/topic/299626-cannot-modify-header-information/ ) you would have found what it means. YOU CANNOT SEND ANYTHING* TO THE BROWSER BEFORE YOU USE A HEADER(), SESSION_START(), OR SETCOOKE() STATEMENT. the http protocol REQUIRES that any 'control' headers be sent to the browser before you send anything else to the browser. * - anything includes a BOM (Byte Order Mark) character that your editor saved at the start of your file, any characters outside of <?php ?> tags, even if those characters are white-space characters that you cannot see or are not rendered for display by the browser, the <doctype tag and any other html/javascrpt/css markup are CHARACTERS being sent to the browser, anything your php code echos, prints, var_dump()s, or print_r()s, or anything that php outputs, such as a php error message. your php header() statement must be located in your code before you start sending the html/javascript/css to the browser. your page control logic that determines what to do on the page, or even if you are going to stay on the page, should be near the start of your code. you should only produce and send html/javascript/css to the browser after you have determined if you are even going to stay on the page. edit: see the following post for a suggested page layout that will help make your code fool proof - http://forums.phpfreaks.com/topic/297824-database-issues-and-working/?do=findComment&comment=1519095 your page access level check/header() redirect would be part of item #3 in that page layout. all your code for producing and outputting the web page would fall under items 5-8 in that list.
  22. similar to Barand's reply, you have both too much program logic and it's not organized. it's also doing things that don't make sense in the context where you are doing them (htmlentities() is an output function and should only be used on values being output to the browser, not on values that you are using in a database query.) for your actual problem, you are dong a header redirect after you have output an error message for a failed prepare() statement (you have a sql syntax error in the update query.) you will never see that error message, because php/most of the all-in-one development systems have output_buffering turned on by default and any output your code produced is lost when the redirect occurs. prepare() and execute()/query() errors are fatal problems for code that's database dependent. your code should have logic that handles all database errors. the database error information should be displayed when learning, developing, or debugging code, and it should be logged when your code is running on a live server. when there is a database error, your code should not continue running as though no error occurred. it's easiest to handle fatal errors within your code by using exceptions. you won't have to put a conditional statement around each database function that can fail (you don't have any conditional test around your ->execute() statements anyways.) for code that will never be used on a live server, you can just enable exceptions for the database statements, without catching them or supplying your own exception handler, to get the error information to be output (uncaught exceptions are fatal php errors and php will stop program execution if one occurs..) to enable exceptions for the mysqli_ statements, add the following two lines of code - $driver = new mysqli_driver(); $driver->report_mode = MYSQLI_REPORT_ALL; // note: this will also throw exceptions for queries that don't use an index. if you only want excpetions for actual errors, use MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT
  23. we know what you are trying to do, the point is, in order for you to do it, and not just us fixing each thing that's wrong with your code, you must know what the output is supposed to be and when it isn't the correct output, to debug what your code is doing to find why it isn't the expected output. i recommend that you look at the 'view source' of your page in your browser. you will see a php/mysql error at the bottom that would help you determine why it currently isn't working. if you had organized your code as suggested in post #40 in this thread, with any database dependent code separate from any html markup, the php error wouldn't be buried in the 'view source'.
  24. you need to determine what your script is doing, in order to pin down where and what the problem is, before you can fix it. just randomly changing things without any purpose or plan won't produce any result. is the html markup correct for the form, the select menu, and option choices within the select menu?
  25. the problem is line 88 in your REAL code, along with including/defining/making two database connections. the code in post #24, that you stated was your real code, had lines 69-89 removed. it turns out on line 88 you are closing the database connection, so, any use of the database after that point will produce the error in question. rather than to piece together blocks of code to build your web page, you need to organize your program logic so that any database specific code is grouped together and it all uses a single database connection. see the following recommended layout - http://forums.phpfreaks.com/topic/297824-database-issues-and-working/?do=findComment&comment=1519095 either don't close your database connection and let php close it for you or close it after the point where you have finished with database queries.
×
×
  • 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.