I recently bought a PHP/MySQL book that has code for setting up an online store. The first PHP/MySQL program created the database and the tables, and worked successfully. The second program adds data, and it is the one I am having a problem with. You'd think it would work just fine since it came out of a book by expert authors, but it didn't work yet. The part of the code that creates the database and tables is as follows:
<?php
require 'db.inc.php';
$db = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD) or
die ('Unable to connect. Check your connection parameters.');
mysql_select_db(MYSQL_DB, $db) or die(mysql_error($db));
// Create the main database if it doesn't already exist
$query = 'CREATE DATABASE IF NOT EXISTS DonsStore';
mysql_query($query, $db) or die(mysql_error($db));
// Make sure database above is the active one
mysql_select_db('DonsStore', $db) or die(mysql_error($db));
// Create the Products table
$query = 'CREATE TABLE IF NOT EXISTS products (
prodcode CHAR(5) NOT NULL,
prodtype VARCHAR(25) NOT NULL,
prodname VARCHAR(100) NOT NULL,
descrip VARCHAR(100) NOT NULL,
price DEC(6,2) NOT NULL,
PRIMARY KEY (prodcode)
)
ENGINE=InnoDB';
mysql_query($query, $db) or die (mysql_error($db));
// Create the Customers table
$query = 'CREATE TABLE IF NOT EXISTS customers (
custnbr INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
firstname VARCHAR(25) NOT NULL,
lastname VARCHAR(25) NOT NULL,
address1 VARCHAR(50) NOT NULL,
address2 VARCHAR(50),
city VARCHAR(25) NOT NULL,
state CHAR(2) NOT NULL,
zipcode CHAR(5) NOT NULL,
phone CHAR(12),
email VARCHAR(100),
PRIMARY KEY (custnbr)
)
ENGINE=InnoDB';
mysql_query($query, $db) or die (mysql_error($db));
// Create the Orders table
$query = 'CREATE TABLE IF NOT EXISTS orders (
ordernbr INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
orderdate DATE NOT NULL,
custnbr INTEGER UNSIGNED NOT NULL,
saleamt DEC(9,2) NOT NULL,
shipamt DEC(8,2) NOT NULL,
taxamt DEC(8,2) NOT NULL,
totalamt DEC(9,2) NOT NULL,
shipfname VARCHAR(25) NOT NULL,
shiplname VARCHAR(25) NOT NULL,
shipaddr1 VARCHAR(50) NOT NULL,
shipaddr2 VARCHAR(50),
shipcity VARCHAR(25) NOT NULL,
shipstate CHAR(2) NOT NULL,
shipzip CHAR(5) NOT NULL,
shipphone CHAR(12),
shipemail VARCHAR(100),
PRIMARY KEY (ordernbr),
FOREIGN KEY (custnbr) REFERENCES customer(custnbr)
)
ENGINE=InnoDB';
mysql_query($query, $db) or die (mysql_error($db));
// Create the Order Details table
$query = 'CREATE TABLE IF NOT EXISTS order_details (
ordernbr INTEGER UNSIGNED NOT NULL,
orderqty INTEGER UNSIGNED NOT NULL,
prodcode CHAR(5) NOT NULL,
FOREIGN KEY (ordernbr) REFERENCES orders(ordernbr),
FOREIGN KEY (prodcode) REFERENCES products(prodcode)
)
ENGINE=InnoDB';
mysql_query($query, $db) or die (mysql_error($db));
// Create the Cart table
$query = 'CREATE TABLE IF NOT EXISTS cart (
session CHAR(50) NOT NULL,
prodcode CHAR(5) NOT NULL,
qty INTEGER UNSIGNED NOT NULL,
PRIMARY KEY (session, prodcode),
FOREIGN KEY (prodcode) REFERENCES products(prodcode)
)
ENGINE=InnoDB';
mysql_query($query, $db) or die (mysql_error($db));
echo 'DonsStore Database and Tables Creation successfully completed!';
?>
The program that adds data follows.
<?php
require 'db.inc.php';
$db = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD) or
die ('Unable to connect. Check your connection parameters.');
mysql_select_db(MYSQL_DB, $db) or die(mysql_error($db));
$query = 'INSERT INTO products
(prodcode, prodtype, prodname, descrip, price)
VALUES
("00001",
"Poetry Book",
"Clarity Of The Heart",
"Spiritually motivational poetry and prose",
14.95)';
mysql_query($query, $db) or die(mysql_error($db));
echo 'Products data entered successfully!';
?>
As you can see, I am trying to add only one record now since I have only one book ready. I am working on two other books when I have time, so there will be more data to add later. The error message I get is 'Unknown column 'prodname' in 'field list''. I have checked and double-checked the spelling of my field names, and they are correct. There must be something happening that I am not aware of. I would very much appreciate help that would resolve this problem. Thanks.
Don Glass