I'm having some problems understanding the connection to the database in CakePHP.
I have a table called user (which was taken from the CakePHP tutorial website) I added another row to the table called "bio" I am able to insert text into the bio but i'm not able to retrieve it.
I've managed to retrieve all the other rows by:
$name = CakeSession::read("Auth.User.username");
$mail = CakeSession::read("Auth.User.email");
$created = CakeSession::read("Auth.User.created");
$id = CakeSession::read("Auth.User.id");
but for some reason it won't work with the bio.
Do I need to add anything to controllers to be able to this? Or is it a variable that I need to set?
Thank you!
As #sgt BOSE said. Using CakeSession::read("Auth.User") you will get session data of your current logged user.
$current_user_data = $this->User->FindById(CakeSession::read("Auth.User.id"));
This will return current (logged) user data from database.
Related
I have a coins table in phpMyAdmin and I need it to be automatically updated when a user completes a purchase via PayPal. The way it is setup is that PayPal will automatically redirect them to a link I choose.
So, how can I run a script that takes the parameters from the URL and adds a new record to the table?
Thank you for all your help.
So that link would need to be a page called something like payment_success.php
On that page you would need to have a script that looks like this:
<?php
include 'credentials.php'; /*this is a file that will contain your database connection info
Then here you will establish the connection and enter the data into the desired table*/
$var1 = 'some information collected from paypal that you want to update';
$id = 'some identifiable information about your user that can connect them to the correct row in your table';
I won't hold your hand through all this but the SQL query you would need would be:
UPDATE table_name SET column_1 = '$var1' WHERE ID = '$id'
Set the confirmation PayPal link to increment the number of coins value in the MySQL table for the particular user....!!!
I hope that will work out..... If I could get a idea of your table or some more info, I could give you a clear idea about that.
I am trying to finish this website I am currently creating, but I am kind of stuck.
I want to create a table called "orders" in my DB. I want this table to be related to my users table so that when the user goes to his "orders.php" page (once logged in already) he sees all his current and previous orders.
These would be my table fields/cols:
id
username
ordernumber
description
quantity
total
This is my approach:
Whenever a new order is created, insert all the table fields/cols depending on the user's choice (selected stuff for the order), but the username would be the only value gathered from a $_SESSION or $_COOKIE variable, which holds the username. Then, once the user goes to orders.php, I will execute a query to show all the orders that only that username has ordered. Please note that I do sanitize all my input/output and I do not store sensitive data in my cookies. My system is designed so it only uses the session as the method of authentication, therefore you need to login every time you close the browser but that is fine.
1) Is this a safe approach? Do you have any suggestions/comments?
2) Could you help me construct the query?
I haven't really worked with relational databases, so I am kind of lost. How can I call all the orders from table "orders" where username = "username from the session"?
So far I have this:
"SELECT * FROM orders WHERE username = ? " //(Using PDO)
I know that this will work but my concern is in case of getting a session hijacked or something like that, then a user would be able to retrieve any users' orders, or not?
Thank you for explaining this a little bit further and helping me out!
Cheers!
Be careful! Please don't create a plain text cookie containing a human-readable user id (like user2345995 or OllieJones). It's far too easy for a badguy to fake a cookie like that just by guessing, and then your users' information leaks out.
You're working in php. Therefore you can use php's session mechanism to store your userid and other values. php uses hard-to-guess session ids (SIDs) and stores them in either a cookie or as a sid=1234abcd9875 parameter in URLs.
For the sake of your system's integrity, please read up on this. It's actually a pretty well-designed feature and it's been in the wild for fifteen years or so: it's debugged.
http://php.net/manual/en/session.idpassing.php
If you're using the session system, you basically do this in your first page, your login page.
session_start();
...
$_SESSION['username'] = $username; /* which you get by logging in */
...
On your order lookup page you do something similar to retrieve the username and use it in a query.
session_start();
...
$orderstmt = $pdoconn->prepare("SELECT * FROM orders WHERE username = :username");
$orderstmt->execute( array(':username' => $_SESSION['username']) );
...
while ($row = $orderstmt->fetch()) {
/* use the row's data */
}
$orderstmt->closeCursor();
I'am learning to make a blog in CodeIgniter 2.x . What I want - when user clicks on the link readmore on the blog's page, it will redirect him to the page with corresponding id to show him a full article.
Now, while retrieving a $query->row(); from a database in content_model.php, i want to update a viewed column in database at the same time. So instead of showing him for example 34 views, it will show 35 views, and when someone open the same link it will shows him by one more - 36 views.
What's the best way to do it without making it vulnerable to hackers somehow ?
Thank you in advance for any help.
When you retrieving data from database using CI framework, you check this following:
if ($result_from_get_function->num_rows()>0){
return $this->updateViewColoumnByOne();
}else{
return false;
}
function updateViewColoumnByOne(){
//retrieve number of view in view column, suppose 12.
//plus one with it; 12+1=13
//update it.
//again retrieve the new update number of views
//and return.
}
I think this could help you.
This should be very easy. When someone tries to read a post, just before you retrieve the post information, update the count views in the database. Execute a query that goes something like this
UPDATE `posts` SET `views` = `views` + 1;
After that, simply retrieve the post information just like you normally would, and then show it to the user by outputting it to the browser.
how do i pull data from my database table and put that data into a readonly textinput in a form for a user to see.
<input readonly type="text" name="em1" id="em1"/>
for this specific field i want to pull the data from a column called wmyt000 in the row of the currently logged in user using a $username variable with session that i created to get the current logged in users username in a table called cash_flow_plan
im using mysql and php
i havnt attempted to try anything yet. i was looking into mysql_fetch_array but that wants to display all of the users information for their row.
i havnt attempted to try anything yet.
ya. well, if you had, you'd know that you can do more with it than you think.
if you write a query limiting your results, then you're going to get what you want.
$query = "SELECT wmyt000 FROM cash_flow_plan WHERE username = '$username'"
$row = mysql_fetch_row($query);
print_r($row); // now you can see the structure of your array. access it accordingly.
I have to do a blog assignment. In it I have to update a field when someone adds a post. The field is in users table and its name is no_of_posts. When someone posts something the value of no_of_posts related to the user who is logged in should be retrieved then incremented by 1 and updated back to database.
I am trying the following code but its not solving the problem.
$userid holds the current users id.
$userid = $this->Auth->user('id');
$userpost = $this->User->query("SELECT no_of_posts FROM users WHERE id = '$userid'");
$userpost++;
$this->User->query("UPDATE users SET no_of_posts = '$userpost' WHERE id = '$userid'");
I also tried using find but its of no use.
$userpost = $this->User->find('count', array('conditions'=>array('User.id'=>$userid)));
Thanks for any help.
You'll want to look into counterCache.
As long as you've properly set up the relation between the Post model and the User model (which you should, anyway), Cake keeps track of the post count automatically for you if you just add a few lines of code.
Check this page in the CakePHP Cookbook: http://book.cakephp.org/1.3/en/view/1033/counterCache-Cache-your-count
As for what you're doing wrong in your example: the query call you do returns an array, so it's only logical that your update query fails.
Apart from that, using the query() method really isn't the CakePHP way of doing things. You should really read the Cookbook pages on Models, because now you're not using CakePHP's full power. Find the Cookbook pages I mean here: http://book.cakephp.org/1.3/en/view/1000/Models