DavidAM
Staff Alumni-
Posts
1,984 -
Joined
-
Days Won
10
Everything posted by DavidAM
-
Don't know how I missed that. I scanned through the code looking specifically for that and did not see it. I guess its time to get my eyes checked.
-
If either field (name or location) is blank when submitted, that query will return everything in the database.
-
Can you be a little more specific? Being submitted ... WHERE? In the $_POST array? In the Database ? on another page that shows the data? Where is it wrong? If you call print_r($_POST); Are the values associated with the correct key? If you call print_r($row); (after the SELECT) are the values associated with the correct key? Have you checked the database directly (phpmyadmin or command line mysql)?
-
scandir, recursive call prints an extra closing tag for xml install file
DavidAM replied to fero57's topic in Applications
The last line shown here is executed just before exiting the (recursed) function and is ALSO executed as soon as the recursed call returns. There should probably be an ELSE just before it since you close that tag inside the IF- 5 replies
-
- scandir
- recursive call
-
(and 1 more)
Tagged with:
-
Javascript not firing correctly when called in PHP
DavidAM replied to farmallnerd's topic in Javascript Help
You cannot execute JavaScript in a PHP script; and you cannot execute PHP in a JavaScript script. PHP is a server-side scripting language. It executes to completion and sends something to the browser. JavaScript is a client-side scripting language. It executes in the browser -- but only AFTER PHP is completely done. -
require and include (the their "_once" cousins) bring the code from the specified file into the current file as if you had typed that code right there. If that code outputs anything then the output appears right there. PHP is run on the server, and all of its output is sent to the browser. The browser then renders the (usually) HTML to show the page to the user. If you want that script to be run when the link is clicked, then the link needs to point to a URL that will execute that script.
-
my code for save mobile number doesn't work correctly
DavidAM replied to nekooee's topic in PHP Coding Help
You open the "D" file with "a+", which opens it for Append and puts the pointer at the end of the file. I would think, then, that feof($file_D) will be true, the loop will not run, and the $exists_numbers_D will be empty. In my opinion, instead of that loop, try using $exists_numbers_D = file($filename_D); before you open the file for appending. That one statement will do exactly what you are trying to do with the loop, so take that loop out. You might also try using print_r or var_dump to see what is in that array to see if you actually read anything. -
This shows the importance of asking the entire question the first time. This is not the way I would handle it, but I'm not going to rewrite it at this point. The if(empty($headings)) statement will only be true on the first pass through the loop. So everything you want to do with the headings can be done there. while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { // Add headings to combobox if (empty($headings)) { $headings = $data; # MAD - Added for ($c = 0; $c < count($headings); $c++) { echo "<option value='" .$headings[$c] ."'>" .$headings[$c] ."</option>"; //Only need first row in drop down box. EG the headings } ?></select><?php } // END IF(empty($headings)) $num = count($data);
-
One of the most common issues with sending emails from PHP is the use of the "From:" header. This header must be a valid email address that has authority to send emails from the SMTP server being used. You are assigning the "Name" field from the form, which is apparently the user's real-world name. This is not going to work. I have never used GoDaddy, but I believe you get at least one email address with the hosting service. If godaddy is providing an SMTP server (or sendmail executable), then that address is the one you should use. If you have configured PHP to use a different SMTP server, then you need to use your email address on that server. If you want the user-supplied email address in the message so you can reply to them. You add a "Reply-To:" header with their address. There are some other issues with that script: 1) if ( preg_match( "/[\r\n]/", $name ) || preg_match( "/[\r\n]/", $email ) ) { [... direct user to an error page and quit ...] } is not valid code and should be causing an error. It may even prevent the script from executing at all. 2) You never did assign anything to the $message variable so the email is going to be empty 3) You never output anything and you don't redirect, so even if the script works, the user will be left with a blank white page. At the very least do something like this: if (mail( ... )) { echo 'Email Sent.<BR>' . $message; } else { echo 'Email Failed'; }4) Turn on error reporting in development so you can see any errors that occur. Put these lines at the beginning of the script: error_reporting(-1); ini_set('display.errors', 1);
-
Blindly creating variables from the $_POST (or $_GET) array is NOT a safe practice. I could add a field to the form with a name of "IsAdmin" and a value of "1" (or true); and post the form. If your script uses a variable named $IsAdmin the POST value could overwrite it giving me admin priviledges. This is the reason that "register globals" has been removed from PHP. It is better and safer to assign specifically those POST values that you are expecting. @OP That line of code is referencing $practice_type which you do not assign until somewhere around line 83. The example you copied it from probably depended on register_globals being on. It isn't going to work that way anyway, since you don't assign TO email address to practice_type until the switch statement around line 119. Move the offending line below that switch (or remove it and have the switch assign the address directly to $email_to).
-
How do I add to an associate array to a associate array?
DavidAM replied to Stuart_Westgate's topic in PHP Coding Help
Barand's answer is probably the best. Put the data in a single array when you collect it. If they are coming from different places, and that is not possible, have a look at array_merge -
You can ADD validation on the client, but you should NOT move the validation there. The client can disable JavaScript and post anything they want, so you absolutely need to do validation on the server. Doing validation on the client is only for the user's benefit (immediate feedback and fewer round-trips to the server, so faster). @OP Are you having problems with the posted code? Tell us what it does that it should not do; or what is does not do that it should. Turn on error reporting and remove the error suppressor (@) from your code. The "@" only hides an error, it does not magically fix it. You need to see the error message so you can fix your code. // Turn on error reporting - Near the top of the script error_reporting(-1); ini_set('display.errors', 1);
-
You basically need to write the same calculations in PHP. Have the form post to a script (the same one or a different one), that collects the data from $_POST and does the calculation.
-
Using a hidden field for critical data is not a good idea. Hidden fields can be modified by the user, so it would be a simple matter to post the form with a "TotalPrice" of $0.01. So I can get it free. You can do calculations with JavaScript for the user's review, but when the form is posted; you must do the Price calculations server-side (PHP) to make sure they are correct.
-
get content from form and compare for duplicates in txt files
DavidAM replied to aceman19's topic in PHP Coding Help
This is your code with a little indenting to make it easier to read/understand: <?php $text = $_GET['email']; $text1 = $_GET['sex']; $text2 = $_GET['age']; $dates = date("d.m.Y H"); $savestring = "\n" . $dates . ";" . $text . ";" . $text1 . ";" . $text2 ; $fp = fopen("myFile.txt", 'r'); while (!feof($fp)) { $line = fgets($fp); if(strcmp($savestring,$line)==0) { echo "<h1>Duplicate entery.</h1>"; fclose($fp); } else { fclose($fp); $fp1 = fopen("myFile1.txt", 'a'); fwrite($fp1, $savestring); echo "<h1>Success. U have been added to list.</h1>"; } } There are several problems with the logic here.1) Lines in a text file usually END with a newline (not START with it). fgets includes the newline at the END of the line it reads so your $savestring is never going to match anything read from the file. 2) Line 16 is closing the input file if you find a match, but you don't exit the loop, so the loop is going to try to process the next line in the file, and you get that message in the error log 3) Line 17 - This ELSE branch will be executed every time the $savestring does not match the line read from the file (and it will never match). So after reading ONLY one line, and failing to match it to the user's input, you are adding the user's input to the file. 4) Line 18 - You close the input file but you don't exit the loop, so again, the loop will try to read another line from the file and produce that error message in the log file 5) You have done nothing to sanitize or validate the user's data. If someone includes a semi-colon in any of the input fields, that line in your file will be unusable by whatever process you have using it. While it is true that none of those fields should need a semi-colon, you can not depend on user-entered data ever Here is some psuedo-code of how I would handle that loop: open file for reading Set FoundOne to false while (not at end-of-file) read a Line if Line == savestring FoundOne = true break out of the loop end if end while close file if FoundOne write error message else add savestring to file end if -
No. When you use double quotes, the variable name is replaced by the string representation of the variable's value. Math is not performed, functions are not called. It is simply a way to put a variable value into a string. <?php $t = time(); $t = "$t+3600"; var_dump($t); date('r', $t); ?> # Results string(15) "1369417662+3600" Notice: A non well formed numeric value encountered in /home/mad/logs/- on line 5 - PHP 5.2.6
-
You are making this more difficult than it needs to be (I should know, I do that all the time). You can't say "File not found" inside the while loop just because the current file does not match. You have to make a note when you DO find a file and then echo the "Not Found" message AFTER the loop (if you didn't find one). Try this rewrite: $dir = 'dir'; $exclude = array('.','..','.htaccess'); $q = (isset($_GET['q']))? strtolower($_GET['q']) : ''; $res = opendir($dir); if (strlen($q) < 3) { echo "Search must be longer than 3 characters.<br><br>"; } elseif (($q == "mp3") || ($q == "wav")) { echo "Invalid Search Criteria<BR>"; } else { $foundOne = false; while(false!== ($file = readdir($res))) { if(strpos(strtolower($file),$q)!== false &&!in_array($file,$exclude)) { $info = pathinfo($file); $nicefile = $info['filename']; if (($info["extension"] == "mp3") || ($info["extension"] == "wav")) { echo "<a href='http://domainname/filename.php?name=$file'>$nicefile</a>"; echo "<br>"; $foundOne = true; } } } if (!$foundOne) echo "No files found<BR>"; } closedir($res); I don't know if it was the forum that messed up your code, or you just didn't use indenting. If it was you, learn to use it, it makes it clearer as to what is part of an IF or ELSE or LOOP or whatever.
-
Calling results from first while loop in to second?
DavidAM replied to Kristoff1875's topic in MySQL Help
Since you are only going to find ONE row in the second snippet, there is no need for a loop there. So, you can do the Area logic after retrieving that record: $sql1 = "SELECT * FROM Users WHERE UserID = $UserID"; $result1 = mysql_query($sql1); $row1 = mysql_fetch_array($result1); // Add area LOGIC HERE I usually have a function for this purpose. Something like: function getAreaOptions($psSelected = '') { $out = array(); $sql = "SELECT DISTINCT Area FROM Users ORDER BY Area ASC"; $result = mysql_query($sql); while($row = mysql_fetch_array($result)) { $out[] = sprintf('<OPTION %s value="%s">%s</OPTION>', ($row['Area'] == $psSelected ? 'SELECTED="selected"' : ''), $row['Area'], $row['Area']); } return implode(PHP_EOL, $out); }Then call it from your user code: $sql1 = "SELECT * FROM Users WHERE UserID = $UserID"; $result1 = mysql_query($sql1); $row1 = mysql_fetch_array($result1); echo '<select name="Area" id="Area" class="dropdown" style="width:260px;"> <option> Any </option>'; echo getAreaOptions($row1['Area']); echo '</select>'; -
Not really. Is this a WordPress site, or is some other framework involved here? Did you change the form field (the checkbox) to include value="1"? Did you change the IF test to use empty instead of isset? Is the FORM tag using POST or GET as the method? For debugging purposes, add the following lines immediately after the $data = array( ...); line that you showed above: printf('data: <PRE>%s</PRE>', htmlspecialchars(print_r($data, true))); printf('POST: <PRE>%s</PRE>', htmlspecialchars(print_r($_POST, true))); printf('GET: <PRE>%s</PRE>', htmlspecialchars(print_r($_GET, true))); die();Then submit the form with the checkbox checked. Copy and Paste the results (in code tags) here. Also, post the code in that script from the beginning up to and including the $data = array(... statement.
-
You absolutely must check the values when the form is submitted. It is a trivial matter for a user (i.e. bad guy) to send any data they want regardless of the values (OPTIONS) in your SELECT list.
-
Displaying number of records in continuous manner in php paging
DavidAM replied to phpcodingfreak's topic in PHP Coding Help
Pretty! ... I give it a 6 - it's indented well, but there aren't any comments and it's not symmetrical. If you want help, you will have to tell us what the problem is or ask a question or something. -
There is no need to select and loop through all the data just to update one row. Plus, that code is updating each row with the same data, which, I'm guessing, you only want on the one row that finished. Along with Barand's suggestion, that code should be something like: if($_POST['submit']=="Bake Finished") { $time = strftime('%I:%M %p'); $hours = strftime('%H'); $min = strftime('%M'); if(isset($_POST[$recno])!=NULL) { $recno=$result['RecNo']; mysql_query("UPDATE tbl_baking_chamber set TotalTime='$TotalTime', EndTime='$CurrentDate - $time', Status='Finished' where RecNo='".$recno."'"); } } Of course there are still issues:1) I don't see $TotalTime defined in the code you posted; 2) I don't see $CurrentDate defined in that code either; 3) I don't think $CurrentDate - $time is going to produce what you want; 4) In a normalized database, you would store StartTime and EndTime and calculate TotalTime when you SELECT the data. 5) I don't recommend building the query and executing it in a single line of code. It really should be: $sql = 'SELECT ...'; $result = mysql_query($sql); if (! $result) echo 'ERROR: $sql - ' . mysql_error();6) The mysql extension is deprecated, you should use mysqli for new development. @Q695 You would normally use a JOIN for that. The exception to the rule is extremely rare.
-
As requinix said, your original code should have already been displaying the headers since they are the first row of data you read. I just added a line to "capture" the headings into an array variable. That's what you said you wanted to do. Why are you echoing the headings inside the loop for every row? What are you trying to accomplish? To answer your question exactly, $headings is an array, just like $data. So you can echo the headings the same way you echo the data: for ($c = 0; $c < count($headings); $c++) echo $headings[$c] . " ";. But it makes no sense to do that in the read loop, since you are already printing them in the for loop on the first read.
-
The code in my previous response included two lines I added to capture the headings.