Jump to content

mac_gyver

Staff Alumni
  • Posts

    5,536
  • Joined

  • Days Won

    192

Everything posted by mac_gyver

  1. what you are using for the forum's [/nobbc] bbcode tags isn't working, as you can see since your posted code isn't contained within a color-highlighted box -<?php// your php code here...echo "hello world";?> i suspect that your closing bbcode tags isn't [nobbc][/code] ?
  2. just because you saw, found, or bought something on the Internet doesn't mean it's the correct way to write code. there's a ton of crap code to be found on the web that was written by people that never learned the actual reason for doing things and are more concerned with click revenue and things they can get you to buy on their site than the quality of the code they are posting/selling.
  3. the reason your code currently isn't working is because you are not echoing the $result->id in the various form name= .... attributes
  4. actually, your title of this thread is about php, but what you are having a problem doing is programming. these are two different things. php is a programming language. putting programming language statements together so that they accomplishing something, is programming. php is a fairly intuitive programming language. you can generally look at code written in php and deduce/infer what that code is doing, provided that people have used meaningful names for things like variables, form fields, database tables/columns, ... and have not used any short-cuts that magically create or alter values without explicit code or lists of data doing the creating or altering of the values. programming, i.e. actually putting code together that accomplishes a stated goal, requires definition, definition, and more definition. in order to produce program logic that does something, you must first define what you are trying to accomplish/what the goal is, then what inputs you have available (even if the input is just the request to run the code), define what processing you are going to do based on the inputs, and define what result or output you are going to produce from those inputs. programming also requires that you are familiar enough with the syntax, punctuation, and spelling of the language you are using so that you can write meaningful sentences/lines of code using that language. for your current thread - http://forums.phpfreaks.com/topic/297755-improper-form-working-in-php/ what you are trying to accomplish is to provide a form for entering a hostname/domain name, then display the dns records for that hostname/domain name. this is actually two separate goals/processes on one physical page, two logical pages in one. displaying the form for entering the data is one goal/process. displaying the result, after the form has been submitted, is a second goal/process. for the form, what inputs do you have, what processing will there be for each of those inputs, and what result/output will you produce. inputs - along with just the request to run the code, you may (optionally) have validation errors and the previously entered form field data. processing for each of those three inputs - (unconditionally) produce a form, (optionally) format validation errors, and (optionally) get previously entered form field data. output/result - display any validation errors, display the form that you produced, and repopulate the form field(s) with any previously submitted form data. so, for your form, what would applying this process this look like - // form // inputs - request to run the code, (optionally) validation errors, (optionally) previously entered form field data. // processing - (unconditionally) produce form, (optionally) format validation errors, (optionally) get previously entered form field data. // output/result - display any validation errors, display form, repopulate form field(s) with any previous form data. // processing - // get any previously entered form field data. $url_value = isset($_GET['url']) ? $_GET['url'] : ''; // produce form $form_output = "<form action='". htmlspecialchars($_SERVER['PHP_SELF']) . "' method='GET'> Enter URL: <input type='text' name='url' value='$url_value'> Submit: <input type='submit' name='submit'> </form>"; // format any error messages - this example assumes that you have an array of messages in $errors (so that you can handle any number of errors, not just one.) $error_output = ''; if(!empty($errors)){ $error_output .= "The following errors occurred:<br>"; foreach($errors as $error){ $error_output .= "$error<br>"; } } // output/result - if(!empty($error_output)){ echo "<span class='error'> * $error_output</span>"; } echo $form_output; for the form processing code, you would do the same. define what inputs you have, what processing you are going to do on each of those inputs, and what output/result you are going to produce from those inputs.
  5. yes you can. between all the threads you have started for this, you have been given the parts you need, though some of the advice has been off topic because you have shown things like using a select/option menu where nothing is being selected. start off with the basics and define what you are trying to do or produce before writing any code for it. until you can create a form that submits the data you want, there's no point in all of the other markup you have in your code. see this basic example - <form method="post" action="index.php"> <?php // Query for all the products $query_field2 = "SELECT * FROM products ORDER BY name"; $result_field2 = mysqli_query($connection, $query_field2) or die (mysqli_error($connection)); // you were missing the connection link in the mysqli_error() statement while($row_field2 = mysqli_fetch_array($result_field2)){ $id = $row_field2['id']; // i guessed this was id, even though you stated your columns were product_id (and product_name) in one of the threads $name = htmlspecialchars($row_field2['name']); $price1 = htmlspecialchars($row_field2['price1']); // these should be decimal numbers only and be under your control, so no need to apply htmlspecialchars $price2 = htmlspecialchars($row_field2['price2']); $price3 = htmlspecialchars($row_field2['price3']); echo $name; echo "<input type='radio' name='price[$id]' value='$price1' checked>€ $price1"; // pre-check/select the first price if($price2 != null){ echo "<input type='radio' name='price[$id]' value='$price2'>€ $price2"; } if($price3 != null){ echo "<input type='radio' name='price[$id]' value='$price3'>€ $price3"; } echo "<label> Qty "; echo "<input name='quantity[$id]' type='text' size='4'></label><br>\n"; } ?> <button type="submit" name="submit" >Create Order</button> </form>
  6. @purge08, we are not trying to give you a hard time. however, this thing called programming requires that you learn what every character in every line of code means, so that you will know what it does, so you will know if it even belongs in your code or where it belongs in your code to accomplish a goal. to do this, you cannot simply follow along with things you have seen. you must really learn what they mean. until you learn and understand how to write a simple form and php code to process that form, you are not ready to do this for your data. forget about writing out 30 different sets of form/php code. that's just a waste of your time at this point (and as i wrote above, you should be letting the computer do the repetitive work of creating the form fields and processing the form data, rather than you writing out all this code.)
  7. you need to post a topic in the freelancing/job offers forum section to find that out. the php coding help forum section is for programmers and those leaning to program to get help with code they are working on.
  8. if you paid anything for this script, it should have been written with proper error checking and error handling logic in it, because well written code is self troubleshooting. the code should be telling you why it is failing. all the database statements - connection/query/prepare/execute should have error checking/handling logic. until you add some error handling that tests the result of the query and echoes/logs the $conn->error property when the query fails, you will not know why the query is failing. it could be that the database connection isn't selecting the correct/a database, or that the database table or column names don't exist or are incorrect for that database. in addition to what has already been mentioned, there are a number of other shortcomings with this code. it's not protecting against sql injection in most of the queries. it's not hashing the user passwords. the code referencing the signup variables isn't part of any of the singnup form processing code. there's a race-condition in the singup code between the select query and the insert query that would let multiple visitors try to use the same username.
  9. your query is matching no rows. where is $strId coming from and what does echoing/dumping its value show? your code should test if the query found any rows before trying to use the data from the query.
  10. that statement is testing if $_REQUEST is logically true or not, which is effectively testing if it is empty or not. it is always set, but if it is empty/there are no array indexes present, that's a false value. if it is not empty/there are array indexes present, that's a true value. the overall php $sql string statement is within double-quotes and the php variables will be parsed. the single-quotes within the php string are part of the sql syntax, not the php string syntax. @StreamersUnited, the error message you are getting, which is very common and if you had researched the web for it, means that the query failed due to an error. you must ALWAYS test if a query even ran before you can try to use the result from the query. $results will be a false value if the query failed due to an error and you can echo/log the $conn->error property to find out what sort of error occurred. $results will be a result object/true value if the query ran without any errors. lastly, your code should only make one database connection. you need an exit;/die; statement after your header() redirect to prevent the rest of the code from running. and, if you are expecting post data from a form, use $_POST, not $_REQUEST. $_REQUEST combines $_GET, $_POST, $_COOKIE (default order is GPC) data and if you are in a habit of using $_REQUEST you will likely at some point write some code that adds a same name variable between those three different sources and end up with code that doesn't do what you expect. using $_REQUEST just makes more work when writing code, because you and all the programmers working on a larger project need to keep straight all the possible same name $_GET, $_POST, $_COOKIE and $_REQUEST variables.
  11. a UNION query built from the x number of SELECT queries would result in the fewest round-trip communications/queries ran.
  12. no. that looks the same for the empty test. your current logic only reports that at least one of the required fields was left empty. not which one(s). your code also prevents the rest of the validation tests from running if there are any empty fields. there could be problems with the non-empty fields, but the user won't see those errors until they fill in the empty fields. this will result in an unhappy user, because he may have to submit the form extra times, when he could have corrected all the errors at once.
  13. that's not what testing if $_POST is empty or not does. testing if $_POST is empty or not detects if the form was submitted. it's testing if any of the named form fields are present, even if they are empty, in the $_POST array. $_POST will be empty if no form was submitted or a form was submitted and the total size of the form data exceed the max_post_size setting.
  14. this bit of code has several problems - if (empty($_POST) === false) { $required_fields = array('usernamne', 'password', 'password_again', 'first_name', 'active', 'email'); foreach ($_POST as $key => $value) { if (empty($value) && in_array($key, $required_fields) === true) { $errors[] = 'Fields marked with * are mandatory'; break 1; } } 1) you should not blindly loop through the $_POST data. a hacker can submit 100's or 1000's of values. you should instead loop through the definition of your fields and test the corresponding $_POST data. also, unchecked checkboxes and unselected radio button groups are not set in the $_POST data, so your current logic won't detect if a required checkbox/radio button choice hasn't be selected. 2) a zero is considered to be empty(), which is probably why your zero case wasn't working. 3) by breaking out of the loop upon the first empty value, you are only reporting a general error. you should specifically produce an error for each required field that was empty. this will help the visitor know what he left empty (yes, they need to be specifically told), and it will help with programming errors/typo's by reporting exactly which fields the code thinks was left empty. 4) this programming style is a killer - empty($_POST) === false. the empty() function is designed to return a boolean value that you directly use in your program logic. by adding another comparison operator to it, you are building up a forest of unnecessary code.
  15. the error means an array element is being referenced that doesn't exist. adding isset() to test if something exists before trying to referenced it is only valid if what is being referenced is optional, i.e. it may or may not be present. this would be the case when testing if a page got requested and a form may or may not have been submitted, a form element may or may not exist (checkboxes, radio buttons), a get parameter in the url may or may not be present, or code didn't find any matching row in a database table and the code didn't take the added step of preventing any follow on code that's dependent on that data from running. for data that the code requires to be present, you would not use isset() to prevent the error. you would find out why the expected data isn't present by tracing back in the code to find out where it is being produced at and find out why it isn't being produced. adding isset() in this case would just be hiding problems. for the missing $product['key'], i would expect that the key element is required for this code to work at all. if some of the other $product[...] elements are present, but not the ['key'], you would need to find out why. one possible cause would be a difference in the index name from where the value is being produced and where it is being used. it might be ['id'] or ['product_id'] rather than ['key']. for the missing $product['recurring'], that one sounds like it could be optional. but it depends on where and how it is being stored/produced to determine if using isset() to test if it exists before referencing it would be appropriate. if the line of code for it in what you posted is the way the original code is, it would be a safe guess that it is missing an isset() around the $product['recurring'] in that line. sadly, problems like this in code you find posted on the web are due to the person writing the code not really knowing the reason behind what they are doing and are developing code with php's error reporting not fully turned on and/or not fully testing the code they have posted.
  16. here's a thread/post showing how you would run an UPDATE query to toggle a value and retrieve a value from that row at the same time - http://forums.phpfreaks.com/topic/296803-get-id-after-update/?hl=%2Blast_insert_id&do=findComment&comment=1513845 the user_id in that thread would instead be your column holding the previously randomized numbers.
  17. your stated method actually has a race condition where multiple concurrent instances could produce and try to insert the same number. you can do this using one INSERT query, which avoids the race condition. see the following example - http://forums.phpfreaks.com/topic/297377-going-from-sessions-only-to-remember-me/?do=findComment&comment=1517023 by only running one query, your code should be at least twice as efficient. i would think that 45 seconds for 1000 requests probably means that your current code has some additional inefficients and/or your database table doesn't have an index (the method in the code i linked to above requires an index.) does what you are using these values for require a random number or could it be an incrementing number? also, how many total numbers will there be, because another way of doing this would be to generate all the possible numbers, randomize them, then store them in a database table. you can just query for the next unused value. to avoid the race condition, you would actually run an UPDATE query to toggle a 'used' bit in an available row. then, if the UPDATE query was successful, you know that the row you just toggled contains your random number. you can get the number from that row by using the msyql LAST_INSERT_ID(expr) function in the query, then retrieve the number by fetching the last inserted id value.
  18. this is the first prepared query in your code - $stmt = $DB_con->prepare(" ... "); this is the second one, where the error is at - $stmt->$DB_con->prepare($query); look at what is different in those, right after the $stmt variable.
  19. it will be easiest if you add rows. if you weren't using a <table> for your layout, this would be simple. you could just define a template section around the first instance of a row, then dynamically append new instances - <script type="text/javascript"> function dyn_add() { // create an empty div element var div1 = document.createElement('div'); // get the template html and put it into the empty div div1.innerHTML = document.getElementById('template').innerHTML; // append the new div to the target area on the page document.getElementById('add_here').appendChild(div1); } </script> <p> <a href="javascript:dyn_add()">Dynamically Add another of whatever is defined in the template div</a> </p> <form method='post' action='formaction.php'> <!-- Template. This first instance of "whatever" will be appended in the add_here div --> <div id="template"> <?php // produce the first row/instance $fields = array('Quantity 1','Pressure','Vacuum','Quantity 4','Temp','Solids'); $temp = ""; foreach($fields as $element){ $temp .= "<input name='{$element}[]' size='3' type='text'>"; } $temp .= "<br>"; echo $temp; // output the first instance in the template div ?> </div> <!-- if you want to have more initial fields shown, output them here, after the template --> <?php echo str_repeat($temp, 3); ?> <!-- container to hold the dynamically added instances of "whatever" --> <div id="add_here"> </div> <input type='submit'> </form> however, this won't work with a <table> as you cannot put a <div> directly into a table. afaik, with a table, to append rows or columns, you would need to specifically build rows and cells where you want them and then append whatever html you want into each new cell. if you search the web for "javascript dynamically add table rows" you will find a ton examples. you could of course do this server-side using php to add any requested rows/columns to the html.
  20. if you stored procedure worked when you were using mysql_ statements, the problem isn't in the stored procedure. i'm betting that nothing is displayed when you request your page. there's a space in the $my _p variable in the bind_param(....) statement that is causing a fatal php syntax error. when developing and debugging code, you must have php's error_reporting set to E_ALL and display_errors set to ON in your php.ini on your development system so that all php detected errors will be reported and displayed (parse errors in your main file won't be reported if you try to set the error_reporting/display_errors settings in your code because your main code never runs to cause the settings to take effect.)
  21. to use a prepared query inside of a loop, the only things that go inside of the loop are the statements that populate the bound variables with data and the ->execute() statement. the code building the sql query, the ->prepare(), and the ->bind_param() should only exist once and come before the start of your loops. where is $l_planid defined at? should it instead be $l_pla? what symptom or error are you getting that leads you to believe that it is failing?
  22. the error is self explanatory, it means that you are referencing an array index that doesn't exist. since the only reference to an array index by that name is in this line - $er_id = $row1["er_id"];, does your database table have a column named er_id? also, why do you have two database tables, one for employees and one for employers, and double the amount of code? that's not how to program. the visitors that are logging in shouldn't need to pick which type they are on the form page. all they should need to do is enter their email and password. it should be up to your single database table and your code to know what type they are. by having two tables, you are have to write, test, and debug double the amount of code for the form and for the form processing. lastly, the mysql_ database functions are obsolete and will be removed from php in the near future. you should be learning to use either the PDO or mysqli_ database functions so that what you are learning isn't already out of date.
  23. that may be, but is the php language engine 32 or 64 bit? that statement concerns the form fields, not the <form tag. that sounds like it's one of the third-party flash based file unloaders. care to share which one in case someone has some specific knowledge about it and your symptom that could lead to a solution?
  24. a post method form that has no name='....' attributes in the fields or has only empty name='' attributes in the fields will also send a CONTENT_LENGTH of zero. is there any chance that you are dynamically producing the form, either via php or javascript, or are using javascript to submit the form?
  25. the methods we have shown in this thread are for the type of query you posted at the start of this thread, that is testing if there is a user row matching the id and to fetch that row if there is, i.e. a query that will match at most one row. if you are running a query that can return any number of matching rows, you need to write the code differently. you would need to test the number of rows, then loop over the result set if there are rows. as an alternative, since you should be separating your database dependent code from any html markup, would be to simply fetch all the rows, even if there are none, into a php array. then use that array any place you need to access the data. you can find how many rows the query matched by using count() on the array. you can loop over the array using a foreach loop to process the rows.
×
×
  • 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.