Jump to content

Landslyde

Members
  • Posts

    93
  • Joined

  • Last visited

  • Days Won

    1

Everything posted by Landslyde

  1. Is it possible to read x number of chars in a PHP string? Here is my problem: I want to set MySQL's NOW() to a PHP variable: $year = 'Select NOW()' then read the first four chars of that variable to get the year. This is for my website that will track fiscal quarters for clients. And since this will be ongoing, year after year, I really don't want to hard-code the year in my query: "SELECT SUM(balances) FROM fiscalTable WHERE date BETWEEN `2015-01-01` AND `2015-03-30`" If I did it that way, I'd have to remember to change it yearly, etc. What I'd rather do, if it's possible, is read the first four chars from the var $year that I set above. If I cld do that, then my code wld be once and done. Any suggestions, Forum?
  2. Thanks, maxxd. I actually came up with a solution on another forum that, while much like your suggestion, does what I need it to do. catch (PDOException $ex){ if ($ex->getCode() == 23000) { //Check if it's a duplciate key violation. if (preg_match("/email/", $ex)) { $emailErr = '* Email address already in use'; } elseif (preg_match("/username/", $ex)) { $unameErr = '* Username already in use'; } else { throw $ex; //Rethrow other errors echo '<script type="text/javascript">alert("There was an unforeseen database error."+"\\n"+"The database administrator has been contacted."+"\\n"+"\\n"+"Please try again later. Thank you"); </script>'; mail_error($ex); } } As you can see in the error I posted, it lets me know what key threw the error: either email or username, the two Unique columns of this table. And this little bit of preg_match code keys in on those two words and forks the correct error msg to the user. And I'm rolling again Thanks for your input, maxxd.
  3. ** Hehehe...sorry abt the double post. I failed to see the error output had the two words running together, hit rewind and edited it and got two posts. Cheesy ** Frustrating. I know which key I'm violating but don't have the smarts to catch it in the Try/Catch block. I've looked high and low on the web and can't find even one example on this. The error: * Username already in use: exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'cowboy@dfwit.co' for key 'email'' in /var/www/html/dfwit/register.php:36 Stack trace: #0 /var/www/html/dfwit/register.php(36): PDOStatement->execute(Array) #1 {main} As you can see, it's the email duplication that throws the exception, even giving me the key 'email' to assist me in coding the exception. I just can't get that part right. I can't figure out how to code it. I appreciate any help you can offer, Barand. Thank you.
  4. Frustrating. I know which key I'm violating but don't have the smarts to assimilate it in the Try/Catch block. I've looked high and low on the web and can't find even one example on this. The error: * Username already in useexception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'cowboy@dfwit.co' for key 'email'' in /var/www/html/dfwit/register.php:36 Stack trace: #0 /var/www/html/dfwit/register.php(36): PDOStatement->execute(Array) #1 {main} As you can see, it's the email duplication that throws the exception, even giving me the key 'email' to assist me in coding the exception. I just can't get that part right. I can't figure out how to code it. I appreciate any help you can offer, Barand. Thank you.
  5. MySQL How do I get it to show me the key? Sorry, but I've never handled this particular kind of error before. Your insight wld be appreciated. Thanks, Barand.
  6. In the beginning, I had just one unique key set up in a table: username. When a username duplication in the Registration form was entered, I'd catch it and handle it like this: catch (PDOException $ex){ if ($ex->getCode() == 23000){ //Check if it's a duplciate key violation. $unameErr = '* Username already in use'; } Today, in the same table, I added another unique key: email. Now that I have two unique keys that will both have the same violation, how can I tell which one was the duplicate so I can provide the correct error to the user?
  7. Just tying to make a two-column display. I have a page that does three, but can't get this page to do two. Here's my CSS: #container { width: 100%; margin: 0 auto; #background-color: #000; } #left { float: left; width: 360px; margin: 0; padding: 2em; #padding-bottom: 8em; } #right { float: left; width: 360px; margin: 0; padding: 2em; #padding-bottom: 8em; } #middle { margin-left:auto; margin-right: auto; padding: 2em; #padding-bottom: 8em; } Here's the HTML: <div id="container"> menubar stuff here <div class="left"> More stuff here </div> <div class="right"> More stuff here </div> </div> I've also tried moving stuff around in the css file: #container { width: 100%; margin: 0 auto; #background-color: #000; } #left { float: left; width: 360px; margin: 0; padding: 2em; #padding-bottom: 8em; } #middle { margin-left:auto; margin-right: auto; padding: 2em; #padding-bottom: 8em; } #right { float: left; width: 360px; margin: 0; padding: 2em; #padding-bottom: 8em; } The right class always moves below the left class. How can I get it to the right side of the left class?
  8. For me, that query's really involved, not just a simple SELECT going on there. Am thankful to you for the example to learn by...and your follow-up explanation. Goes a long way with me. Have a good one, kicken.
  9. kicken, yes, each attendee will have but one entry per week, so, with their attendeeid's, last_payment will be unique for each attendee. I'm flipping back and forth, studying your code. I really appreciate your help, kicken. I never come here trying to get anyone to write my code for me; I only need small examples like you provided to steer me in the right direction. That said, I do have a question about part of the query. Will you kindly explain this to me, especially the use of maxHistory. As I see it, you're setting the proceeding SELECT stmt to maxHistory, right? Sorry, just trying to wrap my mind around it to where I understand as it's suppose to be. A HUGE thanks to you! INNER JOIN (SELECT attendeeid, MAX(last_payment) as last_payment FROM history GROUP BY attendeeid) maxHistory ON maxHistory.attendeeid=a.attendee
  10. Well, this makes the 8th forum today I guess this is not the normal query to be asking help on, but here we go. I have these two tables: attendees & history. Each attendee has a single entry in the attendee table, while all their payments to the therapist are stored in the history table. What I'm trying to do is retrieve the fields from the last payment the attendee made in the history table using MAX(historyid) and attendeeid, while getting their name from the attendee table. Here are the table schemas: TABLE `attendees` ( `attendeeid` int(10) unsigned NOT NULL AUTO_INCREMENT, `fname` varchar(20) NOT NULL, `lname` varchar(20) NOT NULL, `dojid` varchar(10) NOT NULL, `address1` varchar(25) NOT NULL, `address2` varchar(25) NOT NULL, `city` varchar(20) NOT NULL, `state` char(2) NOT NULL, `zipcode` varchar(5) NOT NULL, `phonenumber` varchar(15) NOT NULL, `paroleofficer` varchar(20) NOT NULL, `childrest` char(1) NOT NULL, `casedetails` varchar(255) NOT NULL, `disdate` date NOT NULL, `memberid` int(10) unsigned NOT NULL, PRIMARY KEY (`attendeeid`), KEY `memberid` (`memberid`), CONSTRAINT `attendees_ibfk_2` FOREIGN KEY (`memberid`) REFERENCES `members` (`memberid`) ON DELETE CASCADE ON UPDATE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8; TABLE `history` ( `historyid` int(10) unsigned NOT NULL AUTO_INCREMENT, `amount` float NOT NULL, `subsidy` char(1) NOT NULL, `last_payment` date NOT NULL, `amount_paid` float NOT NULL, `balance` float NOT NULL, `attendeeid` int(10) unsigned NOT NULL, `memberid` int(10) unsigned NOT NULL, PRIMARY KEY (`historyid`), KEY `attendeeid` (`attendeeid`), CONSTRAINT `history_ibfk_2` FOREIGN KEY (`attendeeid`) REFERENCES `attendees` (`attendeeid`) ON DELETE CASCADE ON UPDATE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8; The query I'm using sure 'nuff returns MAX(historyid), but the fields are from the first-ever entered row in the History table instead of the MAX(historyid) row. select * from (select a.attendeeid,max(h.historyid),fname,lname,h.last_payment,h.balance FROM history AS h INNER JOIN attendees AS a ON a.attendeeid = h.attendeeid group by a.attendeeid) as maxHistoryPerAttendee where attendeeid = 29 I'm new to MySQL and really don't even understand the query (given to me by someone in another forum). Would someone please help me out of the woods here? I've been at this one for a day and a half with nothing to show for it.
  11. If I see this right, you're not starting a session until the user gets to Login_successful.php, which comes after the user logs in on Inlog.php, where you're trying to set $_SESSION variables $_SESSION['myusername']= "myusername"; $_SESSION['mypassword']= "mypassword"; header("Location: Login_success.php"); You have to start your session on the Inlog.php (if not sooner) to be able to set your SESSION variables. I.E. - start_session() has to be called before you can set $_SESSION['variables']. Also, your header has them being directed to Login_success.php, yet the page you gave us in called Login_successful.php. Change that, start your session on the login page so that your $_SESSION vars carry from one page to the next. Also, as you move from one page to the next, be sure to set those $_SESSION vars: SomePageAfterInlog.php $myun = $_SESSION['myusername']; $mypw = $_SESSION['mypassword']; Then use those variables ($myun AND $mypw) on that page. And on any pages the user goes after logging in, be sure to set your $_SESSION vars like I just showed you. Every page you want to use them has to have them.
  12. Thanks, Barand. I tried every way but the right way on that one. Much appreciated. Think I'm gonna call it a day now
  13. $id = $_POST['view']; // Pass this to see which attendee was selected in the Selection box options $stmt = $db->prepare('SELECT a.fname, a.lname, h.amount, h.subsidy, h.last_payment, h.amount_paid, h.balance FROM attendees As a INNER JOIN history AS h ON a.attendeeid = :id AND h.attendeeid = :id'); $stmt->bindValue(':id', $id, PDO::PARAM_INT); $stmt->execute(); $result = $stmt->fetchAll(); I know the last part of the query is wrong, the ON a.attendee = :id, etc. But how do I pass $id off to both a.attendeeid and h.attendeid and make it work? I need to be able to somehow make it ON a.attendeeid = h.attendeeid'); ...but that makes them devoid of $id. I've also tried $stmt = $db->prepare('SELECT a.fname, a.lname, h.amount, h.subsidy, h.last_payment, h.amount_paid, h.balance FROM attendees As a INNER JOIN history AS h WHERE a.attendeeid = :id AND h.attendeeid = :id'); $stmt->bindValue(':id', $id, PDO::PARAM_INT); but that offers nothing either. Any suggestions?
  14. Very much appreciated, CroNIX. I'm indebted to you and mac_gyver BIG time. Many thanks...
  15. Thanks for your input, mac_gyver, for opening my eyes to a select box that I saw change every time I used it Sometimes you really can't see the trees for the forest.
  16. How do I retain the name I selected after clicking the "Fetch" button? Is there a way to do this? I ask because you're 110% correct with your answer. I wasn't paying attention to the fact it was reverting back to "Select". I manually set it and my update held. Thank you for that. But, again, is there a way to retain the selected name after clicking the "Fetch" button?
  17. I tried putting the PHP code here, but it goes every which way when I paste it. Please view the link I posted above.
  18. Have a look. All the code is on one page. http://paste.ofcode.org/h8gVHHCuTKMW4YJXqR53jV
  19. No, the two submit buttons and the select box all have unique names. No conflict there that shows its face. The only quirky thing going on is $id losing its value as it goes from the SELECT to the UPDATE.
  20. I load a Selection box's options with names from my db. The <select> name = 'view'. When I select a name, it loads input boxes with the record by the selected name's ID in the table. So I then set the variable $id to that: $id = $_POST['view']; // From <select> option if($_POST['fetch']) { $stmt = $db->prepare('SELECT * FROM attendees WHERE memberid = :memberid AND attendeeid = :id'); $stmt->bindValue(':memberid', $memberid, PDO::PARAM_INT); $stmt->bindValue(':id', $id, PDO::PARAM_INT); $stmt->execute(); $result = $stmt->fetchAll(); etc etc The record pulls up fine. But after doing any editing, I submit it to UPDATE the record and it doesn't get updated. So I set an echo one line above my UPDATE statement: } elseif($_POST['update']) { echo 'Attendee ID is '.$id; // $id is gone at this point $stmt = $db->prepare('UPDATE attendees SET fname = :fname, etc, etc WHERE attendeeid = :id'); } only to find that the value of $id is missing. Does the fact that I bind $id to :id in my SELECT query cause $id to become empty after executing that query? The value of $id is held through the SELECT stmt but is lost when I get to the UPDATE stmt. Any ideas? This is driving me nuts!
  21. geniesgona: Read this: http://codular.com/php-mysqli It will give you beginner information for connecting to a database.
  22. mac _gyver: I'll check all you suggested. Really appreciate your input and advice on what to test, etc. I can tell you now that the only call to a session destroy is by actually clicking 'Logout' on the menubar, taking the user to a logout page that unsets, destroys, the redirects to the index page. No, this is more like the session timing out, or, like you say, the sessionID somehow changing and not matching up with the cookieID. Since I've shown you all my php settings and you see nothing awry, then it sort of boils down to my coding. And that wldn't surprise me. Thanks for your help. Seriously appreciated.
  23. I got it off an online tutorial. Live and learn
  24. mac_gyver: have restarted apache again. Here are my settings per phpinfo: session Session Support enabled Registered save handlers files user Registered serializer handlers php php_binary wddx Directive Local Value Master Value session.auto_start On On session.cache_expire 180 180 session.cache_limiter nocache nocache session.cookie_domain no value no value session.cookie_httponly Off Off session.cookie_lifetime 5000 5000 session.cookie_path / / session.cookie_secure Off Off session.entropy_file /dev/urandom /dev/urandom session.entropy_length 32 32 session.gc_divisor 1000 1000 session.gc_maxlifetime 5000 5000 session.gc_probability 1 1 session.hash_bits_per_character 5 5 session.hash_function 0 0 session.name PHPSESSID PHPSESSID session.referer_check no value no value session.save_handler files files session.save_path /home/dfwit/tmp /home/dfwit/tmp session.serialize_handler php php session.upload_progress.cleanup On On session.upload_progress.enabled On On session.upload_progress.freq 1% 1% session.upload_progress.min_freq 1 1 session.upload_progress.name PHP_SESSION_UPLOAD_PROGRESS PHP_SESSION_UPLOAD_PROGRESS session.upload_progress.prefix upload_progress_ upload_progress_ session.use_cookies On On session.use_only_cookies On On session.use_trans_sid 0 0 See anything out of whack?
  25. To allow a space in the code I provided you: <?php if ($_SERVER["REQUEST_METHOD"] == "POST") { // First brace if (empty($_POST["uname"])) { $unameErr = "* Username is required"; } else { $uname = test_input($_POST["uname"]); if (!preg_match("/^[a-zA-Z0-9]*$/",$uname)) { $unameErr = "* Letters and numerals only"; } } } // Last brace ?> do this if (!preg_match("/^[a-zA-Z0-9\s]*$/",$name)) { Just add the '\s' Simple as that.
×
×
  • 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.