Jump to content

man5

Members
  • Posts

    173
  • Joined

  • Last visited

Everything posted by man5

  1. Nope, I know how to add and retrive posts from mysql table. It's just that I am having hard time getting data from multiple joined tables. Say I have post#1 inserted in favorites table. I can retrive the post by using user_id or post_id, how ever it won't give me the actual post which consists of title and post desc. I am assuming foreign keys are created(unless defined as primary key) automatically when creating table in mysql. And it seems like foreign keys don't work on Myisam table, only Innodb. However, my posts table is Myisam. Main reason is because of FULL-TEXT search.
  2. It would really help to have an outside view to what I am trying to do. I have 1 users table. 1 posts table and 1 favorites table. Basically what I am trying to do is give a user an option to add other users' posts to their favorites folder. Say these are the options for each table in mysql. users:: - id - username posts:: - post_id - user_id - title - post favorites:: - favorite_id - user_id - post_id Now I know I can use joins to join these tables together. I have tried them for other things and they work. It's just that with this particular case, I am having some issues getting the query right. This is my query. Please point out what's wrong with it and how to do it correctly. $stmt = $dbh->prepare("SELECT users.*, posts.*, favorites.* FROM posts LEFT JOIN users ON records.user_id = users.id LEFT JOIN favorites ON posts.user_id = favorites.id WHERE posts.user_id = {$userid} ORDER BY posted DESC LIMIT 4"); ps. $userid is definied outside of this query, so don't worry about it.
  3. Simply brilliant!!! You have done it once again Ch0cu3r. Thank you so much!!!! It works like charm.
  4. Oh I see. So I insert it as a normal timestamp as you put but when I query to get the date from the db, that's when I change it to facbook format? So say this is my get query. $get = mysql_query("SELECT * FROM posts"); while ($row = mysql_fetch_assoc($get)) { echo $row['posted']; // this returns the date in this format ("Y-m-d H:i:s") } How I convert it to facebook like date?
  5. I actually did change it to match the timestamp. This is what normal timestamp looks like. date('Y-m-d H:i:s') and this is mine. $mydate = '$years | $months | $days | $hours | $minutes | $seconds'; Still no difference. The form is submiting correctly because I have other inputs that are being insert into the table as well. It's just the $mydate is not being inserted correctly.
  6. UPDATE I had to change $post to $_POST. It works now. The only thing is the new date isn't shown in db. It's listed blank like this 0000-00-00 00:00:00. The 'posted' column which is set to timestamp. Do I need to modify it it in anyway to show the new type of formating?
  7. Here is the full code. I still get the error that $createdday var is undefined. $today = time(); $createdday= strtotime($post['created']); //mysql timestamp of when post was created $datediff = abs($today - $createdday); $difftext=""; $years = floor($datediff / (365*60*60*24)); $months = floor(($datediff - $years * 365*60*60*24) / (30*60*60*24)); $days = floor(($datediff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24)); $hours= floor($datediff/3600); $minutes= floor($datediff/60); $seconds= floor($datediff); //year checker if($difftext=="") { if($years>1) $difftext=$years." years ago"; elseif($years==1) $difftext=$years." year ago"; } //month checker if($difftext=="") { if($months>1) $difftext=$months." months ago"; elseif($months==1) $difftext=$months." month ago"; } //month checker if($difftext=="") { if($days>1) $difftext=$days." days ago"; elseif($days==1) $difftext=$days." day ago"; } //hour checker if($difftext=="") { if($hours>1) $difftext=$hours." hours ago"; elseif($hours==1) $difftext=$hours." hour ago"; } //minutes checker if($difftext=="") { if($minutes>1) $difftext=$minutes." minutes ago"; elseif($minutes==1) $difftext=$minutes." minute ago"; } //seconds checker if($difftext=="") { if($seconds>1) $difftext=$seconds." seconds ago"; elseif($seconds==1) $difftext=$seconds." second ago"; } echo " | ".$difftext; $mydate = "$seconds | $minutes | $hours | $days | $months | $years"; if(empty($post['created'])) { echo 'Please fill in the field'; } else { comment = $_POST['created']; $insert = mysql_query("INSERT INTO posts (comment, posted) VALUES($comment, $mydate)"); } <form action="" method="post" > Enter your comment<br> <input type="text" name="created" > <input type="submit" name="submit" value="upload"> </form>
  8. Actually you are right. I understand your code now. Now I still get an error but it's because the following variable isn't defined. $createdday= strtotime($post['created']); where do I define the $post field? Is it the name of the submit input or the comment input? Below is the form. <form action="" method="post" > Enter your comment<br> <input type="text" name="created"> <input type="submit" value="Post"> </form>
  9. Things are still little hazy. Let me try it my method based on yours. $Mytime = '$createdday | $datediff | $difftext | seconds | $minutes | $days | $hours | $months | $years'; $today = time($Mytime); //pardon the mysql_* query $insert = mysql_query("INSERT INTO posts (created) VALUES($today)"); also, I am a little lost on this variable. What's it for? $createdday= strtotime($post['created']);
  10. This code is taken from http://itfeast.blogspot.in/2013/08/php-convert-timestamp-into-facebook.html How would you actually show this on a page? Say I have a mysql table with date column and I would like to insert the results of this code in there and as well as show it on page, how would that be done? $today = time(); $createdday= strtotime($post['created']); //mysql timestamp of when post was created $datediff = abs($today - $createdday); $difftext=""; $years = floor($datediff / (365*60*60*24)); $months = floor(($datediff - $years * 365*60*60*24) / (30*60*60*24)); $days = floor(($datediff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24)); $hours= floor($datediff/3600); $minutes= floor($datediff/60); $seconds= floor($datediff); //year checker if($difftext=="") { if($years>1) $difftext=$years." years ago"; elseif($years==1) $difftext=$years." year ago"; } //month checker if($difftext=="") { if($months>1) $difftext=$months." months ago"; elseif($months==1) $difftext=$months." month ago"; } //month checker if($difftext=="") { if($days>1) $difftext=$days." days ago"; elseif($days==1) $difftext=$days." day ago"; } //hour checker if($difftext=="") { if($hours>1) $difftext=$hours." hours ago"; elseif($hours==1) $difftext=$hours." hour ago"; } //minutes checker if($difftext=="") { if($minutes>1) $difftext=$minutes." minutes ago"; elseif($minutes==1) $difftext=$minutes." minute ago"; } //seconds checker if($difftext=="") { if($seconds>1) $difftext=$seconds." seconds ago"; elseif($seconds==1) $difftext=$seconds." second ago"; } echo " | ".$difftext;
  11. Yes I came to that conclusion as well(after 2 days of sweat and tears). So I decided to stop beating the dying horse. I have finally found a solution. Works beautifully. Instead of trying to change the user's password mysql, I decided to give the user an option to update their own password. Thanks for all your help guys. I needed it to find the solution.
  12. Yes you are correct that the emails should be unique. However we need to get the $full_name variable to send in email. So we require the use of foreach to get that from DB. Below is the updated code. I've been at it all day and still can't fix it. As far as I can tell, the emailed password does not match with the stored password when i try to log in, despite the fact they are the same. <?php require_once 'core/init.php'; ?> <!DOCTYPE HTML> <html lang="en"> <head> </head> <body><?php //Check if the form was submitted if(isset($_POST['email'])) { //Trim the post value and verify it is not empty $postEmail = Input::get('email'); if(empty($postEmail)) { //Email was empty echo 'please enter email'; } else { //Run select query using email $emailSQL = escape($postEmail); $getData = DB::getInstance()->query("SELECT * FROM users WHERE email='{$emailSQL}'"); //Check if query passed if(!$getData) { //Query failed echo 'select query failed'; } elseif(!$getData->count()) { //No records returned echo 'email not found'; } else { foreach($getData->results() as $row) { $full_name = $row->full_name; } $salt = Hash::salt(32); $new_password = Hash::make(rand(999, 999999),$salt); $temp_pass = substr($new_password, 0, ; $updatePass = DB::getInstance()->query("UPDATE users SET password = '{$new_password}', salt = '{$salt}' WHERE email='{$postEmail}'"); //Check if query passed if(!$updatePass) { //Query failed echo 'updated query failed'; } else { //Send confirmation email $to = $postEmail; $subject = "Password Reset"; $body = "Hello {$full_name},\n\nYour new password is: {$temp_pass}\n\n-mycomp"; if(!mail($to, $subject, $body)) { echo 'error sending email'; } else { echo 'an email has been sent'; } } } } } ?> <form action="" method="post"> <span>Enter your email address:<span><br> <input type="text" name="email" size="40"><br> <input type="submit" value="Recover"> </form> </body> </html>
  13. It'll probably help to see what my login function looks like. public function login($email = null, $password = null, $remember = false) { if(!$email && !$password && $this->exists()) { Session::put($this->_sessionName, $this->data()->id); } else { $user = $this->find($email); if($user) { if($this->data()->password === Hash::make($password, $this->data()->salt)) { Session::put($this->_sessionName, $this->data()->id); if($remember) { $hash = Hash::unique(); $hashCheck = $this->_db->get('users_session', array('user_id', '=', $this->data()->id)); if(!$hashCheck->count()) { $this->_db->insert('users_session', array( 'user_id' => $this->data()->id, 'hash' => $hash )); } else { $hash = $hashCheck->first()->hash; } Cookie::put($this->_cookieName, $hash, Config::get('remember/cookie_expiry')); } return true; } } } return false; } my login.php page if(Input::exists()) { if(Token::check(Input::get('token'))) { $validate = new Validate(); $validation = $validate->check($_POST, array( 'email' => array('required' => true), 'password' => array('required' => true) )); if ($validation->passed()) { $user = new User(); $remember = (Input::get('remember') === 'on') ? true : false; $login = $user->login(Input::get('email'), Input::get('password'), $remember); if($login) { Redirect::to('index.php'); } else { echo '<p>Sorry, login failed.</p>'; } } else { foreach($validation->errors() as $error) { echo $error, '<br>'; } } } } ?> <form action="" method="post"> <div class="field"> <label for="email">Email</label> <input type="text" name="email" autocomplete="off"> </div> <div class="field"> <label for="password">Password</label> <input type="password" name="password" autocomplete="off"> </div> <div class="field"> <label for="remember"> <input type="checkbox" name="remember" id="remember" autocomplete="off">Remember me </label> </div> <input type="hidden" name="token" value="<?php echo Token::generate(); ?>"> <input type="submit" value="Log in"> <div class="forgot_password"> <span><a href="reset_password.php">Forgot password?</a></span><br> </div> </form>
  14. Update again. So i got it working. I get the substring code sent to my email, while hashed and salted password is updated in the DB. Here is the code. $email_password = substr(Hash::make(rand(999, 999999),$salt), 0, ; $new_password = Hash::make(rand(999, 999999),$salt); $updatePass = DB::getInstance()->query("UPDATE users SET password = '{$new_password}', salt = '{$salt}' WHERE email='{$email}'"); Now the only problem left is that it won't log me in with the new code. Granted I can't login with the old pass, which is good.
  15. Update. I got it to update the database with the new password(the email matching was incorrect). Now when it updatest he database with the new password, it inputs the same string as as the one emailed. For eg. 0a6564e8. It should be showing the encrypted version of the password in the database. The passwords in the DB are encrypted with hash and salt functions, which are this. public static function make($string, $salt = '') { return hash('sha256', $string . $salt); } public static function salt($length) { return mcrypt_create_iv($length); } If I am correct with all of the above. Then I have to get the following variable right. $salt = Hash::salt(32); $new_password = substr(Hash::make(rand(999, 999999)), 0, 8, $salt));
  16. That's great. It makes it clear now. The only thing you are missing in the above code is foreach statement to get the variables. I did that and it's good now. It does email the new pass code. Now on to a next issue. I am unable to login with the new pass code. Also, it doesn't update the password in the database. I am assuming that is done after I login with the new pass code? I would like to mention that the original password created from registration is done with hash and salt. In the DB, there are two columns; Password column a04eff113e34a39fab1410c22d0b4061ffe300dbd3d7244a8d Salt column –°Ò'{èUQð¾~îþhâLY§Ò÷×ÍB9ù‡\
  17. That's the weird thing. Nothing happens. No errors show up. It doesn't process. As for the input type. It is actually available in HTML5.
  18. I am trying to create a forget password option. For some reason, it's not working. Please check the code below to see what is wrong with it. if(isset($_POST['submit'])) { if(!empty($_POST['email'])) { $email = escape($_POST['email']); $getData = DB::getInstance()->query("SELECT * FROM users WHERE email = {$email}"); if($getData->count()) { foreach($getData->results() as $row) { $username = escape($row->username); $full_name = escape($row->full_name); $password = escape($row->password); $db_email = escape($row->email); } } else { echo 'No data returned!'; } if($email == $db_email) { $new_password = substr(md5(rand(999, 999999)), 0, ; $to = $db_email; $subject = "Password Reset"; $body = "Hello " . $full_name . ",\n\nYour new password is: " . $new_password . "\n\n-Favecave"; $updatePass = DB::getInstance()->query("UPDATE users SET password = {$new_password} WHERE email= {$email}"); mail($to,$subject,$body); } else { echo 'Email not found'; } } else { echo 'Please enter field'; } } ?> <form action="" method="post"> <span>Enter your email address:<span><br> <input type="email" name="email" size="40"><br> <input type="submit" value="Recover"> </form>
  19. sorry for the late reply. I tried to figure this out on my own, so far so luck. The urls I have are actually directory based. Like this. <a href="posts.php?title=my post title here">Post one</a> Can you please show me the rewrite code that outputs the above to what you mentioned? And here is my .htaccess file. RewriteEngine On RewriteCond %{REQUEST_FILENAME} -f [OR] RewriteCond %{REQUEST_FILENAME} -d RewriteRule .* - [L] Thanks.
  20. Alright so I am trying to modify the url with mod_rewrite in .htaccess. Here is an example. Tell me if it's wrong. localhost/demo/post.php?title=my post title here RewriteRule ^post/([^/\.]+)/?$ post.php?title=$1 [L] If it's correct, it doesn't seem to work.
  21. Great. That's what I was looking for. Thank you!
  22. I have question regarding the setup of url for multiple pages. For eg. How does one have a setup like this? http://www.groupon.ca/deals/gatineau_en/Antirouille-Metropolitain/35853918 The setup I have is like this. //Page 1 with posts shown in a specific catagory. www.mysite.ca/posts_cat.php?catagoryId=1 //Page 2 with a single post shown. www.mysite.ca/posts.php?title=this is my post As you can see, my url setup is quite different than Groupon's. I am not sure if it's the correct way to do it; but it does work. The reason I have two different pages for posts are because I have two different css designs. I suppose my question is, is my method ok? And how do I make it so it looks like Groupon's way?
  23. I think my question was confusing. But I have a solution. I had to to set $_SESSION variable to get an idea of a different page. It works.
  24. Say my goal is to have posts with user comments with each individual post. And these posts are posted in relavent catagories. This is all done. Works beautifully. Now say my 2nd goal is to use ajax for everytime a user posts a comment in a post, it'll load the comment without refreshing the page. This is where I am having an issue. So say this is my setup. index.php <a href="records.php?title=<?php echo $record_id; ?>"> records.php <head> script type="text/javascript"> $(document).ready(function(){ $(".comment_button").click(function() { var element = $(this); var test = $("#content").val(); var dataString = 'content='+ test; if(test=='') { alert("Please Enter Some Text"); } else { $.ajax({ type: "POST", url: "ajax.php", data: dataString, cache: false, success: function(data){ $('#display').html(data); document.getElementById('content').value=''; } }); } return false; }); }); </script> </head> <body> <form method="post" name="form" action=""> <textarea name="content" id="content" cols="45" rows="5"></textarea> <input type="submit" value="Update" id="v" name="submit" class="comment_button"/> </form> <div id="display" align="left"> <div> </body> ajax.php // need to get id of current post here. Of course this is not the complete code but you get the idea. The queries that insert and the get the records work. The only issue I have is that the 'get' query won't get the current post id. It makes sense since it's in ajax.php and it's processing through the ajax function. If that query was on records.php, yes it will work. So my question is, is there a way around it?
  25. Never mind, I got it working by using get current page function. All good now.
×
×
  • 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.