Jump to content

Archived

This topic is now archived and is closed to further replies.

matthew61773

php MySQL help

Recommended Posts

question I have been set and am having triouble with!

Write a PHP script, including the approriate SQL, that shows how to read data from and write data to the following MySQL database table

(PRODUCT)

Prod-Id            Product      Price      No_in_stock
    0                  Hammer      £5.00            22
    1                  Pump          £3.00            4   
    3                  Spanner      £2.00            6

Share this post


Link to post
Share on other sites
I got just the site for you [url=http://www.php-mysql-tutorial.com/]php-mysql-tutorial.com/[/url]. Go through tutorial numbers 4 through to 7. Go through number 3 first for the SQL basics.

What you learn there should help you complete you mini assignment.

Share this post


Link to post
Share on other sites
I am too helpful for this. Only this time: here is your code sample. Do the error checking yourself.
[code]<?php
// Setup your article array, you can do this a zillion different ways
$a = array();
$a[0]['id']    = 0;
$a[0]['prod']  = 'Hammer';
$a[0]['price'] = 5.00;
$a[0]['no']    = 22;
$a[1]['id']    = 1;
$a[1]['prod']  = 'Pump';
$a[1]['price'] = 3.00;
$a[1]['no']    = 4;
$a[2]['id']    = 3;
$a[2]['prod']  = 'Spanner';
$a[2]['price'] = 2.00;
$a[2]['no']    = 6;

// setup connection to db
$conn = mysql_connect("localhost", "ronverdonk", "ronnie09") or die(mysql_error());
mysql_select_db("vwso") or die(mysql_error());

// execute the CREATE TABLE statement
$create = "CREATE TABLE IF NOT EXISTS myproducts (
                          id          INT PRIMARY KEY AUTO_INCREMENT NOT NULL,
                          prod_id    INT NOT NULL,
                          product    VARCHAR(64) NOT NULL,
                          price      DECIMAL(5,2) NOT NULL,
                          no_in_stock INT NOT NULL)";
$res = mysql_query($create) or die(mysql_error());

// insert each array entry into the table
for ($i=0; $i < count($a); $i++) {
  $var1 = $a[$i]['id'];
  $var2 = $a[$i]['prod'];
  $var3 = $a[$i]['price'];
  $var4 = $a[$i]['no'];
  $insert = "INSERT INTO myproducts (prod_id, product, price, no_in_stock) VALUES (
                  $var1, '$var2', $var3, $var4)";
  $res = mysql_query($insert) or die(mysql_error());
}

// SELECT all rows from table
$b = array();
$select = "SELECT prod_id, product, price, no_in_stock FROM myproducts";
$res = mysql_query($select) or die(mysql_error());
// retrieve all rows from SELECT result and store in array
$i=0;
while ($row = mysql_fetch_assoc($res)) {
  $b[$i]['id']    = $row['prod_id'];
  $b[$i]['prod']  = $row['product'];
  $b[$i]['price'] = $row['price'];
  $b[$i]['no']    = $row['no_in_stock'];
  $i++;
}
print '<pre>';
print_r($b);
?>[/code]

Ronald  8)

Share this post


Link to post
Share on other sites

×

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.