-
Posts
5,449 -
Joined
-
Days Won
174
Everything posted by mac_gyver
-
@ryanmetzler3, the point of programming help is to find what is causing the problem and fix it. the line of code you posted, besides being invalid php, is just slapping a band-aid over the top of the problem. you would use isset() (it requires a variable as a parameter, not an expression) to test for an optional variable, one that may or may not exist during normal program execution. in this case the variable is expected to exist if the form has been submitted (even if no file was selected) and the problem is one of a half-dozen things that could cause the expected index to not exist.
-
you would use an array name for the form field. see this link - http://php.net/html#faq.html.arrays the array index would be the id - $id = from your database ... echo "<input type='text' name='weight[$id]' >"; the submitted array would be in $_POST['weight'], which you could/would loop over using a foreach loop to get the id and the corresponding value.
-
do you have php's error_reporting set to E_ALL and display_errors set to ON so that any php detected errors will be reported and displayed? what symptom are you getting that leads you to believe the code isn't working? are you catching the exception from that code and what are you doing then in the catch code? do you have php's output_buffing on, in your php.ini or in your script, and you are either redirecting or intentionally discarding the buffer so that any error output from the code is lost?
-
the original syntax is valid. the query does need to ALWAYS be separated out and checked to see if it worked (and debugged if not to find out why it is failing - no database selected, problem with table/column names, problem with the connection...) before you can use any result that the query was expected to return.
-
a little hand please (proof read work cant see error)
mac_gyver replied to Lone_Ranger's topic in PHP Coding Help
sorry for the blunt commentary, but the posted code is so far from what it needs to be in order to work. you need to slow down and organize what you trying to do before you try to write the code to do it. for that specific symptom, your form does not have a method specified, so the get method is used as the default. when you have a get method form, the form data replaces anything you coded on the end of the url in the action attribute. also, an image as a submit button submits the coordinates where it was clicked. either use the post method or put the action value into a hidden form field. some issues with the code (most of these are consistency issues and constancy counts in programming) - 1) you need to pick one library of database of functions to use and stick to that one library. you apparently have an include file making a msyql_ connection. why are you then making a mysqli connection later in the code, that isn't actually used? btw - the mysql_ functions are depreciated and new code should use either the msyqli or PDO database library of functions. 2) you are switching back and forth between using die() and trigger_error() statements. pick one and stick with it. 3) you are putting or die()/or trigger_error() statements on the end of string assignment statements. why are you doing that? a string assignment, unless it is an empty string or a zero value won't fail and cause the or ... statement to run. (this is a rhetorical question, i already know why you are doing it, you are copy/pasting things without knowing what they mean, but you must know and have a state-able purpose for every statement you put into your code.) for debugging purposes, you would need to put the or die()/or trigger_error() statements on the end of your mysql_query() statements. 4) your code on this page is dependent on someone being logged in. you should test for the logged-in state, first, once, not multiple times and not after you have already tried to use the value in the logged-in session variable. 5) you are storing the online/offline status in two tables. this is redundant and just results in more code you have to write and debug. 6) it's likely several of your database queries are related and should be consolidated into JOIN'ed queries, but until the code is cleaned up, it's hard to tell what the actual logic/goal of the code is. 7) the code handles multiple action== "..." values. the common code on the page shouldn't be repeated within each action block. see this link - http://en.wikipedia.org/wiki/Don%27t_repeat_yourself each action block should produce only the unique output it is responsible for and your basic page layout should be separate and at the end of your code. you would just echo the unique output where it is needed in the page layout. you are running separate database queries getting the online/offline status rows separately. why not use one query, ordering the rows by the online/offline status? it also appears for this specific set of queries that earlier in the code you are querying for this same data to get a count. all three queries can be replaced with one query, then just use the result from that one query as needed. 9) related to item #4, ALL the action code on the page would need to test if the current user is logged in and if the input values that action code expects actually exists. however, by separating out the logged-in test and putting it up near the start of the page, you can easily insure that the code on the page will only run if the current visitor is logged in. it appears you are just piecing together code without a plan. this just results in excessive and repetitive logic, making it harder for you to keep track of what your code is doing or supposed to do, and making it harder for anyone trying to help with the code to even see what goal the code is trying to accomplish. -
a little hand please (proof read work cant see error)
mac_gyver replied to Lone_Ranger's topic in PHP Coding Help
you need to tell us what sort of symptoms or errors you are getting to narrow down the problem, since the problem could be something to do with your server/browser, which we cannot duplicate. -
i'm curious how you managed to post this thread three times? there have been other recent double/triple posts. did the forum software act like it wasn't accepting the form submission and you hit the submit button multiple times? are you using a mobile device?
-
the suggestion to use ob_end_flush won't have any effect on your live server and at best is putting a band-aid on top of a special case condition on your development system. did you even try to change the output_buffering setting in your php.ini? btw - changing the setting won't fix anything, it will just make your development system operate the same as your live server and allow you to fix this problem in your code and avoid this problem in any future code you write and test on your development system. edit: and i just noticed the forum section where this thread is at and the code/problem has nothing to do with the php application, frameworks... forum section. moving the php help forum section....
-
Quick question. - Accessing Database with MySQL and PHP
mac_gyver replied to GumbiRo's topic in PHP Coding Help
sorry to be blunt, but you need to add an item 0 to your list of steps - Steps: 0. pick the database library you intend to use and learn how to make a connection, form and execuite a query, check if the query ran, and retrieve the result from the query and if using prepared queries, how to form and prepare the query, bind any inputs, execute the query, bind any result, and retrieve the result from the query. 1.check user input. 2.Check database for duplicate. 3.Return false/true. before you can write code that uses a database for your data, you must learn how to use the database statements at all. this thread is just randomly trying things and that is a waste of time when trying to program. leave prepared queries for later, just get a normal mysqli query to work, escaping input values as needed. here are some things about the code in general that need to be changed/fixed - 1) your form processing code needs to test if a form was submitted at all. you should not run the posted code unless you know the form has been submitted. 2) you need to validate the expected form data to insure that it is at least not empty. there's no point in trying to use empty data as that just wastes resources running queries that won't match anything. your current code would happily insert empty data into your database. 3) code that combines conditions like this - if ($result && mysql_num_rows($result) > 0) doesn't do what you think. if the query is failing due to an error, this combined test will say that the username is not in use, when in fact it could be in use. you must first test for query errors, and handle them separately, before you can use the result from a query. P.S. for this specific line of code, you would need to use the mysqli form of num_rows. -
the output_buffering setting is turned on in your php.ini on your development system. if you turn it off, the code you develop will work regardless of the setting and will work on your current web hosting.
-
Quick question. - Accessing Database with MySQL and PHP
mac_gyver replied to GumbiRo's topic in PHP Coding Help
you wouldn't use a prepared query AND put an escaped value into it. you would use a place-holder in the prepared query and bind the actual value before executing the query. if you are escaping and putting the value into the query, there's no point in going through the extra statements needed for a prepared query. what is your actual code that is running this prepared query? -
after a little research and experimentation - <!DOCTYPE html> <html> <head> <script type='text/javascript' src='jquery-2.0.3.js'></script> <script> $(document).ready(function(){ $(".flip").dblclick(function(){ $("#panel" + $(this).attr("id")).slideToggle("slow"); }); }); </script> <style type="text/css"> .panel{ padding:5px; text-align:center; background-color:purple; border:solid 1px #c3c3c3; } .panel{ padding:10px; display:none; } .flip{ padding:5px; text-align:center; background-color:#e5eecc; border:solid 1px #c3c3c3; } </style> </head> <body> <div > <?php // ... your database code was here.... echo "<table border='1'> <tr> <th>Firstname</th> <th>Lastname</th> <th>Age</th> <th>Hometown</th> <th>Job</th> </tr>"; $i = 1; while($row = mysqli_fetch_array($result)) { echo "<tr class='flip' id='$i'>"; echo "<td>" . $row['FirstName'] . "</td>"; echo "<td>" . $row['LastName'] . "</td>"; echo "<td>" . $row['Age'] . "</td>"; echo "<td>" . $row['Hometown'] . "</td>"; echo "<td>" . $row['Job'] . "</td>"; echo "</tr>"; echo "<tr class='panel' id='panel$i'>"; echo "<form action='#' method='GET'>"; echo "<td><input type='text' value='".$row['FirstName']."'></input></td>"; echo "<td><input type='text' value='".$row['LastName']."'></input></td>"; echo "<td><input type='text' value='".$row['Age']."'></input></td>"; echo "<td><input type='text' value='".$row['Hometown']."'></input></td>"; echo "<td><input type='text' value='".$row['Job']."'></input></td>"; echo "<td><input type='submit'></input></td>"; echo "</form>"; echo "</td>\n"; $i++; } echo "</table>"; ?> </div> </body> </html>
-
a) id's must be unique. you would need to use a class to assign the event handler to each region you want it to work with. you would also need to use a class for the css. b) to reference the element that triggered the event, you need to use $(this) (i'm not sure of the actual usage, not being a jquery guru.)
-
you mentioned '... and not loop'. i'm guessing you have a redirect loop? if so, why are you redirecting on line 5 of your global.php file when the session based username is set? here's a more basic question. what are you trying to accomplish with your "scripts/global.php" file? you need a clear statement of what the purpose of that code is and the confusing code you have now doesn't appear to have a defined goal. are you trying to include that on any 'protected' page and a) have it present the login form if the visitor is not logged in, process the login form on that same page, and process the log out action on that same page, and b) only allow access to the protected page if the visitor is logged in? edit: there are cases where you want/need to query the database on each page request, i.e to get a 'banned' status that takes affect immediately, to allow usernames/access level to be changed and take affect immediately. in these cases, your session variable only identifies who the visitor is, by his user_id. you would use the user_id to retrieve the rest of the user information to be used on the page request. for a "remember me" feature, you would generate a unique id (see: the uniqid() function) and store this in the cookie and in a column in your clients table. this unique id will only identify who the visitor is. if it is set, you would use it to query for the actual user information it corresponds to.
-
if all you are doing is posting your existing code, without showing your attempt at accomplishing the stated goal, we cannot help you because you haven't provided any information about what you tried and what errors or symptoms you got when you tried, so we don't have a clue what to help you with. if you are having trouble finding example code or understanding the code you have found, you would need to ask specific questions to get help, because again, without specific information from you we don't have a clue what to help you with, because we are not here to find or to write code for you.
-
blank .php pages are usually the result of a fatal php parse error, a fatal php runtime error, or code that simply doesn't output anything. for the first two possibilities, you need to have php's error_reporting set to E_ALL and display_errors set to ON in the php.ini on your development system to get php to report and display all the errors it detects. for the last possibility, deliberately echo some test string in your php code, outside of any conditional logic statement, to see if it is displayed.
-
using php array functions, you can very quickly produce a number of option menus with a dynamic preselected option - $array[1] = array('id'=>1,'name' => 'namea'); $array[2] = array('id'=>2,'name' => 'nameb'); $array[3] = array('id'=>3,'name' => 'namec'); $array[4] = array('id'=>4,'name' => 'named'); function _options($arr){ $selected = isset($arr['selected']) ? " selected='selected'" : ''; return "<option value='{$arr['id']}'$selected>{$arr['name']}</option>"; } $current_options = $array; // get a copy of the list of options // preselect an option(s) - works with any quantity of preselected options in one menu $current_options[2]['selected'] = true; // set an element for a selected option $options = array_map('_options',$current_options); echo implode("\n",$options);
-
you would make the select menu with all the choices, but pre-select the option entry that matches the value from each the current record.
-
this is a very common error. see the following - http://forums.phpfreaks.com/topic/273121-readme-php-resources-faqs/?do=findComment&comment=1428660
-
Is this a functional way of exploding userID+update mysql DB CONCAT
mac_gyver replied to slyte33's topic in PHP Coding Help
by storing each data item in its own row, you can perform operations directly using queries. to add an item, just inserting a row (if you set up a unique key, you don't even need to try to select the item first to test if it already exists before trying to insert it). to delete an item, simply delete the row. you can get a count or retrieve one, any, or all matching values directly in a query. you can run one JOIN'ed query using the values to get related information (such as getting the friend's usernames based on the stored friend's ids.) storing a list requires two queries and a bunch of php code to accomplish any of these operations. -
the symptom is that of the code on the server not being the actual code you are looking at in your editor. the mysql_pconnect will work just fine (it gets a regular connection when on a server that doesn't support a persistent connection.) it's more likely that when you changed the code, you actually saved it/uploaded it onto the server and replaced whatever was actually running on the server.
-
you are comparing $d['user_id'] with $_SESSION['user_id']. for equal/not-equal, there are only two choices. they either are equal to each other or they are not.
-
Is this a functional way of exploding userID+update mysql DB CONCAT
mac_gyver replied to slyte33's topic in PHP Coding Help
forum software generally stores unread posts, which you only need to store for active users. the id of the last post at the time of the last visit is stored for each user. on the next visit, posts made since the last visit are added to the 'unread' table and the id of the last post is stored. as the posts are read, the row with their id/the user's id is removed from the 'unread' table. also, there is generally a 'mark all posts are read' button that simply removes all that visitor's records from the 'unread' table. -
a) one equal = is an assignment operator. two == is a comparison operator. this is a first-week beginner mistake. you have 400+ posts over years of being a member on this forum. why are you making such a mistake at this point in time? b) you should almost never run queries inside of loops. for inserting/updating a large amount of data, you should be using one of the following methods - 1 - a multi-value INSERT .... ON DUPLICATE KEY UPDATE query 2 - a multi-value INSERT IGNORE query 3 - a multi-value REPLACE