-3

The below program is not working.

add_product.php

<?php

    $name=$_REQUEST["txtname"];
    $price=$_REQUEST["txtprice"];
    $category=$_REQUEST["ddlcategory"];
    $weight=$_REQUEST["txtweight"];
    $description=$_REQUEST["txtdescription"];
    $img=$_REQUEST["btnimage"];

    $connect=mysql_connect("localhost","root","","CakeShop") or die(mysql_error());
    echo "Connected..";
    mysql_select_db(CakeShop);

    if(isset($_REQUEST['btnadd']))
    {


  //The if loop gets executed even if the image is selected....!
            if(getimagesize($Files['btnimage']['tmp_name'])==FALSE)
            {
                $message="Please select an image";
                echo "<script type='text/javascript'>alert('$message');</script>";
            }
            else
            {
                    $insert="Insert into tblProduct(p_name,p_price,p_category,p_weight,p_description,p_image) values ('$name','$price','$category','$weight','$description','$img');";
                    mysql_query($insert) or die("Failed to insert data");
                    echo "<h3>Product Details Inserted........</h3>";
            }

        }

        mysql_close();  

    ?>

add_product.html

<html>
<head>
<title>Cake Central</title>
</head>
<body>
  <h1>ADD NEW PRODUCT</h1>
<hr>
<form method="post" action="add_product.php">
<table>
<tr>
<td>Name:</td>
<td><input type="text" name="txtname"></td>
</tr>

<tr>
<td>Price:</td>
<td><input type="text" name="txtprice"></td>
</tr>

<tr>
<td>Category:</td>
<td>
    <select name="ddlcategory">
    <option>Veg</option>
    <option>Non-Veg</option>
    </select>
</td>
</tr>
<tr>
<td>Weight:</td>
<td><input type="text" name="txtweight"></td>
</tr>

<tr>
<td>Description:</td>
<td><textarea rows="2" cols="16" name="txtdescription"></textarea></td>
</tr>

<tr>
<td>Image:</td>
<td><input name="btnimage" type="file" /></td>
</tr>

<tr>
<td><input class="btn" type="submit" name="btnadd" value="&nbsp; Add &nbsp;"></td> 
</tr>
</table>
</form>
</body>
</html>
DirtyBit
  • 16,613
  • 4
  • 34
  • 55
  • Apart from using the _deprecated_ mysql functions. What exactly do you mean by _The below program is not working_? What errors are you getting? – DirtyBit Sep 20 '15 at 07:31
  • deprecated functions in the sense ? I am a beginner in php so i have no idea about the semantics. – Manali Bhavsar Sep 20 '15 at 07:33
  • Andd, this is incorrect `$insert="Insert into tblProduct(p_name,p_price,p_category,p_weight,p_description,p_image) values ('$name','$price','$category','$weight','$description','$img');";` Notice the extra semi-colon before `"` in the end. – DirtyBit Sep 20 '15 at 07:34
  • And the data is not inserted into the db – Manali Bhavsar Sep 20 '15 at 07:34
  • I dont think there is an error in semicolon i have executed a similar insertion program before and the data was inserted – Manali Bhavsar Sep 20 '15 at 07:35
  • There are more than just a couple of detailed answers/articles around on mysqli/PDO such as http://stackoverflow.com/questions/13569/mysqli-or-pdo-what-are-the-pros-and-cons Go through them to know why/how the deprecated mysql functions are _bad_. – DirtyBit Sep 20 '15 at 07:37
  • And And If that is all of your code, which I assume it is. `if(isset($_REQUEST['btnadd'])) {` you are missing the closing `}` bracket. – DirtyBit Sep 20 '15 at 07:41
  • Oh and a missing `enctype="multipart/form-data"` attribute in the form tag. – DirtyBit Sep 20 '15 at 07:47

2 Answers2

0

I have simplified your code to the best of my knowledge. You don't really need two different pages so I have removed the action = "" in the form tag. Please let me know how it goes.

<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}
else
{
    $db_selected = mysql_select_db('CakeShop', $conn);
    if (!$db_selected) 
    {
        die ('Can\'t Select DB : ' . mysql_error());
    }
}
    if(isset($_POST['check']) && $_FILES['file']['size'] > 0)
    {
         $name=$_POST["txtname"];
         $price=$_POST["txtprice"];
         $category=$_POST["ddlcategory"];
         $weight=$_POST["txtweight"];
         $description=$_POST["txtdescription"];


          $tmpName     = $_FILES['file']['tmp_name'];    
          $fp                    = fopen($tmpName, 'r'); 
          $img  = fread($fp, filesize($tmpName)); 
          $img = addslashes($img);
          fclose($fp); 

      if(!get_magic_quotes_gpc())
          {
              $fileName = addslashes($fileName);
          }

          $query = "INSERT INTO tblProduct (p_name,p_price,p_category,p_weight,p_description,p_image)
                        VALUES ('$name','$price','$category','$weight','$description','$img')";


          mysql_query($query) or die('Error, query failed');
          $imgid = mysql_insert_id();  

          echo "<br>Details successfully added to database<br>";
    }
      else 
        die('You have not selected any image');

    ?>

    <html>
    <head>
    <title>Cake Central</title>
    </head>
    <body>
      <h1>ADD NEW PRODUCT</h1>
    <hr>
    <form method="post" action="" enctype="multipart/form-data">
    <table>
    <tr>
    <td>Name:</td>
    <td><input type="text" name="txtname"></td>
    </tr>

    <tr>
    <td>Price:</td>
    <td><input type="text" name="txtprice"></td>
    </tr>

    <tr>
    <td>Category:</td>
    <td>
        <select name="ddlcategory">
        <option>Veg</option>
        <option>Non-Veg</option>
        </select>
    </td>
    </tr>
    <tr>
    <td>Weight:</td>
    <td><input type="text" name="txtweight"></td>
    </tr>

    <tr>
    <td>Description:</td>
    <td><textarea rows="2" cols="16" name="txtdescription"></textarea></td>
    </tr>

    <tr>
    <td>Image:</td>
    <td><input name="btnimage" type="file" /></td>
    </tr>

    <tr>
    <td><input type="hidden" name="check"></td>
    </tr>

    <tr>
    <td><input class="btn" type="submit" name="btnadd" value="&nbsp; Add &nbsp;"></td> 
    </tr>
    </table>
    </form>
    </body>
    </html>

Note: Since you are really new and have no idea about mysqli/PDO, I have suggested a link and you will find alot of other useful articles on this site as well, One of the reason why I did not change it to mysqli/PDO was because I did not want you to get confused. cheers

DirtyBit
  • 16,613
  • 4
  • 34
  • 55
0

Try adding the enctype attribute to the form tag.

<form method="post" action="add_product.php" enctype="multipart/form-data" >
  • Notice: Undefined index: btnimage in E:\xampp\htdocs\CAKE SHOP\add_product.php on line 7 Notice: Undefined variable: Files in E:\xampp\htdocs\CAKE SHOP\add_product.php on line 11 Warning: getimagesize(): Filename cannot be empty in E:\xampp\htdocs\CAKE SHOP\add_product.php on line 11 Warning: mysql_close(): no MySQL-Link resource supplied in E:\xampp\htdocs\CAKE SHOP\add_product.php on line 27 – Manali Bhavsar Sep 20 '15 at 08:24