-
Posts
5,536 -
Joined
-
Days Won
192
Everything posted by mac_gyver
-
PHP Version 5.6.40, internal error for connect mysql
mac_gyver replied to nitiphone2021's topic in PHP Coding Help
http 500 errors are due to either php parse errors or fatal runtime errors. according the phpinfo output, you need to edit your php.ini and set error_reporting to E_ALL and set display_errors to ON so that php will help you by reporting and displaying all the errors it detects. stop and start your web server to get any changes made to the php.ini to take effect and then check in the phpinfo output to make sure the settings were actually changed. note: since you are using the procedural mysqli_connect(), you cannot use $conn->connect_error to test for a connation error. you will just get a php notice about trying to get property 'connect_error' of non-object ... and the code will continue to run as though the connection was successful, which may be the cause of the http 500 error later in code that's trying to use the connection. don't mix procedural and OOP mysqli statements. or even better yet, switch to use the simpler and more consistent PDO extension. it doesn't have all the problems that the mysqli extension has. -
in what php version does that not produce a syntax error? is that your EXACT code? if that was in fact, like this - $result = $mysqli -> query("SELECT * FROM mytable"); { // do something } php allows extra {} to exist that are not part of any actual statement, but they have no significance. the code inside of them is always executed. so, for the case of a successful query() statement, it would appear to you that doing this and using an if(){} are the same. however, as @Barand has posted, using an if(){} conditional around the query() code insures that the // do something code is only executed if the query() was successful. the problem with this is that nothing is done for the case where the query failed and in most cases a failed query is a fatal problem, due to a programming mistake. in this case, when learning, developing, and debugging, you would like the raw database error information to be displayed, so that you have immediate feedback as to if and why the query statement failed (when running this code on a live/public server, you would like this information to be logged instead.) the simple way of accomplishing this, without adding even more code at each database statement that can fail or editing/removing code when moving between development and a live server, is to use exceptions for database statement errors and in most cases let php catch and handle the exception, where php will use its error related settings to control what happens with the actual error information (database statement errors will 'automatically' get displayed/logged the same as php errors.) you can then remove any existing error handling logic for database statements as it will no longer get executed upon an error (execution transfers to the nearest correct type of exception handling upon an error.) your main code only has to deal with error free database statement execution, simplifying the code. if you want to do this, someone can post the statement that will set the mysqli error mode to use exceptions.
-
Splitting multidimensional array based on values
mac_gyver replied to 684425's topic in PHP Coding Help
example - <?php // index/pivot the data // note: if you are using the PDO extension, there's a fetch mode that will do this for you $data = []; while($row = whatever_fetch_statement_you_are_using) { $data[ $row['status_id'] ][] = $row; } // define output heading information $headings = []; $headings[1] = ['label'=>'Heading for status id 1 section']; $headings[2] = ['label'=>'Heading for status id 2 section']; // produce the output foreach($headings as $key=>$arr) { // start a new section echo "<h3>{$arr['label']}</h3>"; if(!isset($data[$key])) { echo "<p>NO DATA</p>"; } else { foreach($data[$key] as $row) { // reference elements in $row to produce the output... echo '<pre>'; print_r($row); echo '</pre>'; } } // any code needed to finish a section goes here... } -
Splitting multidimensional array based on values
mac_gyver replied to 684425's topic in PHP Coding Help
you would need to post the offending code to get the most direct help with what is wrong with it. however, you can probably solve this by indexing/pivoting the data, using the status_id value as the index, when you retrieve it. this will give you zero, one, or two sub-arrays of rows of matching data, depending on if there was no data at all, data for one or the other status_id value, or for both status_id values. you can then test/loop over the indexed/pivoted data to produce the output for none, one, the other, or both status_id values. -
apparently the INSERT part of the query is failing for these new rows. do you have any error handling for the database statements so that you would know if and why they are failing? temporarily add the following to your code to get php to report and display all errors, including database statement errors - // report all php errors error_reporting(-1); // display any reported errors ini_set('display_errors', '1'); // use exceptions for mysqli errors, which php will then report and display via an uncaught exception mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
-
PHP session randomly dying (Nginx)
mac_gyver replied to tonyfriz's topic in PHP Installation and Configuration
are there any ajax requests being made to a page that is also using session variables? what if any is the 'logout' code? can the logout code be reached by any other code not die'ing after a redirect? the quickest way of eliminating a lot of guessing is to just post the code, so that the problem can be narrowed down to just a few things that can be investigated further. -
you are either getting a fatal php syntax/parse error or a fatal run-time error. for both of these, get php to help you by finding the php.ini that php is using and set error_reporting to E_ALL and display_errors to ON, so that php will report and display all the errors it detects. stop and start your web server to insure any changes made to the php.ini take effect and use a phpinfo() statement in a .php script file to confirm that the settings took effect.
-
we don't know what your standard is for working or not working. you must tell or show what result you did get that leads you to believe that something didn't work.
-
is this code doing anything else with the submitted values beyond building the hidden fields in the form? btw - the sqTotal field being repeatedly output is pointless. it should just be output once, after the end of the looping, and i would even venture to guess that it is not needed anyways.
-
the code is blindly looping over 100 possible values, without testing if they exist before referencing them. this error was always occurring, but was probably not being reported or displayed. you should use an array for the form field, with the array index being the 0-99 value. this will eliminate the need for the logic to take the $_POST[sqNum_xx] value and put it into $SQarray[xx]. you will already have an array of data from the form that the if ( isset($SQarray[$i]) ) { can test and use.
-
Displaying data using href by $_GET['id'];
mac_gyver replied to Steve_Berry's topic in PHP Coding Help
how about doing the two tasks that were defined in your previous thread for this problem? producing navigation using all the rows of data and using the get input to query for the matching (one) row of data. -
start by getting php and the database to help you. do you have php's error_reporting set to E_ALL and display_errors set to ON, preferably in the php.ini on your system, so that php will help you by reporting and displaying all the errors it detects? do you have error handling for all the database statements that can fail - connection, query, prepare, and execute, so that you will know if and why they are failing? the easiest way of adding error handling for database statements, WITHOUT adding logic at each one, is to use exceptions for database statement errors and in most cases let php catch and handle the exception, where php will use its error related settings (see the above paragraph) to control what happens with the actual error information, via an uncaught exception (database statement errors will ‘automatically’ get displayed/logged the same as php errors.) to enable exceptions for errors for the mysqli extension, add the following line of code before the point where you make the connection - mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
-
a. any substitution or processing of output, should occur when you output the data, not when you store the data. b. if you have a need to store data containing sql special characters, quotes in this case, you would apply any escape_string() function or even better yet, simply use a prepared query, right before executing the query, not prior to content being added to the data that contains quotes.
-
the sql query you are showing and the output you are (trying) to produce from that query make no sense. if this was working, you are querying for the row of data WHERE the id column matches the $_GET['id'] value and looping to produce a (one) link with ?id=$row['id'] in it, i.e. a link containing the same id that was in $_GET['id']. you need to step back and come up with a stateable definition of what your code needs to do. you are doing two things, 1) producing navigation links, and 2) when a link has been clicked, you are displaying the content that corresponds to the id in the clicked link. to do item #1, you would query to get ALL the page ids, and page names/titles, which i assume are in the menuheader column (if you list out the columns you are selecting in a query, rather than using *, it helps make your code/query self-documenting.) you would then test and loop over the result from this query to produce navigation links. Note: almost every if() conditional test needs an else so that code does something when the main condition fails. for navigation link data, if there is no data, rather than outputting nothing, you should output a 'sorry, nothing to display' or similar message. to do item #2, you would test for a $_GET['id'] value and use it to query to get the matching row of data, fetch that single row of data (no loop needed), and if there was a matching row of data, output the content for the page. if there was no matching row of data, you would instead output a 'nothing to display' or similar message.
-
that's not the FromData object, as was mentioned, and the jquery .serialize() method doesn't include the file data.
-
in order to upload a file using ajax, you need to use the FormData() object (current, best method) - https://developer.mozilla.org/en-US/docs/Web/API/FormData
-
since a button of type = 'button' doesn't do anything by itself and you are outputting json encoded data back to the browser, what is the javascript/ajax code that's trying to upload the file?
-
results are sorted by directory name and not date
mac_gyver replied to Tim32's topic in PHP Coding Help
see the usort() function - https://www.php.net/manual/en/function.usort.php -
all you have done is replace the single $row = $query->fetch_assoc(); statement with one inside of a while(){} loop. how is that going to make this work? rather than starting new threads for the same problem, read the reply, on how to correct this, that you have already gotten in the first thread.
-
just fetch all the rows of data into an appropriately named php array variable, then use the contents of that variable in your html document. if the variable is empty, no rows of data were matched. to display the number of rows of data, use php's count() function. to get a copy of the common values for displaying the one-time heading, just reference the zero'th row. to loop over the data, use a foreach(){} loop.
-
database connections are resources. all resources on a web page are destroyed by php when the php script ends, i.e. you cannot pass a database connection in a session variable. you must make a new database connection on any page that needs one. as to the http 500 error, you have a php syntax error in the code on that page. find the php.ini that php is using and set error_reporting to E_ALL and display_errors to ON, so that php will help you by reporting and displaying ALL the errors it detects.
-
your posted code has one technical issue, in that it only stores the last validation error in the $error variable, so, if there are multiple validation errors, you would only see the last error message. using an array to hold the error messages will solve this, and using the field name as the array index will let you test for and display the messages adjacent to the fields they belong with. i recommend displaying any error above or next to the field, rather than below it, in case the field is at the bottom of the screen and anything below it might not get seen. does your posted code operate as i have described above or does it appear to insert empty values when you don't enter anything in the form fields? if so, i suspect that your html markup has some white-space as the field value attributes, which won't be considered as empty(). correcting the html mark would correct this, but trimming the data as suggested would handle the case where a visitor accidentally enters space character(s) in a required field. do you have a specific question, problem, or error concerning the suggestions?
-
external data can be anything and can come from anywhere. you must validate data on the server before using it. your form processing code should - detect that a post method form was submitted. trim, than validate all inputs, storing validation errors in an array, using the field name as the array index. if there are no errors (the errors array is empty), use the submitted data.
-
Check if data exist before insert not working
mac_gyver replied to mahenda's topic in PHP Coding Help
a phone number, despite being called a number, isn't an integer. it is a formatted string consisting of 3 or 4 fields, depending on which country you live in and if you are including the country code with international numbers. the signed integer you are using (int(12) isn't even valid) can only hold a value up to 2147483647 (214 748 3647) which can only store some US phone numbers up to area-code 214. use a string data type and format the value into a common format before using it. once you define the column with a usable data type and as a unique index, just attempt to insert the data and detect if a duplicate index error number occurred to determine if the phone number already exists.