Psycho
Moderators-
Posts
12,157 -
Joined
-
Last visited
-
Days Won
129
Everything posted by Psycho
-
Your code makes no sense. You prepare the query into the object $stmt, but then run the query on the unprepared string $sql. There is no need to prepare it since there are not variables in the query. So, lines 2 and 3 are unnecessary. As to your original question, how do you know there are 8 records in the result set? Have you verified the email address to be used? Echo some text to the page for debugging: $sql = "SELECT email, supplier_id FROM tbl_suppliers"; foreach($db->query($sql) as $row) { $email = $row['email']; $supp_id = $row['supplier_id']; echo "Email: {$email}, Supplier. ID: {$supp_id}<br>\n"; $emailCSV->setEmailMessage("some generic text message"); $emailCSV->sendEmail("me@me.com", $email, "Quote Request"); }
-
You should be using 1 for TRUE and 0 for FALSE Typically, people have issues with checkboxes because they are only sent in the POST data if they are checked. Not knowing your specific issues, I can't provide any suggestions. PDO and MySQL are not mutually exclusive. MySQL is a type of database. PDO is a database extension that you use to interact with a database. You may have meant mysqli_ which is the MySQL specific extension for working with MySQL databases. I would suggest the PDO over the mysqli_ extension. PDO can be used with many different types of databases. So, if you change from MySQL to SQLLite, PostgreSQL, or something else you may not have to change any code. With mysqli_, it only works with the MySQL database. Plus, PDO is easier to understand in my opinion. It's not a bad idea. It just depends on how you need it to work. Without understanding the requirements and the context of the record to be edited, it is impossible to provide a knowledgeable suggestion.
-
1. No, you should NOT add validation logic "in the form". You can add Client-Side validation logic via JavaScript, but it should not be written "in the form". It should be written as separate JS functions that are called when there is a change event detected for the fields. 2. Client-Side validation logic is never, ever a substitute for Server-Side validation logic. Client-Side logic should only be added to compliment the logic that would be in place on the server. Client-Side logic can always be circumvented and can never be counted on to prevent erroneous or malicious input.
-
Based on a quick Google search of that error, the problem is likely that the AJAX call is getting back an error page. You displayed the JSON code that you state is being generated, but are you sure that is what is getting returned? If you get back an error page it may be formatted in HTML. E.g. rough example: "<html><body>404 Not Found</body</html>", What do you get when you request the PHP page directly, i.e. not through AJAX? What is displayed on the page?
- 12 replies
-
That is the input form. That is not where you would add any validation logic. You would add the validation logic to the page that processes the form input. Where is that code and what have you tried so far?
-
Help me resolve a wierd problem in a FORM with radio buttons
Psycho replied to new2you's topic in PHP Coding Help
Then as we've already described, there must be other code that is doing the insert or the code is being run twice. For example, it could be running once to insert the record, but then does a redirect back to itself and runs the false condition. There is definitely something going on here that is not in the code you have provided. What is in that other function I identified which is being called? fieldsArrayForCreateUpdate() -
Good to know. But, with all due respect, what the hell were you even asking in your original post? I've reread it a couple of times and it still doesn't make any sense as to how it applies to what I now understand you were trying to accomplish.
-
That doesn't sound like anything to do with what you originally asked. Perhaps you need to provide some examples of what you are talking about. But, if your problem is that you need to prevent duplicates with respect to two fields, you can enforce that directly through the DB schema. For example, if you have fields for First Name and Last Name you can set the schema up so that the combination of First name and Last Name are unique. Here is an example: First Name | Last Name ------------------------- David Smith David Andeerson Richard Smith If you set the combination of First Name + Last Name must be unique, all those values would be allowed. But, you could not add another record such as "David Smith"
-
Help me resolve a wierd problem in a FORM with radio buttons
Psycho replied to new2you's topic in PHP Coding Help
@new2you, there is nothing in that code that would wipe out the $_POST array, but there is another function call in that function $fields = fieldsArrayForCreateUpdate(); @ginerjm, the code does not have labels within labels. There is only one label. His code is overly complex and utilizes the ternary operator where, in my opinion, makes it more difficult to read. Here is a rewrite that woud do the same thing that is more logical //Since you want the format for the input fields to be the same, //You can create a function to create them function standardInputField($fieldLabel, $fieldName, $fieldValue, $errorsAry) { //Determine label ID and input ID $fieldId = ($fieldName == "new_store_name" || $fieldName == "new_item_name") { $labelID = 'label-new-store-or-item'; $inputID = 'new_store_name'; } else { $labelID = ''; $inputID = 'new_item_name'; } //Determine error message if(isset($errorsAry) && in_array($fieldName, $errorsAry)) { $fields = fieldsArrayForCreateUpdate(); $error .= " <span class='error-message'>{$fields[$fieldName]['error']}</span>\n"; } else { $error = "": } $fieldHTML = "<div class='control-group'>\n"; $fieldHTML .= " <label id='{$labelID}' class='control-label'>{$fieldLabel}</label>\n"; $fieldHTML .= " <div class='controls'>\n"; $fieldHTML .= " <input class='other-store-or-item' id='{$inputID}' type='text' name='{$fieldName}' value='{$fieldValue}' />\n" $fieldHTML .= $error; $fieldHTML .= " </div>\n"; $fieldHTML .= "</div>\n"; return $fieldHTML; } -
You first need to use correct terminology. You keep switching back and forth between "object" and "array". Yu are dealing with an object. I know this because the first bit you displayed looks to be from a print_r Then when you tried to use it, incorrectly, in num_rows(), it also told you it was an object Objects have properties and methods. You can see all the properties of the object in the print_r() that you did. Protected properties of a method cannot be accessed directly. They are built this way to prevent manipulation of data that would otherwise create invalid scenarios. Your object apparently represents a DB query result. If you were able to change 'num_rows' then it would not match the results. If there are protected or private properties that are menat to be accessed there should be methods for the object to return that data. I don't know squat about moodle. But, you should be able to find by checking the documentation for it. But, there's a good chance they simply used the same name to retrieve that value. Try: $mysqli_native_moodle_recordset->numrows(); Or ]$mysqli_native_moodle_recordset->num_rows(); If neither works, go check the documentation or post in a forum to get help with 3rd party frameworks.
-
Not according to PHP object given Where, exactly, are you getting this array/object from?
-
Help me resolve a wierd problem in a FORM with radio buttons
Psycho replied to new2you's topic in PHP Coding Help
OK, I agree with ginerjm. You are providing way too much information. This makes it difficult for us to understand what you are asking and then expecting us to read through a bunch of code to try and decipher decreases your chances of getting a response. You need to learn how to reduce the code (and explanation) to what is pertinent. What I understand is that the condition to check if the super global $_POST is empty is not generating the results you think it should. Specifically, you are getting this on the page: Array() There's something wrong somewhere!!! Array() Then, looking at just the relevant code, you have this print_r($_POST); //##### if ( !empty($_POST)) { // keep track of $_POST(ed) values by storing the entered values to memory variables $store_id = $_POST['store_id']; $item_id = $_POST['item_id']; $qty_pkg = $_POST['qty_pkg']; $pkg_of = $_POST['pkg_of']; // . . . // . . . $sql = "INSERT INTO shoplist (store_id,item_id,qty_pkg,pkg_of,price,flyer_page,limited_time_sale,flyer_date_start,nos_to_purchase,section_id,shopper1_buy_flag,shopper2_buy_flag,purchased_flag,purchase_later_flag,no_flag_set) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; $q = $pdo->prepare($sql); $q->execute(array($store_id,$item_id,$qty_pkg,$pkg_of,$price,$flyer_page,$limited_time_sale,$flyerDateStart,$nos_to_purchase,$section_id,$initialize_flag_N,$initialize_flag_N,$initialize_flag_N,$initialize_flag_N,$initialize_flag_Y)); } else { echo "<br />There's something wrong somewhere!!!<br />"; //##### print_r ($_POST); //##### // . . . The ones I commented with "#####" are the ones producing the output. So, that tells me that $_POST is empty. But, you say that a record is getting created based on the code in the TRUE part of the conditional block. That is impossible based on what you posted. Either this isn't the right code, there is other code that isn't being displayed, or you are misinterpreting the results. To verify, just add some additional debugging logic echo "Count of $_POST var: " . count($_POST) . "<br>\n"; if ( !empty($_POST)) { echo "Starting the code block for the TRUE condition<br>"; // keep track of $_POST(ed) values by storing the entered values to memory variables $store_id = $_POST['store_id']; $item_id = $_POST['item_id']; $qty_pkg = $_POST['qty_pkg']; $pkg_of = $_POST['pkg_of']; // . . . // . . . $sql = "INSERT INTO shoplist (store_id,item_id,qty_pkg,pkg_of,price,flyer_page,limited_time_sale,flyer_date_start,nos_to_purchase,section_id,shopper1_buy_flag,shopper2_buy_flag,purchased_flag,purchase_later_flag,no_flag_set) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; $q = $pdo->prepare($sql); $q->execute(array($store_id,$item_id,$qty_pkg,$pkg_of,$price,$flyer_page,$limited_time_sale,$flyerDateStart,$nos_to_purchase,$section_id,$initialize_flag_N,$initialize_flag_N,$initialize_flag_N,$initialize_flag_N,$initialize_flag_Y)); echo "Executed SQL query: $sql<br>"; } else { echo "Starting the code block for the FALSE condition<br>"; echo "<br />There's something wrong somewhere!!!<br />"; print_r ($_POST); // . . . -
Those values are "protected". That means you cannot access them in that way (http://php.net/manual/en/language.oop5.visibility.php). I don't know why you would be trying to do that anyway. You have an object. You need to understand the methods that the object provides. You're apparently using mysqli, so why not just use the mysqli_ method to get the number of rows? http://us1.php.net/manual/en/mysqli-result.num-rows.php
-
Sounds like a character encoding problem. Take a look at this thread http://forums.phpfreaks.com/topic/290726-character-encoding-problem-got-strange-characters-in-my-database/?hl=%2Butf+%2Bdatabase&do=findComment&comment=1489348
-
Does this Hostgator Technician Know What He's Talking About?
Psycho replied to Fluoresce's topic in MySQL Help
As mac_gyver stated, it is closed automatically when it is done executing all the pages needed for the request. It is not when it encounters a ?>. You can reuse that request across any of the pages used on that request. Another thought to your original problem. Are you using any AJAX on your pages? If so, there could be multiple requests made to the server while a user is watching a video. -
Not sure I follow you. getTimezoneOffset() returns the difference between the user's timzone and UTC. You must live in a timezone the same as UTC. Are you wanting to translate that into a textual description of the timezone? E.g. PST (Pacific Standard Time)? If so, you would have to create a dictionary of all the possible values. But, it would not be 'accurate' since multiple users can have the same "time" (and UTC offset) but be in different timezones.
-
Is a new hash generated every time a form is submitted?
Psycho replied to raydona's topic in PHP Coding Help
There is no way for us to know. How are $salt, $ip, and $formName defined? If they are the same, then the resulting hash will be the same. -
I have a lot of experience with generating PDFs ia a web interface, but this was with the use of custom PostScript files and a backend processor. This would be well outside what you are looking to do. You'll need to run the output through an additional process to make it a PDF. There is built-in PDF handling in PHP (http://php.net/manual/en/book.pdf.php), but I don't think it is the easiest to work with. Fo rexample, you can't just send the output for PDF, you basically have to tell the code where to put each piece of information. For example, for the first line you would provide code to set the position on the page, set the font to use, the color of the font, and then pass the text to output. Then you would set the position for the 2nd line and send the text for that line. Unless you are going to build your own PDF framework, I don't think it is worth your time. However, there are plenty of other frameworks/classes that are available which would be easier. I don't have on to suggest though. Alternatively, there are also applications out there that allow you to prepare a "page" using HTML and then pass that output to an application that will generate it into a PDF. Most of these (at least the ones that do a good job) cost money. Here are a couple resources you can take a look at to see if any meet your needs: http://www.tcpdf.org/ http://www.fpdf.org/ http://mpdf.bpm1.com/
-
If you really want it exactly as it is returned with the exception of the part in parenthesis, then just use a string manipulation to only get the first part. You need to convert it to a string first. var visitors_time = new Date().toString(); var displayDate = visitors_time.substr(0, visitors_time.indexOf('(')-1);
-
MySQL error when single quote or double quotes are used
Psycho replied to barkly's topic in PHP Coding Help
Set up the table in the database so that the column is UNIQUE. The database will then actively prevent a duplicate from being created. But, it will generate an error, so you need to check for any error and then for the specific error of a duplicate constraint. You're mixing mysql_ and mysqli_ in your code. You should either use mysqli_ or PDO. Here is a quick example of how the code might look with PDO: // Assumes PDO DB connection was made with $db // being the instance of that connection //Create the prepared INSERT statement $sql = "INSERT INTO table (message) VALUES (:message)"; $stmt = $db->prepare($sql); //Associate data with an array for the prepared statement $data = array('message' => $_POST[message]); //Try to execute the INSERT statement try { $stmt->execute($data); //Set default message assuming this completed $error = "This message does not exist. Insert it!!!"; } catch (PDOException $stmt_error) { //check the error code if ($stmt_error->errorInfo[1] == 1062) { //Error was due to a duplicate exception $error = "Message Exists."; } else { //Error was due to some other issue $error = "An error occured."; } } -
Then, didn't you just answer your own question? Although, having a keyword for "rugby" and then another for "rugby league" seems odd. I think you're making it too specific. If "rugby" is a keyword on its own, why do you need "rugby league"?
-
Mac_gyver has provided a solid solution that you should consider. But, there is a simple 'hack' to get you a workable solution in the short run. If an error is encountered, first check the modified date of the log file. If it is more than N minutes since it was last modified, send the email. Otherwise do not send the email But, in either case you would still log the error. The result is that once you start getting errors you will get an email. But, you would not get another email from any failures unless there were no errors for the given amount of time.