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