user2411816
user2411816

Reputation: 11

undefined index logical error in php

hy! this is my html code....

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Add New Templete</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <?php include 'side.php'; ?>
    <div class="main">
    <h2>Add New Templete</h2>
    <form method="post" action="add.php" enctype="multipart/form-data">
        <table border="1px solid">
        <tr>
        <td>Templete Name:</td><td><input type="text" name="temp_name"></td></tr>
        <tr>
        <td>Templete Category</td><td><input type="text" name="category"></td></tr>
        <tr>
        <td>Templete Image</td><td><input type="file" name="image"></td></tr>
        <tr>
        <td>Templete Discription</td><td><input type="text" name="decp"></td></tr>
        <tr>
        <td>Templete Quantity</td><td><input type="text" name="qty"></td></tr>
        <tr>
        <td>Templete Price</td><td><input type="text" name="price"></td></tr>
        <tr>
            <td></td>
        <td>    
        <input type="submit" value="ADD">
        </td>
        </tr>
        </table>
    </form>

</div>
</body>
</html>

and this is my php code

<?php
include 'connection.php';
$name=$_POST['temp_name'];
$cat=$_POST['category'];
$image=$_POST['image'];
$desc=$_POST['decp'];
$qty=$_POST['qty'];
$price=$_POST['price'];
$qry="INSERT INTO templetes(templete_name,category,image,description,quantity,price)VALUES('$name','$cat','$image','$desc','$qty','$price')";
$res=mysql_query($qry,$con);
if($res)
{

    echo "record inserted";
}
mysql_close($con);
?>

Every thing are fine the data will be stored in database but the image are not stored in database, it says "undefined index image on ....... line ...." means it will show the undefined index error. all the data will stored except the image?

Upvotes: 0

Views: 80

Answers (3)

hardcoderrsl
hardcoderrsl

Reputation: 361

 $_POST['image'];

you can't get file in $_POST please use

 $_FILES['image']

And as you are inserting the image into a table, so it has to be converted to binary from string. Here is the solution for you-

connection.php

<?php
 $con = mysqli_connect("localhost", "root", "", "todo");
 if (!$con) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
 }
?>

and add.php

<?php
include 'connection.php';
$name=$_POST['temp_name'];
$cat=$_POST['category'];
$image=$_FILES['image'];
$desc=$_POST['decp'];
$qty=$_POST['qty'];
$price=$_POST['price'];
$imgData =addslashes (file_get_contents($_FILES['image']['tmp_name']));;
$qry="INSERT INTO     templetes(templete_name,category,image,description,quantity,price)VALUES('$name'   ,'$cat','{$imgData}','$desc','$qty','$price')";
$res=mysqli_query($con,$qry);
if($res)
{
   echo "record inserted";
}
mysqli_close($con);
?>

remember data type of the image field in the database should be BLOB or Large BLOB.but remember this is not the safest or coolest way to do the job.

Upvotes: 1

Girish
Girish

Reputation: 12117

$_POST['image']; you can't get file in $_POST please use

$_FILES['image']

Also, you should use move_uploaded_file function to move the uploaded file on server dir.

move_uploaded_file($_FILES['image']['tmp_name'], './you_dir_path'.$_FILES['image']['name'])

Upvotes: 0

P&#233;ter Simon
P&#233;ter Simon

Reputation: 421

Try this:

$image=$_FILES['image'];

Upvotes: 0

Related Questions