kevwood Posted March 5, 2015 Share Posted March 5, 2015 I have created to parts of code that work independently of each other but when i try combine them the upload part stops working! here is my html form code <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Sonc Hydro -Mammoth Pro Tents</title> <link rel="stylesheet" type="text/css" href="css/main_styles.css" /> <link rel="stylesheet" type="text/css" href="css/main_layout.css" /> <script type="text/javascript" src="scripts/image.js"></script> </head> <body bgcolor="#4d6821" onLoad="switchImage('slideImg')"> <div id="admin_container"> <div id="admin_body"> <h1>Upload Page</h1> <div class="admin_select"> <h2>Pleas select which action to take</h2> <form id="selection"> <select onchange="window.open(this.options[this.selectedIndex].value,'_top')"> <option>Please Select..</option> <option value="test_add.php">Add Prod</option> <option value="test_edit.php">Edit Prod</option> <option value="test_del.php">Delete Prod</option> </select> </form> </div> <div class="admin_select"> <form id="prod" action="insert3.php" method="post" enctype="multipart/form-data"> Name:<br /> <input type="text" name="name" id="name" /> <br /> <br /> Desctiption:<br /> <textarea id="description" name="description" rows="25" cols="100"></textarea> <br /> <br /> Price: (do not put £ symbol in)<br /> <input type="text" name="price" id="price" /> <br /> <br /> Image: <input type="file" name"image" id="image" /> <br /> <br /> Type: <select id="prod_type" name="prod_type" > <option value="lighting">Lighting</option> <option value="systems">Systems</option> <option value="filters/fans">Filters/Fans</option> <option value="tents">Tents</option> <option value="nutrient">Nutrient</option> <option value="sheeting">Sheeting</option> <option value="accessories">Accessories</option> </select> <br /> <br /> Made by:<br /> <input type="text" name="made" id="made" /> <br /> <br /> Distributed by:<br /> <input type="text" name="distribute" id="distribute" /> <br /> <br /> <input type="submit" name="prod_sub" id="prod_sub" value="add to DB" /> </form> </div> </div> </div> </body> </html> and here is the php script insert3.php <?php $name = $_POST['name']; $desc = $_POST['description']; $price = $_POST['price']; $prod_type = $_POST['prod_type']; $made = $_POST['made']; $dist = $_POST['distribute']; $servername = "xxxxxxxxxx"; $db = "xxxxxxxxxxx"; $user_name = "xxxxxxxxxx"; $password = "xxxxxxxxx"; $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["prod_sub"]) && isset($_FILES["image"])) { $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."; } } // 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(); } try{ $stmt = $conn->prepare("INSERT INTO Products(NAME, DESCRIPTION, PRICE, IMAGES, 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(); } ?> as i mentioned if i take the code out for the file upload it works fine (apart from storing the image how i want) and the upload script works fine to upload an image but they do to work together. the information is added to the db but the file is not uploaded and every echo message i have is displayed. any other information need please ask, i can give you a link to the testing site if needed. Quote Link to comment https://forums.phpfreaks.com/topic/295121-upload-image-script-not-working-with-insert-into/ Share on other sites More sharing options...
mac_gyver Posted March 5, 2015 Share Posted March 5, 2015 what does the following debugging code, added immediately after the line with the <?php tag in the insert3.php file, show - echo "<pre>"; echo "POST:"; print_r($_POST); echo "FILES:"; var_dump($_FILES); echo "</pre>"; Quote Link to comment https://forums.phpfreaks.com/topic/295121-upload-image-script-not-working-with-insert-into/#findComment-1507643 Share on other sites More sharing options...
kevwood Posted March 5, 2015 Author Share Posted March 5, 2015 here is what i get printed out POST:Array([name] => dgdfgfdgdf[description] => rsthsrhnfgxb[price] => 666[prod_type] => sheeting[made] => hxfghrsh[distribute] => rshrjryj[prod_sub] => add to DB)FILES:array(0) {} Quote Link to comment https://forums.phpfreaks.com/topic/295121-upload-image-script-not-working-with-insert-into/#findComment-1507658 Share on other sites More sharing options...
Solution mac_gyver Posted March 6, 2015 Solution Share Posted March 6, 2015 your form <input tag is missing the = in the name='image' attribute. yours - <input type="file" name"image" id="image" /> should be - <input type="file" name="image" id="image" /> 1 Quote Link to comment https://forums.phpfreaks.com/topic/295121-upload-image-script-not-working-with-insert-into/#findComment-1507785 Share on other sites More sharing options...
kevwood Posted March 9, 2015 Author Share Posted March 9, 2015 i feel like taking a long walk off a short pier haha. why does it always end u being something so small haha. i have fixed that and updated the code so the insert is actually part of the upload script now. here is the updated script which is working fine now. <?php echo "<pre>"; echo "POST:"; print_r($_POST); echo "FILES:"; var_dump($_FILES); echo "</pre>"; $name = $_POST['name']; $desc = $_POST['description']; $price = $_POST['price']; $image = $_POST['image']; $prod_type = $_POST['prod_type']; $made = $_POST['made']; $dist = $_POST['distribute']; $servername = "xxxxxxxxxxxxxxxxx"; $db = "xxxxxxxxxxxxx"; $user_name = "xxxxxxxxxxx"; $password = "xxxxxxxxx"; $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["prod_sub"]) && isset($_FILES["image"])) { $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."; // 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(); } try{ $stmt = $conn->prepare("INSERT INTO Products(NAME, DESCRIPTION, PRICE, IMAGES, 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(); } } else { echo "Sorry, there was an error uploading your file."; } } ?> thanks for the help on this. Quote Link to comment https://forums.phpfreaks.com/topic/295121-upload-image-script-not-working-with-insert-into/#findComment-1507939 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.