mfredy92;11039043 wrote:oh yes, I surely know how to use $GET arrays, because I use that to display the products in the basket itself. But I am just not sure why would u use $GET to add products to the database?
Because you are passing the user's choices to your script in the query string.
mfredy92;11039043 wrote:Before, I was adding products tot he database. I used URL to display products that the user selected. but my mentor, said that is is very insecure as anyone can really change the data.
This insecurity will always exist; which is why you should be validating the data before you store it in your DB.
Take my earlier example of the Price: say you give the user a link like so:
<a href="?dvdID=1&name=The%20Awesomest%20Movie%20Ever&price=100">Buy <i>The Awesomest Movie Ever</i> for $100</a>
What if, instead of clicking on that link, the user typed the URL into their address bar instead—but made a little change first?
http://your-website.example.com/your/script.php?dvdID=1&name=The%20Awesomest%20Movie%20Ever&price=0
Since you don't validate the input, the record you insert into your DB will have a price of $0. YES! Free movies!
This is possible even if you use forms (GET or POST). You must validate the user's input. You must Never Trust User Input.
Take this a step further, though. Out of all that information, which part is the only thing that you don't already know? You already know which Title goes with which ID. You already know how much it costs. The only thing you don't know is which one the user will choose. So, there's no need to ask them what the title is or how much it costs: only ask them which one they want.
<a href=?dvdID=1>Buy <i>The Awesomest Movie Ever</i> for $100</a>
When you get that dvd ID number, you're all set: insert a record with the dvd ID, the id of the user who wants it, and the sale price.
You'll probably need to re-organize the way your tables are designed, however. Maybe something like:
[COLOR="#A9A9A9"]-- available DVDs + regular price[/COLOR]
create table dvd(
id serial primary key,
name varchar(100) not null,
price decimal(5,2) not null
)engine=innodb;
[COLOR="#A9A9A9"]-- DVDs that a user ordered + final sale price[/COLOR]
create table dvd_cart(
dvd_id bigint unsigned not null,
user_id [COLOR="#A9A9A9"]-- (whatever data type your user table primary key is)[/COLOR]
price decimal(5,2) not null,
[COLOR="#A9A9A9"]-- this foreign key ensures all items in the cart belong to an existing user[/COLOR]
foreign key(user_id) references user(id),
[COLOR="#A9A9A9"]-- this foreign key ensures you cannot buy a dvd that does not exist[/COLOR]
foreign key(dvd_id) references dvd(id)
)engine=innodb;