0

i am trying to write a script that stores information from a html form into a mysql db. i have had the code work to stoer the information into the db but have since modified the code to accomodate the image up load also.

i wan the code to stoe the file into a specified folder and then store the path to this file in the db. the code for the file upload works fine as it was taken from the w3schools site. here is the upload code

$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES['$image']["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {

        $uploadOk = 1;
    } else {

        $uploadOk = 0;
    }
}

// Check if file already exists
if (file_exists($target_file)) {

    $uploadOk = 0;
}
// Check file size
if ($_FILES['$image']["size"] > 500000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES['$image']["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES['$image']["name"]). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}

i have taken this code and added it to my parameterised insert into query code which is as follows

<?php

$name = $_POST['name'];
$desc = $_POST['description'];
$price = $_POST['price'];
$image = $_POST['image'];
$prod_type = $_POST['prod_type'];
$made = $_POST['made'];
$dist = $_POST['distribute'];

$servername = "mysql5.000webhost.com";
$db = "a6382499_product";
$user_name = "a6382499_sonic";
$password = "daniweb1";


// create connection to db
try {
    $conn = new PDO("mysql:host=$servername;$db", $user_name, $password);
// PDO error mode set to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Connected to database";
}
// Set veriable to catch error created
catch(PDOException $error)
    {
    echo "Connection failed: <br />" . $error->getMessage();
}

$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES['$image']["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {

        $uploadOk = 1;
    } else {

        $uploadOk = 0;
    }
}

// Check if file already exists
if (file_exists($target_file)) {

    $uploadOk = 0;
}
// Check file size
if ($_FILES['$image']["size"] > 500000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES['$image']["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES['$image']["name"]). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}

try{
    $stmt = $conn->prepare("INSERT INTO Products(NAME, DESCRIPTION, PRICE, IMAGE, TYPE, MADE, DISTRIBUTE)  
    VALUES (:name, :desc, :price, :image, :prod_type, :made, :dist)");
    $stmt->bindParam(':name', $name);
    $stmt->bindParam(':desc', $desc);
    $stmt->bindParam(':price', $price);
    $stmt->bindParam(':image', $target_file);
    $stmt->bindParam(':prod_type', $prod_type);
    $stmt->bindParam(':made', $made);
    $stmt->bindParam(':dist', $dist);
    $stmt->execute();

    $conn->exec($sql);
    echo "New record added";
}
catch(PDOException $error)
{
    echo $sql . "<br />" . $error->getMessage();
}

?>

when i run the code i am getting the following error message

Fatal error: Call to undefined method PDOStatement::exec() in /home/a6382499/public_html/insert3.php on line 84

any help wold be great

Edited by kevwood

2
Contributors
11
Replies
36
Views
2 Years
Discussion Span
Last Post by kevwood
0

line 84 of the code is as follows

$stmt->execute();

i am using the correct statement

0

i have also cleared my chache to make sure it is not running on an old copy in the cache

0

Well, the error message you entered clearly states

Fatal error: Call to undefined method PDOStatement::exec() in /home/a6382499/public_html/insert3.php on line 84

that your PDOStatement object is trying to use the method exec(), which doesn't exist in a PDOStatement object. Maybe the lines aren't the same in your post as in your code file, but I think the answer to your problem may be the same: your PDOStatement object (whichever it may be) is trying to use a method that it doesn't have.

Also, why is there a $conn->exec($sql); on your line 86? It appears to be a bit useless to me, because I don't see $sql defined anywhere, and also you already execute a statement two lines above.

0

i know the error message states that but if you look at the code it clearly does not say that. so why am i getting this error message?

Edited by kevwood

0

Can you confirm that in your code line 84 is really $stmt->execute(); and can you confirm that $stmt is really a PDOStatement (e.g. var_dump($stmt);?

Could it be that $conn->exec($sql); is generating the error?

0

Line 84 is

$stmt->execute();

i have deleted all the files from the server re-uploaded everything an that is not coming up as a error no more. i am having problems with the uploading of the image.

if i run the script just as the upload it works fine with all the variables changed to work with my form. Once i move the code into the script with the insert into and the connection it stops working!

here is the script that works

html form code

<!DOCTYPE html>
<html>
<body>

<form action="upload.php" method="post" enctype="multipart/form-data">
    Select image to upload:
    <input type="file" name="image" id="image">
    <input type="submit" value="Upload Image" name="submit">
</form>

</body>
</html>

upload.php

<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["image"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["image"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
}
// Check if file already exists
if (file_exists($target_file)) {
    echo "Sorry, file already exists.";
    $uploadOk = 0;
}
// Check file size
if ($_FILES["image"]["size"] > 500000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["image"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["image"]["name"]). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}
?>

here is my script with the above code

<?php

$name = $_POST['name'];
$desc = $_POST['description'];
$price = $_POST['price'];
$image = $_POST['image'];
$prod_type = $_POST['prod_type'];
$made = $_POST['made'];
$dist = $_POST['distribute'];

$servername = "mysql5.000webhost.com";
$db = "a6382499_product";
$user_name = "a6382499_sonic";
$password = "xxxxxxxxxxx";


// create connection to db
try {
    $conn = new PDO("mysql:host=$servername;dbname=$db", $user_name, $password);
// PDO error mode set to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Connected to database <br /> <br />";
}
// Set veriable to catch error created
catch(PDOException $error)
    {
    echo "Connection failed: <br /> <br />" . $error->getMessage();
}
//file upload script
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["image"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["image"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image. <br /> <br />";
        $uploadOk = 0;
    }
}
// Check if file already exists
if (file_exists($target_file)) {
    echo "Sorry, file already exists. <br /> <br />";
    $uploadOk = 0;
}
// Check file size
if ($_FILES["image"]["size"] > 500000) {
    echo "Sorry, your file is too large. <br /> <br />";
    $uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) {
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed. <br /> <br />";
    $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded. <br /> <br />";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["image"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["image"]["name"]). " has been uploaded. <br /> <br />";
    } else {
        echo "Sorry, there was an error uploading your file. <br /> <br />";
    }
}
try{
    $stmt = $conn->prepare("INSERT INTO Products(NAME, DESCRIPTION, PRICE, IMAGE, TYPE, MADE, DISTRIBUTE)  
    VALUES (:name, :desc, :price, :image, :prod_type, :made, :dist)");
    $stmt->bindParam(':name', $name);
    $stmt->bindParam(':desc', $desc);
    $stmt->bindParam(':price', $price);
    $stmt->bindParam(':image', $target_file);
    $stmt->bindParam(':prod_type', $prod_type);
    $stmt->bindParam(':made', $made);
    $stmt->bindParam(':dist', $dist);
    $stmt->execute();


    echo "New record added";
}
catch(PDOException $error)
{
    echo $sql . "<br />" . $error->getMessage();
}

?>

it is adding the data to the db but only $target_dir is being stored in the db but not the $target_file. i am just getting the all the error messages printed from the upload with no file stored in the directory.

Edited by kevwood: more info added

0

all the messages i am gettin on the output page is as follow

Connected to database

Sorry, file already exists.

Sorry, only JPG, JPEG, PNG & GIF files are allowed.

New record added

the file does not already exist and it is a png that i am uploading.

it works perfectly fine if it is not placed within my script i have written for the insert

Edited by kevwood

0

Well, file_exists() does not return true if the file does not exist. That means that $target_file must exist. Can you triple check that it does not? I.e.: check the value of $target_file and output getcwd() (outputs the current working directory) and see if the file really does not exist?

0

i have accessto the directory i am storing the files in. it is a testing site at the min on a free hosting server so when i have uploaded the file i have deleted the file instantly so i can continue testing.

i can give you a link to the testing page if that would help out anymore?

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.