0

hi every! im a newbie and im stocked with some edit codes,. can u make me a code on how to edit records with images? i really need it now. If u can, then it is very much appreciated.

here is my database and table

CREATE DATABASE db1;

CREATE TABLE IF NOT EXISTS tb_product; 
(

  product_id int(20) NOT NULL AUTO_INCREMENT,
  product_name varchar(50) NOT NULL,
  product_image varchar(50) NOT NULL,
  product_desc varchar(50) NOT NULL,
  product_price int(20) NOT NULL,
  product_quantity int(20) NOT NULL,
  product_status enum('Available','Not Available') NOT NULL DEFAULT 'Available',
  date_added timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  category_id int(20) NOT NULL,
  PRIMARY KEY (product_id),
  KEY category_id (category_id)
) ENGINE=MyISAM;

------------------here are my form for adding the products;---------
        <?php
        if(isset($_GET['err2']))
        {
            if($_GET['err2']==1)
                echo '<div class="error"><br/><p><font color=red>Please enter data in all fields.</div></font>';
            else ($_GET['err2']==3)
                echo '<div class="error"><br/><p><font color=red>Book already exists!</div></font>';
        }
        ?>
<form enctype="multipart/form-data" action="addproducts2.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
<p>Image: <input name="add" type="file" /><br />
</p>                    
<p>Name:<input type="text" name="product_name" value="" /></p>           
<p>Description:<input type="text" name="product_desc" value="" /></p>
<p>Price:<input type="text" name="product_price" value="" /></p>
<p>Quantity:<input type="text" name="product_quantity" value="" /></p>
<p>Status:<td><select type= "product_status" name="product_status" value="product_status">
<option selected="selected"></option>
<option name= "product_status" value="Available">Available</option>
<option name= "product_status" value="Not Available">Not Available</option></p></select>                
<p>Cat ID:<td><select type="category_id" name="category_id" value="category_id" />
<option selected="selected"></option>
<option name= "category_id" value="2">Romance</option>
<option name= "category_id" value="3">Sci-Fi</option>
<option name= "category_id" value="4">Comedy</option>
<option name= "category_id" value="5">Horror</option></p></select>            
<p><input type="submit" value="Add" name="add" class="submit></p> 
</form>

----------------here is the code for adding the records;-----------------
addproducts.php

<?php
    //Connect and select a datbase
    mysql_connect("localhost", "root" , "" )or die("cannot connect to database server");
    mysql_select_db("db1")or die("cannot select the database");

    //if form is submitted
    if(isset($_POST['add']))
    {  
        //check if every fields are entered
        if(!$_POST['product_name'] | !$_POST['product_desc'] | !$_POST['product_price'] | !$_POST['product_quantity'] | !$_POST['product_status'] | !$_POST['category_id'])
        {
            header("location:addproducts.php?err2=1");
        }
        else
        {
$product_name=$_POST['product_name'];
$product_desc=$_POST['product_desc'];
$product_price=$_POST['product_price'];
$product_quantity=$_POST['product_quantity'];
$product_status=$_POST['product_status'];
$category_id=$_POST['category_id'];

            //query to know whether a username already exists
            $sql = "SELECT product_name FROM tb_product WHERE product_name='".$product_name."'";
            $resource = mysql_query($sql) or die("username check error");
            $check = mysql_num_rows($resource);
            if($check == 1)
            {
                header("location:addproducts.php?err2=3");
            }
            else
            {     
$target_path = "images/";
$target_path = $target_path . basename( $_FILES['add']['name']); 

if(move_uploaded_file($_FILES['add']['tmp_name'], $target_path)) 
{
//echo "The file ".  basename($_FILES['add']['name'])." has been uploaded";
    //$sql="INSERT INTO tb_product VALUES ('','$product_name', '$target_path', '$product_desc', '$product_price', '$product_quantity', '$product_status', 'now()', '$category_id')";
    $sql="INSERT INTO tb_product (product_name, product_image, product_desc, product_price, product_quantity, product_status, category_id)VALUES('$product_name', '$target_path','$product_desc', '$product_price', '$product_quantity', '$product_status', '$category_id')";
    $result=mysql_query($sql);
header ('Location: viewproducts.php');


}
else
//{
    //echo "There was an error uploading the file, please try again!";
//}
//else
{
$target_path = "images/noimage.jpg";
}
}
}
}
?>

-----------------please, i really need the codes--------------

Edited by Reverend Jim: Fixed formatting

3
Contributors
7
Replies
8
Views
6 Years
Discussion Span
Last Post by smantscheff
0

im already done with the add products script,. what i wanted now is to make a code which i can be able to edit those records that i added including the image.

0

the image is already shown when i view it from my viewproducts script, the problem is how i could modify the records including the image. how could i update it?

0

i tried it though but it didnt, i thought ive got a problem with the target_path. what do you think?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.