Jump to content

php MySQL help


matthew61773

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
Link to comment
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.
Link to comment
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)
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.