Psycho
Moderators-
Posts
12,157 -
Joined
-
Last visited
-
Days Won
129
Everything posted by Psycho
-
The "acer-aspire-e1-571G" is almost definitely not the "name" of the product, but is instead a unique code. So, the query is doing a search on a single column for a matching code.
-
Array row count is right, but can't see values
Psycho replied to mattward's topic in PHP Coding Help
Have you verified that you are connecting to the database? You don't show that code. My assumption is that this condition is returning false if($conn = dbConnect('username')) Also, you say "dropdowns". If you have multiple select lists that you are doing this on you should definitely create a function. -
FWIW: Displaying any information about a password - even it's length is a bad practice. You should not provide any information about a user's password. A malicious user could use that information to narrow the possibilities of passwords to try. And, as stated above, it provides no value to the user. Plus, if I was ever to see that on an application, my first reaction would be to assume they are not hashing my password and would be a huge red flag. That would never pass a security audit.
-
There should be no reason not to develop a site that has similar content/features as another. Look at Facebook, which was similar to MySpace. But, in this litigious society there's no telling what that other site owner may do. You can sue anyone for anything. So, you might want to do a little research into what that other site does to see if there are any copyrights or patents on things you will want to do.
-
Um, ok, you still haven't provided examples of what the data looks like. How can I provide an example of how you would compare those values when I don't know what format they are in? Also, if you are storing the values in the database as text and not as appropriate field types for a "time" element, then you are doing it wrong.
-
You need to supply some sample data or something. What field types are time_start and time_end: time, datetime, timestamp, ??? And, what is the value of the "time schedule"? Does it have a start and end time? But, you probably have two choices: 1. Do all the logic in PHP. Select your records from the database and then compare the fields when processing each record to determine if the checkbox should be enabled or not. 2. Use the comparison in the query to dynamically return a Boolean value on whether the checkbox should be enabled or not. In either case the final code could likely look something like this: while($row = mysql_fetch_assoc($result)) { $disabled = (CONDITION) ? ' disabled="disabled"' : ''; echo "<input type="checkbox" name=\"{$row['field_name']}\" {$disabled} value=\"{$row['valule']}\" /> {$row['label']}"; } CONDITION will either be the logic that you do in PHP or the dynamic value you calculated in the query
-
That's the point - it is an example. It is up to you to build it however you want it to look. Or, if you want to get really fancy you can use the JQuery UI: http://jqueryui.com/dialog/ But, that also requires YOU to actually do some work and you would have to know something about coding in JavaScript to get it working appropriately. If you are just wanting someone to code this for you, then I can move your post to the freelance forum.
-
Yes. Just order the results by the relevant column and use LIMIT to get the first record.
-
[This isn't a PHP issue, moving post to the JavaScript forum] It is most likely just a DIV. Inside that div there is the X element with an onclick trigger attached to it which calls a javascript function to change the display property of the div so it doesn't display. Here is a very ROUGH example script <html> <head> <script type="text/javascript"> function closeDiv(divID) { document.getElementById(divID).style.display = 'none'; } </script> </head> <body> This is some random content on the page <br><br> <div id="dialog" style="width:200px; border: 1px solid black;padding: 3px;"> <div style="text-align:right;" onclick="closeDiv('dialog');">X</div> <div style="padding:10px;">Click the 'X;' to close this dialog<br><br></div> </div> This is some other random content on the page </body> </html>
-
Um, yeah, I already stated that. Did you not read what I posted? Let me break it down for you. 1) What the OP posted is NOT a syntax error. A syntax error is something that prevents the PHP engine from parsing the code. A typo in a field name for a query does not do that and, thus, is not a syntax error. 2) As I JUST stated, Ballam pointed out the apparent typo of that field name back on reply #5. So, I'm trying to understand, what your response added to this conversation since it provided no new information and was factually incorrect.
-
Well, here's my two cents. You would never rely upon JavaScript to do calculations of price because a user can override those calculations. But, it's fine to have something on the client side to provide the user a quick feedback on the price. But, you absolutely should have PHP code that will be used to determine the actual price to be used. So, it makes sense to me that you would have the same logic in PHP. However, your function is still relying upon other POST values to determine the price - which is a bad process and could result in users 'hacking' the data to reduce the price. The only information the form should need to pass is the id of the items in the cart and the quantities. You would then get the price, rates, etc. from server-side data/code. But, the real question is what are you going to do with the price when the PHP page receives it. Do you want to display a page with the order information and a confirmation of the final price? If so, just pass the relevant values to your function and return the calculated price and output that price wherever you want on the page. There is no need to reference element by their ID, since your PHP code will be generating those elements and you can simply include the value as part of that output. I'm also seeing some oddities in your code. For example, you are multiplying all the submitted values by 1? You should be using floatval() instead. Plus, you are using the same POST vaule for multiple variables - that seems to be a waste.
-
OK, I think what you are saying is that you want the user to keep submitting records and have the newly created record and the previously submitted records display. Well, that would assume that you need to SAVE the data. This is typically done with a database. Trying to teach you how to set up a database and do all the CRUD operations (create, retrieve, update, & delete) cannot be done efficiently within a forum post. There are plenty of tutorials out there to get you started. But, as a general explanation, you would take the newly submitted record, run it through whatever validation checks you need to do, then add the necessary record(s) to the DB using an INSERT query. Then you would run a SELECT query to get all the records that have been saved and run a loop to create the output. There are multiple ways to achieve this. As part of the code to add records I would strip the phone input of all non-alphanumeric characters and then validate the length to whatever you deep appropriate. Some would say to strip all non-numeric characters, but I like to give the user the option to use letters. So, you would store the raw, unformatted value in the DB. Here is an example of how the code to validate the phone might look $phone = isset($_POST['phone']) ? trim($_POST['phone']) : ''; $phone = preg_replace("#[^a-z0-9]#i", '', $phone); if(strlen($phone) < 10) { //Error must enter a 10 character phone number } else { //Phone value is OK to save } Then, when you display the phone later you can format the values however you want using string functions. while($row = mysql_fetch_assoc($database_result)) { $displayPhone = '(' . substr($row['phone'], 0, 3) . ') ' . substr($row['phone'], 0, 3) . '-' . substr($row['phone'], 0, 3); } No idea what you are talking about. It sounded as if you want the user to re-use the form over and over to continue adding values. So, changing the color of the form elements based upon the last submission doesn't seem right if the user is going to submit another value.
-
Simple INSERT Script Fails - No Error Message
Psycho replied to spock9458's topic in PHP Coding Help
Fifth: Don't use POST values directly in your query! You should be using PDO or mysqli_ functions, so you can use prepared statements. Sixth: You are running the mysql_ function but then checking for an error using PDO. PDO is not going to report errors if you are not running PDO functions! To see the errors using mysql_ functions, you should use mysql_error(); OK, after putting the code into an editor that color codes the content based upon the type (variables, text, functions, etc.) the issue is clear. There are a couple of variables that are not included properly . . . '".$_POST['contact1']."')", '".$_POST['cont1_title']."')", '".$_POST['cont1_email'].". . . Plus, there are a couple of right parens in the values list. You should only have one at the end of of a record - in this case you have one record, so there should only be one. This should get you going, but doesn't resolve some of the most serious issues (such as using the POST data int he query) <?php //Rob's CompanyDataInsert handler //DB CONNECTION INFO mysql_connect("192.168.2.7", "user", "pwd") or die('Cannot connect to the database because: ' . mysql_error()); mysql_select_db ("nmlta_agents"); //Insert data into 'company' table $sql ="INSERT INTO company (company, comp_uw, comp_street, comp_po, comp_csz, comp_tf, comp_ph, comp_fx, comp_email, comp_web, comp_county, comp_type, contact1, cont1_title, cont1_email, contact2, cont2_title, cont2_email, contact3, cont3_title, cont3_email, contact4, cont4_title, cont4_email, contact5, cont5_title, cont5_email, contact6, cont6_title, cont6_email, contact7, cont7_title, cont7_email, contact8, cont8_title, cont8_email, contact9, cont9_title, cont9_email, contact10, cont10_title, cont10_email, branch1, br1_street, br1_csz, br1_ph, br1_fx, br1_cont1, br1_cont1_title, br1_cont1_email, br1_cont2, br1_cont2_title, br1_cont2_email, br1_cont3, br1_cont3_title, br1_cont3_email, branch2, br2_street, br2_csz, br2_ph, br2_fx, br2_cont1, br2_cont1_title, br2_cont1_email, br2_cont2, br2_cont2_title, br2_cont2_email, br2_cont3, br2_cont3_title, br2_cont3_email, branch3, br3_street, br3_csz, br3_ph, br3_fx, br3_cont1, br3_cont1_title, br3_cont1_email, br3_cont2, br3_cont2_title, br3_cont2_email, br3_cont3, br3_cont3_title, br3_cont3_email, branch4, br4_street, br4_csz, br4_ph, br4_fx, br4_cont1, br4_cont1_title, br4_cont1_email, br4_cont2, br4_cont2_title, br4_cont2_email, br4_cont3, br4_cont3_title, br4_cont3_email, branch5, br5_street, br5_csz, br5_ph, br5_fx, br5_cont1, br5_cont1_title, br5_cont1_email, br5_cont2, br5_cont2_title, br5_cont2_email, br5_cont3, br5_cont3_title, br5_cont3_email) VALUES ('{$_POST['company']}', '{$_POST['comp_uw']}', '{$_POST['comp_street']}', '{$_POST['comp_po']}', '{$_POST['comp_csz']}', '{$_POST['comp_tf']}', '{$_POST['comp_ph']}', '{$_POST['comp_fx']}', '{$_POST['comp_email']}', '{$_POST['comp_web']}', '{$_POST['comp_county']}', '{$_POST['comp_type']}', '{$_POST['contact1']}', '{$_POST['cont1_title']}', '{$_POST['cont1_email']}', '{$_POST['contact2']}', '{$_POST['cont2_title']}', '{$_POST['cont2_email']}', '{$_POST['contact3']}', '{$_POST['cont3_title']}', '{$_POST['cont3_email']}', '{$_POST['contact4']}', '{$_POST['cont4_title']}', '{$_POST['cont4_email']}', '{$_POST['contact5']}', '{$_POST['cont5_title']}', '{$_POST['cont5_email']}', '{$_POST['contact6']}', '{$_POST['cont6_title']}', '{$_POST['cont6_email']}', '{$_POST['contact7']}', '{$_POST['cont7_title']}', '{$_POST['cont7_email']}', '{$_POST['contact8']}', '{$_POST['cont8_title']}', '{$_POST['cont8_email']}', '{$_POST['contact9']}', '{$_POST['cont9_title']}', '{$_POST['cont9_email']}', '{$_POST['contact10']}', '{$_POST['cont10_title']}', '{$_POST['cont10_email']}', '{$_POST['branch1']}', '{$_POST['br1_street']}', '{$_POST['br1_csz']}', '{$_POST['br1_ph']}', '{$_POST['br1_fx']}', '{$_POST['br1_cont1']}', '{$_POST['br1_cont1_title']}', '{$_POST['br1_cont1_email']}', '{$_POST['br1_cont2']}', '{$_POST['br1_cont2_title']}', '{$_POST['br1_cont2_email']}', '{$_POST['br1_cont3']}', '{$_POST['br1_cont3_title']}', '{$_POST['br1_cont3_email']}', '{$_POST['branch2']}', '{$_POST['br2_street']}', '{$_POST['br2_csz']}', '{$_POST['br2_ph']}', '{$_POST['br2_fx']}', '{$_POST['br2_cont1']}', '{$_POST['br2_cont1_title']}', '{$_POST['br2_cont1_email']}', '{$_POST['br2_cont2']}', '{$_POST['br2_cont2_title']}', '{$_POST['br2_cont2_email']}', '{$_POST['br2_cont3']}', '{$_POST['br2_cont3_title']}', '{$_POST['br2_cont3_email']}', '{$_POST['branch3']}', '{$_POST['br3_street']}', '{$_POST['br3_csz']}', '{$_POST['br3_ph']}', '{$_POST['br3_fx']}', '{$_POST['br3_cont1']}', '{$_POST['br3_cont1_title']}', '{$_POST['br3_cont1_email']}', '{$_POST['br3_cont2']}', '{$_POST['br3_cont2_title']}', '{$_POST['br3_cont2_email']}', '{$_POST['br3_cont3']}', '{$_POST['br3_cont3_title']}', '{$_POST['br3_cont3_email']}', '{$_POST['branch4']}', '{$_POST['br4_street']}', '{$_POST['br4_csz']}', '{$_POST['br4_ph']}', '{$_POST['br4_fx']}', '{$_POST['br4_cont1']}', '{$_POST['br4_cont1_title']}', '{$_POST['br4_cont1_email']}', '{$_POST['br4_cont2']}', '{$_POST['br4_cont2_title']}', '{$_POST['br4_cont2_email']}', '{$_POST['br4_cont3']}', '{$_POST['br4_cont3_title']}', '{$_POST['br4_cont3_email']}', '{$_POST['branch5']}', '{$_POST['br5_street']}', '{$_POST['br5_csz']}', '{$_POST['br5_ph']}', '{$_POST['br5_fx']}', '{$_POST['br5_cont1']}', '{$_POST['br5_cont1_title']}', '{$_POST['br5_cont1_email']}', '{$_POST['br5_cont2']}', '{$_POST['br5_cont2_title']}', '{$_POST['br5_cont2_email']}', '{$_POST['br5_cont3']}', '{$_POST['br5_cont3_title']}', '{$_POST['br5_cont3_email']}')"; $insert = mysql_query($sql); if(!$insert) { echo mysql_error(); } else { echo "Database Updated Successfully!"; } ?> See how much easier that query is to read when there are line breaks and some formatting? -
Simple INSERT Script Fails - No Error Message
Psycho replied to spock9458's topic in PHP Coding Help
First off, don't use short tags, i.e. <? Second, I get a parse error on line 14 - which is where you define the query. Why you don't put line breaks in your query to make it readable and provide an error message that would give you a better change of finding the problem is beyond me. Third, your database schema is poorly built. You have multiple "records" of contacts associate with the company. These should be stored in a separate table. Same goes for the "branches" - they belong in a separate table. EDIT: Fourth, the id field should be an auto-increment field - so there's no need to include it in the query at all. In fact, you are specifying a value of an empty string - which is NOT the same as not specifying an ID so the database can assign one. My guess is that the field is an INT type and the insert is not happening because you are setting a value of an empty string for that field - which is not an INT -
I would not rely upon input submit buttons to determine the page. I'm pretty sure that when a users presses their enter key to submit a form (as opposed to clicking the submit button) that the input button fields/values are not passed in the POST/GET data - or at least I don't believe it behaves consistently across browsers. Instead, include a hidden field in the forms to specify whether it is for1 or form2. <input type="hidden" name="form" name="form1" /> But, do yourself a favor and give them descriptive names! Then on the processing page, use that value (only where needed) to determine which processing to run when it needs to be different. In other words, if you have 9 fields on both forms that need to be processed the same and one for has a 10th field, don't copy and past all of the logic for the 9 fields and put them into if/else blocks. Instead do something like this $string_exp = '/^[5]+$/'; if(!preg_match($string_exp,$spamcheck)) { $error_message .= 'The Number you have entered does not appeart to be valid.<br />'; } if($form = 'form2' && strlen($message) < 2) { $error_message .= 'The Message you entered do not appear to be valid.<br />'; }
-
Also, for what it's worth, it doesn't hurt to have logic to handle situations where no data is returned. In this example may not be an issue, but when you develop more involved queries, getting feedback that there were no results, rather than showing nothing, is a lot more informative. Also, it's a good idea to stick with variable names that are widely recognized for common processes rather than coming up with your own. For one thing, it makes it much easier for others to help you if they don't have to trace back through code to determine what a variable contains. Having said that, I would use $result as the result returned from the query. And, while die() is a quick and effective way to troubleshoot, you should not use it in your final code. You should create logic to gracefully handle error conditions to provide users with a general message about the problem, but also have a way for you to get the specific details about the error. Example: <?php //Create and execute query $sql = "SELECT * FROM CustomerTable"; $result = mysql_query($sql, $connect); $output = ''; if(!$result) { $output = "There was a problem getting the data. If the problem persists, please contact the administrator."; //You can create a process to set a debug mode variable to obtain more descriptive error information //This should be able to be set for a user rather than the entire application. Once quick and dirty method is //to add a debug=1 onto the query string and have the code use that to set a session variable. So, you can turn //on debug mode for yourself in a production environment while not affecting other users if($DEBUG) { //Provide a more descriptive error that only YOU will see $output .= "Query:<br>{$sql}<br>Error:<br>" . mysql_error(); } } elseif(!mysql_num_rows($result)) { $output = "There were no results"; } else { $output = "There were " . mysql_num_rows($result) . " results: <br>\n"; while($row = mysql_fetch_array($retrieval, MYSQL_ASSOC)) { $output .= "FORENAME: {$row['forename']} <br>\n"; $output .= "SURNAME: {$row['surname']} <br>\n"; $output .= "CONTACT NUM: {$row['mobileNum']} <br><br>\n"; } } ?> <html> <head></head> <body> <?php echo $output; ?> </body> </html>
-
Spaces aren't allowed in URLs and may be converted to %20. At least you should hope they are converted, otherwise they won't work. So, you need to convert them back to spaces. You can do this with urldecode().
-
<?php $query = "SELECT typeid, foodtype from intlfoodtype"; $result = mysql_query($query); $options = ''; while($row=mysql_fetch_array($result, MYSQL_ASSOC)) { $options .= "<option value=\"{$row['typeid']}\">{$row['foodtype']}</option>\n"; } echo "<table class=\"ex3\" border=\"0\">\n"; echo " <tr>\n"; echo " <td width=\"100\"></td>\n"; echo " <td>\n"; echo " <select>\n"; echo $options; echo " </select>\n"; echo " </td><td></td>\n"; echo " </tr>\n" echo "</table>\n"; ?>
-
Nope. It is perfectly valid to put variables inside strings that are defined with double quotes. However, you have to be careful. Based upon whether the variable is next to other text or not it can be mis-parsed. Plus, arrays cannot use quotes around the key when used in double quoted strings without specific delimiters (more below). but, your example above is actually incorrect. because the array variable is defined outside a quoted string, you should have quotes around the array key. That will work, but it is the wrong way to do it because the PHP parser will first try to find a constant with the name serviceid. If not found it will then use that value as the key name. I typically define my variables inside double quoted strings but I always enclose them in curly braces, {}, to ensure they are parsed correctly. You should read this page which has all the information you need to understand how to define strings and how they are interpreted: http://php.net/manual/en/language.types.string.php @Maclovin: First off, use [ code ] tags when posing code. Your problem is that your query is failing so the return value of $result is the Boolean false. You shoudl first test your queries in PHPMyAdmin or whatever database management tool you use. Then, you should also add error handling to your code. The way you have it provides no information if there is an error or not or how to fix it. plus, the code you are using has a lot of flaws: 1) The mysql_ functions are deprecated 2) You are using input from the user directly in the query without escaping it - opening yourself up to SQL Injection 3) The two select queries at the top have no purpose - the results aren't used anywhere Anyway, you can change those queries to show whatever error is displayed. This is just a quick example script to point you in the right direction. I would have more code for a real application. //Escape the user submitted value $serviceid = mysql_real_escape_string(trim($_POST['serviceid'])); //Create the query as a string $query = "select * from service where serviceid='{$serviceid}'"; //Execute the query $result= mysql_query($query); //Check if query failed or had no results if(!$result) { echo "Query Failed!<br>Query: {}<br>Error: " . mysql_error(); } elseif(!mysql_num_rows($result)) { echo "The query returned 0 results.<br>{$query}"; } else { $row1 = mysql_fetch_array($result); }
-
What does this query return? SELECT COUNT(*) FROM Customers WHERE CustomerFirstName='Alfreds' AND CustomerLastName='Futterkiste'
-
Check for consecutive values in array and copy
Psycho replied to peteykirk's topic in PHP Coding Help
Here's a slightly improved version. Using RegEx is not a good idea when there are string functions that will suffice. Assuming that all the values will start with two letters then the number, substr() is a better choice. Also, there is no need to put $result[$index][] = $value; in both the if and else conditions. Just put it after the condition and remove the else entirely. My take: $inputAry = array("PK100","PK101","PK102","PK110","PK120","PK121","PK122"); // some data function parseArray($inputAry) { $outputAry = array(); $index = -1; foreach($inputAry as $value) { $num = substr($value, 2); if($num != $currentNum++) { $index++; $currentNum = $num+1; } $outputAry[$index][] = $value; } return $outputAry; } $outputAry = parseArray($inputAry); -
The problem is not the array, but how you are using it. There are many different solutions, but without knowing exactly how the array is used I can't really provide a best case solution. Here are a few options. 1. Change how the value in the array is used based upon the context. For example, you could remove the .tpl from the vaues, then when iterating over the array use the base value as the text to display and then append .tpl for the purpose of what file to load. 2. You could change the array so the key is one value and the value another. For example: $menu = array ( 'Page 1' => 'page1.tpl', 'Page 2' => 'page2.tpl', 'Page 3' => 'page3.tpl' ); Now you can iterate over the array using foreach($menu as $pageName => $pageTemplate) and be able to use both values. 3. If you find you need more than the two linked values you could use a multi-dimensional array. That allows you to store multiple parameters $menu = array ( array('page_name' => 'Page 1', 'title'=>'My first page', 'template' => 'page1.tpl'), array('page_name' => 'Page 2', 'title'=>'My second page', 'template' => 'page2.tpl'), array('page_name' => 'Page 3', 'title'=>'My third page', 'template' => 'page3.tpl') );
-
Don't use GLOBAL - it's bad form. Your function is only returning the result of the isdir() command - so why wrap it in a function? $folders = array_filter(scandir($path), is_dir($path . $item));