Log in

View Full Version : Create a shopping cart and a log book in php with mysql


memory
Jun 11, 2007, 05:33 AM
Please help!


I am in a dilema. I have just started learning php; I am developing a website for an IT firm and I nid to add a shopping cart and a log book but I am not sure how. I have never done that in any other language, so am so blank. I can do the tables in mysql, but how do I link it with php?

What I really need is at least a simple but working script/ code which I can understand.

I will really appreciate.

retsoksirhc
Aug 7, 2007, 07:27 AM
I prefer to use arrays to interact with a mysql database from PHP.

Once you have the tables created, you can do all the logging and editing from the php script


<?php
//First you need to connect
mysql_connect("hostname","username","password");

//Then, select the database you want to use
mysql_select_db("mydatabasename");

//Then the queries. Here's one to add
mysql_query("INSERT INTO tablename ('fieldname1','fieldname2') VALUES ('value1','value2')");

//Or you can gather information. This gets a bit tricky
$query=mysql_query("SELECT * FROM tablename");
while($row=mysql_fetch_row($mysql_query)){
echo($row['fieldname1'] . " is the value of field1. The value of field2 is " . $row['fieldname2'] . "<br>");
}
?>

In that last part, we use the variable $query to store out query, and then we use the while loop to get each row that coem out of the query. This works because we use while($row=mysql_query($query)), and as long as there is more information we haven't seen yet in the query, it returns a value. Once we've gotten all the information, the mysql_fetch_row function will return FALSE, and the while loop will end. So, getting each row one at a time in a loop, we can do whatever we want with the data. Whatever the names of your fields are, you can use in the $row array.

vingogly
Oct 31, 2007, 10:49 AM
There's a full-blown shopping cart example with all the code needed in PHP and MySQL Web Development (http://www.amazon.com/PHP-MySQL-Development-Developers-Library/dp/0672329166/ref=sr_1_6/104-4315318-2167145?ie=UTF8&s=books&qid=1193852244&sr=1-6). Don't recall if there's anything in there like a logbook, but if you can master the shopping cart I suspect the logbook will be a piece of cake. :-)

Vasily

interinfinity
Jan 4, 2008, 02:46 PM
Go to barnes and noble. They have entire books full of ecommerce applications for php. I just recently bought 2 myself. Also Google search open source versions for php shopping carts, or entire ecommerce solutions. You can get a lot of ideas and tweak code to be custom for the site you are building