Jump to content

Psycho

Moderators
  • Posts

    12,157
  • Joined

  • Last visited

  • Days Won

    129

Everything posted by Psycho

  1. First of all do not use "no" and "yes" for the $errors value. use the Boolean values of true/false (i.e. not the string values). It makes your code more logical. Then you don't need to do a test such as if($errors=='yes') Instead you can just do if($errors) Second, you have unnecessary duplication in your validation of the POSTed fields. You first check if the field is empty then check if the trimmed field is ="". You can do one check for that using if( empty(trim($_POST['fieldname'])) ) Also, I'm not sure it is even possible to have a POST value sent as NULL, so that validation is unnecessary. Now, looking at the logic of the code I am assuming that there are multiple records in the table for each user. Any appear to be trying to validate the user input to the records in the database looking for a match. But, you are trying to check a single value in the DB to see if it is equal to both answer1 and answer2 from the user input. That doesn't make sense to me. Are you asking the user to input the same value twice? If so, you could do that validation before running the query. Or, are there really TWO values in the DB record that you want to be validating the POSTed values against? if (($answer == $answer1Post) && ($answer == $answer2Post)) { I also see that you are running mysql_real_escape_string() on all the values, but you aren't using them in the query - that makes no sense. Also, you have an empty IF condition. Just reverse your logic and remove the ELSE condition. Plus, there is a problem with your logic. If there are multiple records to be checked and the user input matches one of the records the loop will continue to the next record, which may not match. So the result will be that there are no matches. You need to exit the loop when a match is encountered. Since you are comparing strings and they have to exactly match, you should convert the value to all lower or upper case (assuming these aren't passwords). You can resolve ALL these issues by changing your query to just look for a matching record instead of getting all of them and looping through them. But, then you would only have one error message that there were no matching records rather than the other message for no answers int he DB. Lastly, if you are not getting the results you expect add some debugging code to output values so you can see what is happening.
  2. Seriously? After all this work you now want to change the solution? I'm not going to write any code, but I'll give you some info to do it yourself: Instead of querying for ALL the records for the specified car ID, you would instead do a query for ONLY the records that conflict with the requested dates. Then check how many records were returned using mysql_num_rows(). If 0, there are no conflicts, if there are 1 or more, then there are conflicts. That way you don't need to process the DB results, just check how many results were returned. You will need to format the dates appropriately, but the query would look something like this. SELECT start_date FROM #__carbooking_bookings WHERE car_id = '$car_id' AND end_date > $requested_start_date AND start_date < $requested_end_date LIMIT 1
  3. The "/n", when used in a double quoted string, will create a line break. It makes the HTML source code much more logical and readable. Instead of: <tr><td>Value 1</td><td>Value 2</td><td>Value 3</td><td>Value 4</td><td>Value 5</td><tr><tr><td>Value 1</td><td>Value 2</td><td>Value 3</td><td>Value 4</td><td>Value 5</td><tr><tr><td>Value 1</td><td>Value 2</td><td>Value 3</td><td>Value 4</td><td>Value 5</td><tr> You would have <tr> <td>Value 1</td> <td>Value 2</td> <td>Value 3</td> <td>Value 4</td> <td>Value 5</td> <tr> <tr> <td>Value 1</td> <td>Value 2</td> <td>Value 3</td> <td>Value 4</td> <td>Value 5</td> <tr> <tr> <td>Value 1</td> <td>Value 2</td> <td>Value 3</td> <td>Value 4</td> <td>Value 5</td> <tr>
  4. There is nothing in your loop to define the variables you are using to calculate $resultCalc. So whatever those values are when the loop starts the result will be using those values on each iteration of the loop. I assume you meant to redefine those values based upon each record. I'm not sure if all three of those variables are supposed to be redefined for each record or not - I only see two that appear to match up to the DB fields listed, but here is an idea of how it might look (make corrections as needed). I also cleaned up the code to be easier to read. echo "<table>\n"; echo "<tr><th>Antal</th> <th>Type</th> <th>Mærke</th> <th>Str.</th> <th>Pris</th> <th>RESULT</th></tr>\n"; while($result = mysql_fetch_assoc($findBarId_query)) { $resultCalc = ($result['normalpris'] / ($str * $result['antal'])) * 0.5; echo "<tr>\N"; echo "<td>{$result['antal']}</td>\n"; echo "<td>{$result['type']}</td>\n"; echo "<td>{$result['maerke']}</td>\n"; echo "<td>{$result['stoerrelse']}</td>\n"; echo "<td style='text-align:right'>{$result['normalpris']}</td>\n"; echo "<td>{$resultCalc}</td>\n"; echo "</tr>\n"; } echo "</table>\n";
  5. I wish it was possible. This is how I store dates / date time in my personal coding but I'm getting these values from a CSV file that I'm importing into phpMyAdmin. So, convert the values to proper dates when putting them in the database instead of storing them as strings. Perhaps you are too young to remember the significance of Y2K
  6. @rich_traff Kinda difficult to debug your problem without seeing it all together, but I'll give it a go. In your controller "function" you have an if() condition as follows: if ( $datesAvailable = true) First off, if the value of $datesAvailable will be true or false, you don't need "test" if it is equal to true. That is redundant. Just use if ( $datesAvailable) But, the problems with what you have is 1) You are ASSIGNING the value of true to the variable $datesAvailable not testing it (which would be corrected by what I posted just above). 2) I don't see where you ever assigned a value to $datesAvailable before that if() condition is run. Variables created outside a function are not available inside a function. It is called variable scope. You can code around this, but it is bad practice. What I thin you should be doing is simply calling the datesAvailable() function INSIDE the if() statement. But, to do so you need to pass the required parameters. I only see the car_id defined in the function; you also need the start/end dates. Example: if ( datesAvailable($car_id, $startDate, $endDate)) So, you would need to pass the start/end dates to the function you have. Alternatively, you could define the $datesAvailable outside the function and then pass that to your function.
  7. Do you have any working code for the file upload process at all? What you are asking for would be a highly trivial task with working code, so I assume not. There are countless "PHP Upload" tutorials on the next. Go find one and create an upload page. You should be able to easily figure out the rename part since you have to name the file when you save it anyway. Post back with any problems you have. If you just want someone to build it for you then you can post in the freelance forum.
  8. You have an infinite loop for ($x=1;$x=2;$x++){ That loop will continue as long as you can continue to assign the value of '2' to the variable $x. You should have used something like: for ($x=1;$x<count($emote)+1;$x++){ But, the code is way more complex than it needs to be. Try this: //Create one array with key/value pairs $emote = array( "" => "lol.gif", ":S" => "caring.gif" ); //Convert each values to a full HTML image tag foreach($emote as &$value) { $value = "<img src='emos/{$value}' />"; } $msg = " hahahahaha verry funny :S"; //Replace ALL values with a single function $msg = str_replace(array_keys($emote), $emote, $msg); echo $msg; Output: <img src='emos/lol.gif' /> hahahahaha verry funny <img src='emos/caring.gif' /> By using a single array it is easy to see the relationship between the search and replacement terms
  9. Why would you want to rename the same function with different names. Just create the function ONCE, then use it multiple times. This is programming 101. In my second post I even provided an example of how you would reuse the function multiple times.
  10. First off, the field names from the different tables do not have to have the same names, but they MUST be the same field types. So, you can't have, for example, the third field from one table as an INT type and the third field form another table as a DATE type. Assuming this is the case, the field names returned for the records from ALL the tables will be the same. Either, the names from the first table will be used OR you can give the same aliases to the fields for each table. Here is a small tutorial on using UNION that may help: http://www.mysqltutorial.org/sql-union-mysql.aspx So, there are some problems with your query above. 1. You have 'user_id' as the first field for the first select statements, but it is the 2nd field in the last select. Also, the last select has 5 fields, whereas the first select statements have four. So, get the fields for each select statement in the proper order: "user_id", "date", "title/name", and "type". Since you also need the photo_id, you can grab the record ID from the other tables to be consistent or you can just define a static value as the 5th element for those tables. Here is one possible solution $query = " (SELECT `userid`, `lastupdate` AS `date`, `title`, `classifieds` AS `type`, `classifieds_id` AS 'rec_id` FROM `CLASSIFIEDS_DATA` WHERE `userid` = '$profileuserid') UNION (SELECT `userid`, `lastupdate` AS `date`, `title`, `reviews` AS `type`, `reviews_id` AS 'rec_id` FROM `REVIEWS_DATA` WHERE `userid` = '$profileuserid') UNION (SELECT `userid`, `lastupdate` AS `date`, `title`, `registry` AS `type`, `registry_id` AS 'rec_id` FROM `REGISTRY_DATA` WHERE `userid` = $profileuserid) UNION (SELECT `userid`, `date` AS `date`, `title`, `blogs AS `type`, `blogs_id` AS 'rec_id` FROM `blog_entry` WHERE `userid` = '$profileuserid') UNION (SELECT `User_id` AS `userid`,`Date_created` AS `date`, `Photo_name` as `title`, `Photos`AS `type`, `Photo_id` AS `rec_id` FROM `Photos` WHERE `User_id`= '$profileuserid') ORDER BY `date` DESC LIMIT 10"; If one of those tables doesn't have a unique ID for the 5th column you could set a static value like so: SELECT `userid`, `lastupdate` AS `date`, `title`, `registry` AS `type`, NULL AS 'rec_id`
  11. So, you are wanting to get the question AND all the related answers? The last query I provided will do that, but the logic is backwards on how I would normally do it. I would use this query: $query = "SELECT username, answervotes, answertext, answerscount FROM questions JOIN answers USING(qid) WHERE questions.qid = '$qid'"; Also, that query will return multiple records (one for each answer), but each record will contain the data for the question as well.
  12. You can't do a print_() inside an echo statement (I have made the same mistake before). You would have to assign the results of a print_r to a variable then echo out the variable. Since I did test the last code I provided, I am going to suggest you use that code. Also, it is more logical. Having a variable called $datesUnavailable and setting it to false to mean that dates are available is the programmatical equivalent of a double negative. It can be confusing. That is why I had changed the logic to determine in "datesavailable" is true. Below is a rewrite of your script above with my latest function incorporated with LOTS of debugging code to help pinpoint any issues if the results are not what you expect. As you have done previously, the script currently has a hard-coded $bookedDates array defined in the datesAvailable() function. Once that works, you can remove the hard-coded array and uncomment the query to get the booked dates from the DB. I modified the function to take a parameter for the card ID and to run the query for booked dates in the function. If I didn't make any errors it should work once you uncomment the line to run the query. <?php //Returns true/false based upon whether the requested dates conflict //with the already booked dates in the DB for the requested car id function datesAvailable($car_id, $start_date_ts, $end_date_ts) { /* $query = "SELECT start_date, end_date FROM #__carbooking_bookings WHERE car_id = '$car_id'"; $db->setQuery($query); $bookedDates = $db->loadRowList(); */ //Hard coded test array $bookedDates = array( array ('07/20/2011', '07/21/2011'), array ('08/16/2011', '08/17/2011') ); #DEBUG echo "Booked dates array contains " . count($bookedDates) . " records<br><br>\n"; echo "Checking availabliity from " . date('m-d-Y', $start_date_ts) . " to " . date('m-d-Y', $end_date_ts) . "<br><br>\n"; foreach($bookedDates as $key => $dates) { #DEBUG echo "Checking booked dates of {$dates[0]} to {$dates[0]} - "; //Create variables for rrecord start/end dates list($bookedStart, $bookedEnd) = $dates; if ($start_date_ts < strtotime($bookedEnd) && $end_date_ts > strtotime($bookedStart)) { //Conflicts detected, return false #DEBUG echo "CONFLICT, Return FALSE<br>\n"; return false; } } #DEBUG echo "No Conflicts, Return TRUE<br>\n"; //No conflicts detected, return true return true; } //End function datesAvailable() $car_id = JRequest::getInt('id', ''); if (!datesAvailable($car_id, $start_date_unixTime, $end_date_unixTime)) { $redir_url = 'index.php?option=com_carbooking&id=' . $row->id . '&view=single'; $redir_msg = "Your selection falls over another booking, dates Unavailable: start date: {$start_date_unixTime}, end date: {$end_date_unixTime}, car id: {$car_id}"; $this->setRedirect(JRoute::_($redir_ur), $redir_msg); } ?>
  13. If you are getting the "all fields required" response, then one of the four variables are empty. Make sure you have form fields that correspond to the fields referenced at the beginning of the script. I didn't use all of your original form field names because I just use whatever makes sense to me when I am coding on the fly. Either change the form field names to match the POST variables below OR change the POST variables below to match your form field names. //process the input into variables $username = strtolower(strip_tags(trim($_POST['username']))); $oldpass = strip_tags(trim($_POST['oldpass'])); $newpass1 = strip_tags(trim($_POST['newpass1'])); $newpass2 = strip_tags(trim($_POST['newpass2']));
  14. That is not very helpful - at least post the errors you are getting. Although I do see in the code I provided I ended two lines with a period instead of a semi-colon. But, if you can't find those errors by yourself . . .
  15. There is nothing wrong with that code. PHP has no clue about where it is echoing content. It simply outputs the code and send the result to the browser.What is the background color of that page? have you checked the actual HTML source code?
  16. Ah, I didn't catch that. So, am I correct to assume that there is one, and only one, record from each table matching the "qid"? If so, this will work: $query = "SELECT username, answervotes, answertext, answerscount FROM answers JOIN questions ON answers.qid = questions.qid WHERE qid = '$qid'"; $result = mysql_query($query); //If only ONE record is expected $rowuser = mysql_fetch_assoc($result); echo "Username: {$rowuser['username']}<br>\n"; echo "Answer Votes: {$rowuser['answervotes']}<br>\n"; echo "Answer Text: {$rowuser['answertext']}<br>\n"; echo "Answer Count: {$rowuser['answerscount']}<br>\n"; If there is a many to one or many to many relationship, you need to provide more specifics of what you are trying to do.
  17. A few things. 1. You appear to be trying to get multiple columns for the same record (or records), so you just need to include all the column names in the SELECT portion of the query. 2: In the query you are specifying a WHERE clause using "WHERE qid='$qid'". If there is only one record matching that value, then it will only return one record. If you want all records matching multiple values you can use "WHERE qid IN ('$qid1', '$qid2', '$qid3', '$qid4')" 3: You are only extracting one record from the results. So, even if there are multiple records returned from the query, you are only seeing the first one. You need to use a while() loop to process all the results. If only one record should be returned, then you don't need a while() loop $query = "SELECT username, answervotes, answertext, answerscount FROM answers WHERE qid = '$qid'"; $result = mysql_query($query); //If only ONE record is expected $rowuser = mysql_fetch_assoc($result); echo "Username: {$rowuser['username']}<br>\n"; echo "Answer Votes: {$rowuser['answervotes']}<br>\n"; echo "Answer Text: {$rowuser['answertext']}<br>\n"; echo "Answer Count: {$rowuser['answerscount']}<br>\n"; //IF MULTIPLE records are expected while($rowuser = mysql_fetch_assoc($result)) { echo "Username: {$rowuser['username']}<br>\n"; echo "Answer Votes: {$rowuser['answervotes']}<br>\n"; echo "Answer Text: {$rowuser['answertext']}<br>\n"; echo "Answer Count: {$rowuser['answerscount']}<br>\n"; }
  18. Yes, I do. But, I won't answer here because you don't want to start a new thread for a question that is unrelated to this post.
  19. You should properly indent your code blocks - especially when you have nested if/else statements. If so, you may have seen the error. You have a top-level if/else statement where you are checking if several fields are empty. If not, you will have a defined query in the ELSE condition. however, if the first IF condition is true, you have two child-level IF statement. And, only if those two child-level IF statemetns are true are you defining a query. No query is defined if the first IF condition is true and one of the child IF statements are false. You also seem to be missing a couple of {}. Here is your code with everything indented based upont he logic - I added a couple of ELSE conditions to show where the error is likely occuring if (!$error) { if (!empty($first_name) && !empty($last_name) && !empty($username) && !empty($gender) && !empty($email)) { // Only set the picture column if there is a new picture // Only set the password in there is a new one if (!empty($new_picture)) { if (!empty($new_password1)) { if (empty($age)) { $query = "UPDATE registration SET first_name = '$first_name', last_name = '$last_name', username = '$username', gender = '$gender', email = '$email', age = '$age', password = '$new_password1', picture = '$new_picture' WHERE user_id = '" . $_SESSION['user_id'] ."'"; } else { //No query defined } } else { //No query defined } } else { $query = "UPDATE registration SET first_name = '$first_name', last_name = '$last_name', age = '$age', username = '$username', gender = '$gender', email = '$email', lookingfor = '$lookingfor', haircolor = '$haircolor', height = '$height', education = '$education', drink = '$drink', children = '$children', ethnicity = '$ethnicity', smoker = '$smoker', interests = '$interests', aboutme = '$aboutme', password = '$new_password1', picture = 'new_picture' WHERE user_id = '" . $_SESSION['user_id'] . "'"; } } }
  20. Don't use Opera. Sorry, couldn't resist.
  21. Something to consider: I would advise against using nl2br() before saving the data to the database. You can always use nl2by when displaying the content to the page. The reason I say this is if you ever need to allow the user to edit the value. By using nl2br() when saving the content you have to then reverse that process before populating the data back into a text/textarea. Besides, You should be using nl2br() IN ADDITION TO htmlentities() to ensure the user input does not screw up the HTML code or introduce XSS exploits. So, I would advise storing the data exactly as the user input it (unless there is something you absolutely have to strip out). Then use the appropriate conversion based upon how you are displaying it.
  22. Yeah, right. I find it very interesting that the code from the "copied" page would produce the error you posted with input using a quote mark, yet you now say that you are actually using the 2nd code you posted which would not cause that error. Have your "or die" clause echo the query to the page and you should see exactly what the error is. $result= mysql_query($query) or die("Query: {$query}<br>Error: ".mysql_error());
  23. Where is your validation code that is finding the input to be in error? My guess is that your server has "magic quotes" enabled which is automatically adding a backslash before quote marks. You should have a generic script that checks if magic quotes are enabled and, if so, uses stripslashes() on all user input. See Example #2: http://php.net/manual/en/security.magicquotes.disabling.php
  24. 1: The edit button is only available for a short while after posting 2: Since adding some HTML worked for some browsers. Try creating a complete, valid HTML page.
×
×
  • 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.