DavidAM
Staff Alumni-
Posts
1,984 -
Joined
-
Days Won
10
Everything posted by DavidAM
-
I was just thinking that if it was boolean, the update would fail trying to set it to a string. Again, grasping at straws, is there some kind of protection issue with that table? When you ran the code in phpMyAdmin were you using the same login that your code is using? Is there an UPDATE trigger on that table? or some other constraint? Are you sure you are not falling into some other code that is setting it back to false? I guess that would be kind of hard since you have an exit() right after it. I really do not see any reason that that code would not execute as expected. Is this your actual code? or did you leave out stuff that you think is not significant? (I'm just asking)
-
Code is complete...but doesn't run..get 500 error
DavidAM replied to Luke Martin's topic in PHP Coding Help
You cannot call a javascript function from PHP. There are a couple options here: 1) redirect to one page for success and a different page for failure. You can provide a link on that page to take the user where you actually want them to go. You can also put an HTML META refresh tag on that page to cause the page to load their ultimate destination after a few seconds. However, include the link because I have heard that the META refresh is not reliable. 2) Echo the success or failure to the current page (get rid of the headers) and include a link to their ultimate destination. You can also include the META refresh on this page. -
$updateQuery2 = "UPDATE preview SET draft='false' WHERE uid='$_GET[uid]'"; There's no smiley for "graspping at straws" ... What is the datatype for "draft"? Is it actually a char (or varchar) or is it a BOOLEAN. Boolean is a INT(1) and you would need to set it to 0 (zero) or 1 (one).
-
Updating Offline System - Ideas Needed Please
DavidAM replied to poddys's topic in Application Design
If you provide an install link on the main system, you could set a cookie indicating the version that they last downloaded. Then check that cookie at login to see if it is older than the current version and suggest they install the latest and greatest. Of course, the existance of the cookie will only indicate that they downloaded it. If the install fails or is cancelled, it would be wrong. Or, if the install is an executable running on the client's computer, you can have the last step of the install be to open a specific page with a specific parameter and have that page set the cookie indicating a successful install of the version. If the offline version is running PHP, you could have it use curl to request a specific page from the main site which would return the current version level available. Then it could suggest that the user visit the update page to download the latest. Off course, since it's the offline version, the curl request might fail if there is no internet connection. Of course if it succeeds, the offline version could tell the user that the internet is available and suggest they use the main site (with a link to open it). -
To have it happen in real-time, that is, as you type or when you leave one field and go to another, you are going to have to use Javascript and AJAX. However, unless there is some option I don't know about, Access only actually updates the record in the database when you leave the record (move to another record, or close the form). For that you would need an HTML form and an update button or next button that would POST the values and a php script to receive the values and update the database. I am not familiar with any tutorials, but I'm sure there are plenty out there. Maybe someone else will suggest one. But you need to decide which approach you are going to take so you know which tutorial to start with. Personally, I don't like to update the database without a specific request from the user. My daughter has a cat that likes to walk across my keyboard, and I would hate to think that my best customer's address is now "asdkj923n~afaklasd" and it is in the database and I have no way to find out what it was before ... "BAD CAT!! GET DOWN!!!
-
You cannot run a Javascript function from PHP. PHP runs on the SERVER before the page is sent to the browser. Javascript runs on the CLIENT (browser) after PHP is completely done and exited. There is no way that I know of, to "magically" get the client's timezone. I sure wish they would add it to the HTTP specifications, have the client send current time zone in a header with the original GET request, but it is not there. The best you can do, is show the time and indicate the timezone that you are using for the display. Then give the user an option to specify their timezone. Once the user has selected a timezone, store it in a cookie so you can get it next time without asking them. If these are registered users, you can store it in the database with their profile and carry it in a SESSION. If there is a form the user completes and submits before getting to this page, you could have Javascript on that page to populate a hidden field with the DST value so you can use it on the page that processes the form. If the user does not have Javascript enabled, the value would be empty and then you use the cookie idea above.
-
Code is complete...but doesn't run..get 500 error
DavidAM replied to Luke Martin's topic in PHP Coding Help
The header redirect does NOT end the PHP script. It is a function that places an HTML header into the queue to be sent with the rest of the headers. As a result, if you do NOT put an exit() after this header() function call, the script will continue to run. If the programmer "thinks" the header() call ended the script and sent the user away, there may be sensitive code that is otherwise unprotected. if ($_SESSION['user'] == 'The Boss') { header('Location: http://www.mysite.com/IamWorking.php'); } echo 'The Boss is a stupid jerk!'; In this case, even though you have sent a header for "The Boss" the echo will still be executed. If there is some delay in the redirect, that text may actually make it to the screen. If, instead of an echo, we perform some database manipulations because we think we have a valid user, those manipulations will be executed either way. Headers are (apparently) sent to the browser in one chunk, not one at a time as you call header(). In the OP's script, since the header() is the last thing in the script, it is technically not necessary to include the exit(). However, it is not a good habit to get into. If some code were later added after that part of the script, the header() would be left hanging. Just be aware, the header() call DOES NOT END THE SCRIPT. -
If your source code file is saved in unix format it does not have CR's (\r) in it. Windows systems end lines with CR-LF while unix based systems use only NL. So, your HEREDOC does not have CR's in it. I'm guessing your browser was running on a Windows system so it probably put the CR's in the response. Hmmm, maybe I should add that to my POST parameter cleanup. Always remove "\r" from any TEXTAREA's? I'll have to look at that.
-
nl2br() might leave the CR ( "\r" ) in there, I'm not sure. You might want to do a str_replace() on it just to be safe: $chk = str_replace("\r", "", stripslashes(nl2br($_POST['code'])));
-
Automatically not accept empty entries or delete empty entries on table
DavidAM replied to xerox02's topic in PHP Coding Help
going back to your previous post: { if(empty($_POST['quotes'])) { // Tell them they missed a required field echo 'missed'; } if (isset(($_POST['quotes'])) { // THERE IS AN EXTRA PARENTHESIS HERE mysql_query($sql,$con); echo 'Thanks for your submission '; mysql_close($con); } you have a syntax error in the IF (ISSET( statement. but you do not want to use ISSET there. A variable can be SET and still be EMPTY. You should probably just use an ELSE (off of the IF (EMPTY( above it ) { if(empty($_POST['quotes'])) { // Tell them they missed a required field echo 'missed'; } else { mysql_query($sql,$con); echo 'Thanks for your submission '; mysql_close($con); } -
Use your browser's "View Source" feature to look at the output. Or, even better, use var_dump() on each variable, instead of echo. The browser will hide spaces, newlines, etc. from view. A newline will still be difficult to see in View Source. var_dump() will give you the variable type, length and content. You should be able to see the true difference.
-
rnrn coming into form where I put an "enter" in the code.
DavidAM replied to TeroYukio's topic in PHP Coding Help
$clearString = str_replace("\r\n", "", $badString); although, I would probably replace them individually. That way if the CR is not there, the NL is still replaced: $clearString = str_replace(array("\r", "\n"), "", $badString); -
1) Is there a session_start() in your config.php? The code you showed does not start a session but references $_SESSION variables. This would result in the query returning zero rows and therefore, no output. 2) turn on error_reporting and display_errors (PHP) so you can see any errors that are being reported (including the reference to non-existant session variables) then fix those errors. 3) if you request notifications.php directly from the browser, do you get anything? Note: you may need to look at "View Source" to see what is actually returned. Note: I have never used jQuery, so I can't really say whether that code has problems or not. However, if I understand what it is supposed to do, I think it is a pretty interesting approach. Good Luck.
-
I read something about this the other day. It seems that when the combobox closes, the style of the SELECT element takes control rather than the style of the selected OPTION. At least, this happens in some browsers. The suggested solution was to use some javascript for the onClick attribute (I think) that would copy the style from the selected OPTION to the style of the SELECT element. I came across this when I was googling for a combobox color selector (or something like that).
-
HTML multi-select form values don't appear in MySQL database?
DavidAM replied to pahunrepublic's topic in MySQL Help
I didn't notice the strip_tags() you had in there. That will help with the xss. But you really need the mysql_real_escape(). The other way to do the loop would be: // Original code for($i = 0; $i < count($hobby); $i++) { $h = $hobby[$i]; } // As I said this REPLACES the value so you end up with the last one // It would be the same as this one line $h = end($hobby); // Correct way $h = ''; // Start with an empty string for($i = 0; $i < count($hobby); $i++) { $h .= $hobby[$i]; } // Or, if you are not sure the indexes are sequential $hList = ''; foreach($hobby as $h) { $hList = $hList . $h; } // The dot-equal operator is the short form -- these two statements are the same $hList = $hList . $h; $hList .= $h; Overall, I think implode is faster and simpler to code. The separation string can be more than one character (or it can be empty): $hobby = array('coding', 'sleeping', 'drinking'); $h = implode(' and ', $hobby); // gives: coding and sleeping and drinking $h = implode('', $hobby); // gives: codingsleepingdrinking -
Actually, whitespace might not be so easy to see. If you are running in a browser it will consolidate spaces. If you are running from the command line, it would depend on the font your are using. Your image looks like a proportional space font which would make it hard to determine if there is a space. You could try using var_dump() instead of echo (just before the if). It will show you the datatype, length, and value of the variable, and might give some insight into why it is not working. var_dump(sep_cond[$c]); var_dump(acc_day[$d]);
-
HTML multi-select form values don't appear in MySQL database?
DavidAM replied to pahunrepublic's topic in MySQL Help
for($i = 0; $i < count($hobby); $i++) { $h = $hobby[$i];} This is replacing the hobby value in $h each time you go through the loop. So you only get the last value in the array. If you are trying to build a comma-delimited list, the easiest way is to get rid of the loop and use implode() $hobby = $_POST['hobby']; $h = implode(',' $hobby); Notice: You are using the $_POST values without any validation or protection. This can leave you open to sql injection attacks as well as possible xss attacks. For SQL protection, look at mysql_real_escape(). For the xss, you need to validate the contents of the fields. Just because you put a SELECT list on the form with the name 'hobby' does not mean that the user submitted something from that list. It is very easy to send something completely unexpected. This applies to ALL input from the browser. -
I almost always include an AUTO_INCREMENT column and make it the primary key. Integers are cheap (except in VLDB) and it makes changes easier. For instance, if you later decide to add some statistics about this game_schedule, you have a unique key to reference in your new table; without it, you have to 1) modify the table and your code to add this key; or 2) use a multi-column key in the new table. Concerning your primary key definition: PRIMARY KEY (`sid`,`venue_id`,`game_type_id`,`day_of_week`) Since 'sid' is auto_increment, it is unique, you should use it alone as the primary key. The rest of the fields in that definition are just wasting space. The only way an index will be used in a query is if you reference one or more fields from the start of the list. For instance: //This will use the key SELECT * FROM game_schedule WHERE sid = 10 and venue_id = 9; // This will NOT use the key SELECT * FROM game_schedule WHERE venue_id = 9; Also, since sid is unique, you do not need to include anything else in the WHERE clause, you have already identified the record with side=10. Add additional keys for fields you will use in queries. For multi-column indexes, put the columns in the order of most likely to be used in a query INDEX (venue_id, game_type_id); INDEX(game_type_id, day_of_week); A query can only use one index per table, and it will (should) use the one that results in the fewest number of rows to scan. With the two additional indexes above: //this query has no index to use and will do a table scan SELECT * FROM game_schedule WHERE day_of_week = MONDAY // This query should use an index SELECT * FROM game_schedule WHERE day_of_week = MONDAY AND game_type_id = 4 These are general database design statements. It is possible that some database engines may be optimized to use part of an index without specifying the first field. But without knowing the engine you are using, and without researching the internals of that engine, these are good rules-of-thumb to go by.
-
Notice: Trying to get property of non-object on Localhost
DavidAM replied to neonio's topic in MySQL Help
I don't actually use mysqli, but the error message says $result is not an object. Therefore, $db->query() failed. That generally means there is a syntax error in your SQL statement. $query = "SELECT * FROM 'test'"; the table name does not be surrounded by single-quotes. $query = "SELECT * FROM test"; should be correct. If you not "quotes" around a table (or column) name, use back-ticks: $query = "SELECT * FROM `test`"; -
need array for form data using forms name, fieldid and DB table name
DavidAM replied to jasonc's topic in PHP Coding Help
Something like this? $formarray= array('fieldid1'=> array('Caption'=>'User name', 'DBfield'=> 'hdyfu'), 'fieldid2'=>array('Caption'=>'Name', 'DBfield'=>'col2')); -
how to streamline a potentially huge set of fields
DavidAM replied to turpentyne's topic in MySQL Help
You are going to have to DESIGN the database. I thought you were on the right track when you said: But then you went off to put everything in one table. Basically, I would start DESIGNING this database as: CREATE TABLE Items( ItemID INTEGER AUTO_INCREMENT, Description VARCHAR, PRIMARY KEY (ItemID) ); CREATE TABLE Continents ( ContinentID INTEGER AUTO_INCREMENT, ContinentName VARCHAR, PRIMARY KEY (ContinentID) ); CREATE TABLE SubContinents ( SubContinentID INTEGER AUTO_INCREMENT, ContinentID INTEGER, -- Shows which continent this subcontinent is on SubContinentName VARCHAR, PRIMARY KEY (SubContinentID) FOREIGN KEY FK_SubsContinents_Continents (ContinentID) REFERS TO Continents ON UPDATE CASCADE ON DELETE CASCADE) ); -- Then Countries which refers to the Subcontinents -- Then Provinces (States) which refers to the Countries -- Then the next level -- And so on CREATE TABLE ItemLocation ( ItemID INTEGER, LocationID INTEGER, -- Refers to your lowest level Location table (States or Departments or whatever) ... -- Any other fields that are needed at this level PRIMARY KEY (ItemID, LocationID) FOREIGN KEY ... REFERS TO Items FOREIGN KEY ... REFERS TO Location ); Of course that is my first pass. I would also look at the application requirements and the expected data to determine just how this breakout needs to be designed. For instance, are all countries on a sub-continent? or would that have to be referenced differently? There are other similar questions that would need to be answered. But NO! you do NOT want to put all of those "Locations" as individual columns in ONE table. -
Yes, there is a specific problem and it is rather subtle. The way you wrote your echo line: echo "<table><tr><td>".ClickImage($ItemArray['Image'], "ItemDescription. ... would work if ClickImage() returned a string. But ClickImage() does not return a string, it does not return anything, so there is nothing in the cell. However, since ClickImage() has some HTML outside of the PHP tags, that HTML text is "sent to the browser". Now consider the sequence of events in this line of code. In order for the ECHO statement to send the (table) HTML it has to FIRST evaluate ClickImage(). So ClickImage() is executed (which causes the IMG tag to be sent to the browser). It returns nothing, so (basically) an empty string "replaces" it in the echo statement, THEN the TABLE tag is sent to the browser (remember, the IMG tag was already sent). To fix this, you should have ClickImage() return the string (if you have ClickImage() echo the string you will get the same result). This is why some of the PHP builtin functions (such as print_r) have a $return parameter that let's you control whether the function sends the output to the browser or return it as a string: function ClickImage($Image, $URL, $LinkID) { ?> <img src=<?echo $Image;?> onclick="window.open('<?echo $URL.sha1($LinkID);?> ','','width=250,height=200')" class=parchment><? } I would replace it with something like function ClickImage($Image, $URL, $LinkID, $return = false) { $out = '<img src="' . $Image . '"' . ' onclick="window.open(\'' . $URL.sha1($LinkID) . '\',\'\',\'width=250,height=200\')" class=parchment>'; if ($return) return $out else echo $out; } It is always a pain to get both single and double quotes inside a string. I'm sure that could be cleaned up a bit. It is presented here as an example of how to handle the output functionality. Oh, and don't use short-tags ( <? ) these will lead to problems if you move to a site that does not support them. Always use full tags ( <?php )
-
This code: $data = array($user_name => $numTickets); } is REPLACING the contents of $data with the current record. So in the end, you only have the last record from the database. If you change it to: $data[$user_name] = $numTickets; } $pot = array(); foreach($data as $user_name => $numTickets) { $pot = array_merge($pot, array_fill(0, $numTickets, $user_name)); You should get the array in $data that you expect and the rest should work as expected.
-
The error probably stems from an INSERT statement like: INSERT INTO tablename VALUES (1, 'TestName', 'Something'); // Which would essentially be the same as INSERT (id, name, info) INTO tablename VALUES (1, 'TestName', 'Something'); If there are no fields named in the INSERT statement (first one above), the server wants a value for EVERY column that exists in the table. When you add a new column, there are suddenly more columns than values and the INSERT will fail. This is one reason that I ALWAYS name the columns that I am INSERTing into. If you don't and you need to change the table structure, you have to modify EVERY insert statement in the application instead of just the ones that will include the new column. You will have to find all of the INSERT statments and either add a column list (like the second example) or add a value for the new field (an empty string in this case). Of course, your browse page is not going to magically update this column (well, it might if the programmer considered the possibility) and is not going to magically display this column (again, unless the programmer considered the possibility). Of course, if the progammer HAD considered the possibility, then he would most likely have provided an INSERT that would work under the circumstances. What exactly are you trying to accomplish? An ares to type the notes on the screen so you can print them A blank spot to write in (after printing the current page) Add these notes to the database so you can retrieve them and see them later The amount of code changes depends on your answer to the above. It is highly unlikely that you can get the functionality you are looking for by simply changing the database.
-
$lastpost = mysql_query("SELECT * FROM Stacks WHERE ip='$visitor' ORDER BY posted DESC")or die (mysql_error()); while($latest = mysql_fetch_assoc($lastpost)){ $thelatest= strtotime($latest['posted']); } That query is retrieving EVERY post from that user. The ORDER BY posted DESC means you get the newest one first and the oldest one last. Then you loop through the entire resultset calculating $thelatest. This means at the end of the while loop, $thelatest contains the date of the user's FIRST post. // 1) ADD A LIMIT TO THE QUERY $lastpost = mysql_query("SELECT * FROM Stacks WHERE ip='$visitor' ORDER BY posted DESC LIMIT 1")or die (mysql_error()); // 2) GET RID OF THE WHILE LOOP, YOU DO NOT NEED TO WALK THROUGH THE RESULTS $latest = mysql_fetch_assoc($lastpost)); $thelatest= strtotime($latest['posted']);