
kevwood
Members-
Posts
28 -
Joined
-
Last visited
Everything posted by kevwood
-
Upload image script not working with insert into
kevwood replied to kevwood's topic in PHP Coding Help
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. -
Upload image script not working with insert into
kevwood replied to kevwood's topic in PHP Coding Help
here is what i get printed out -
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.
-
thanks for the replies, i thought the prepare was a function or predefined statement within mysql lol. it has been a long time since i messed about with any of this. who do i give the best answer to haha.
-
Hello all, I am trying to setup parameterised queries to prevent sql injection from a html from when adding data to my database. I have been doing some reading on W3schools website about it. the code i have been looking at i tried to combine with my request to insert but with no joy. here is the code i was looking at $stmt = $dbh->prepare("INSERT INTO Customers (CustomerName,Address,City) VALUES (:nam, :add, :cit)"); $stmt->bindParam(':nam', $txtNam); $stmt->bindParam(':add', $txtAdd); $stmt->bindParam(':cit', $txtCit); $stmt->execute(); and here is the code i have created from the code i was looking above <?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 = "phpfreaks1"; global $conn; // 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: " . $error->getMessage(); } try{ $stmt = $db->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', $image); $stmt->bindParam(':prod_type', $prod_type); $stmt->bindParam(':made', $made); $stmt->bindParam(':dist', $dist); $stmt->exec(); $conn->exec($sql); echo "New record added"; } catch(PDOException $error) { echo $sql . "<br />" . $error->getMessage(); } ?> the error message i am getting is and line 32 in the code is $stmt = $db->prepare("INSERT INTO Products(NAME, DESCRIPTION, PRICE, IMAGE, TYPE, MADE, DISTRIBUTE) any help would be much appreciated.
-
thanks for the reply, i have found something that i may be able to manipulate how i would like. i will see how i go with this, i will no doubt be on looking for help depending how much comes back to me lol. a lot has changed in 7 years!!
-
yeah i would rather learn it (again lol) rather than just ask for the script
-
a few years ago i created a script that upload images to the file directory and stored the file location in a mysql db. i cannot find this script anywhere and it really annoying me haha. i know this script would be useless now due to the changes with the code but it would have at least been a starter for me. what i am looking for is a link to a good tutorial that will aid me in building this, i understand it may be a case of doing two different ones and combing. all i have searched for up to now is not bringing anything of use up really. just looking for a good base start to work from, any help at all would be greatly appreciated.
-
it was a problem with the server name, i had to specify the name of the server my db was being held on. is this simply because it is a free hosting site and my site and database are being held on different servers?
-
i have checked the user name and password a number of times and even changed the password to phpfreaks1 while i was uploading it on here for security reasons even though it only for testing anyway.
-
thanks for the reply. i have updated my code with this (this is the PDO correct way i think, please correct me if i am wrong). // 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: " . $error->getMessage(); } and i am getting this message my password and user name are 100% correct i have re entered them and updated them a few times to make sure of this
-
Hello all, Been a while since i have been on here since he lack of using my degree lol. but i am dusting the cob webs of the brain an realising it has all drastically changed since i last used php and mysql. any enough with the blabbing here is my problem! i am trying to establish a connection with a database i have set up on a free hosting server. when i run the code i am getting conflicting messages. here is my code <?php $name = "name"; $desc = "description"; $price = "price"; $image = "image"; $prod_type = "prod_type"; $made = "made"; $dist = "distribute"; $server = "localhost"; $db = "a6382499_product"; $user_name = "a6382499_sonic"; $password = "phpfreaks1"; // create connection to db $conn = new MySQLi($server, $user_name, $password); // check connection if (!$conn->connect_error) { die("Connection failed: " . $conn->connect_error()); } echo "Connect successful"; ?> it is giving me this error message when i run the code but then i am getting the message that i am connected to the db successfully . any help with this would be great.
-
i have been playing with the code some more and now have managed to get everything to sit virtually on top of each of. i think i am having problems with using the code i have written as the container div i load the three sections of information into is a class and reused to load further bits of information into down the page. i am trying to build it so it has fluidity to the design so the panel which holds the information can stretch to fit the available space between the div's i have floated left and right (which i am struggling to achieve at the min). but firstly i want to just get the look of the panel worked out. this is what my code looks like now .body_panel{ position:relative; overflow:visible; background-color : #FFF; border : solid #45bf55; border-width : 3px; margin-bottom:30px; } .body_img{ position : absolute; width : 200px; height : 200px; background-color : #e0e0e0; text-align : center; display : table-cell; vertical-align : middle; margin-right : 25px; } .body_text{ position:absolute; left:25%; width : 230px; } .body_link{ position : absolute; right : 0; margin-right : 3px; padding-top : 90px; } it is just sitting all the individual panels on top of each starting the top of the next panel 30px down from the last. at least my images are now centered to the img div lol.
-
ok so my code now looks like this .body_panel{ overflow : hidden; width : auto; background-color : #FFF; border : solid #45bf55; border-width : 3px; margin-bottom:30px; } .body_text{ position:relative; top:0; left:200px; width : 230px; } .body_img{ position : relative; width : 200px; height : 200px; background-color : #e0e0e0; text-align : center; display : table-cell; vertical-align : middle; margin-right : 25px; padding-top : 10px; } .body_link{ float : right; margin-right : 3px; padding-top : 90px; } i have got my image to appear in the center of the div now but i cannot get my other two divs sit next to them now. i need the body_img, the body_text then the body link to now appear all next to each other but they are now all underneath each other. would i need to set a width on the body panel and not set it to auto? the reason i set it to auto was to try to get it to fill the space in the middle of the page. ideally the body panel would stretch to fit the gap between the left and right divs on the page with a margin of 30 px between each side. One problem at a time though. so how do i go about getting the divs to sit how i need them to now. i have tried floating to the right but had no joy and tried to set the relative position with a left margin of 200px but it still just placed it underneath the image but to the right and most of the text is not visible.
-
so from what i have read i would need to use a relative positioned div then have an absolute div placed in the center of the second div to load the image into this. would this not just give the same problem though as the images that are being loaded in are of different sizes. at the min i have it set up with a container div holding three separate div to hold the information. i have floated the first to divs left and the the third right. i am just loading the images into the first div. if i now set the first div to be relative to the container div it would then sit it in the top left corner of the container. i would then need to position the next div to be absolute and give it some margins from the top and left to move this towards the center. i will try this code out now (or something similar) #container_panel { position : relative; } #img_panel { position : absolute; top : 20px; left : 20px; width : 200px; } #text_panel{ float : right; } #link_panel{ float : right; }
-
when trying to use positioned elements i have not been able to get the elements to sit right on the page. does any one have a link to some good info on working with positioned elements. i have been trying to convert all the width on my div's to % instead of using px i am used to using a tabular way of building web pages so the whole div/css thing is new but i am making good head way up to now.
-
I am loading information on to my page into a section which is 600px wide and 200px high. i have sectioned the left side of this for the images which gives me a space of 200x200px to load images into. the problem i am getting is that is my images are say 100px high and the next is 150px because i have used padding to get the larger image into the middle of the div the small one with only remain as far from the top as the padding states.
-
from what i can see from that it is just centering a div inside another div. this would just give me the same problem though wouldnt. when the inages are loaded into the inner differ the small ones will appear out of place, and also this div will not be centered to to the center of the page just in its 200m x 200px space.
-
hi i am trying to center and image in a div horizontally and vertically. i have tried to use a table cell then center that but no eval. i added some padding to the top which worked well. but when the small images are loaded in they just sit 10px from the top. here is the code i am using to try to center the div text-align : center; display : table-cell; vertical-align : middle; padding-top : 10px;
-
thanks for the link. i have fixed it by floating the body to the left also. it is looking good on the screen i am using. here are the changes i made to the code #navigation{ float : left; width : 178px; border : solid #45bf55; border-width : 3px; background-color : #fff; margin-right : 30px; } #advert{ float : right; padding : 4px; border : solid #45bf55; border-width : 3px; background-color : #FFF; margin-left : 30px; vertical-align : middle; } #body{ float : left; } .body_panel{ overflow : hidden; height : 200px; width:auto; background-color : #FFF; border : solid #45bf55; border-width : 3px; margin-bottom:30px; } i also used the same technique to position the elements within the body panel. again thank for the link and helping me understand the positioning better.
-
hi all, i am having a problem with getting my divs in the center of the page to look how i want them to. i have not built a webpage without using the table format as this was how i was taught and i am eventually getting my head round the div and css way of doing things. i will attach an image of the problem i am having (fig 1) and what i would like it to look like (fig 2) and the code for my divs and page layout. the code for my divs #navigation{ float : left; width : 178px; border : solid #45bf55; border-width : 3px; background-color : #fff; margin-right : 30px; } #advert{ float : right; padding : 4px; border : solid #45bf55; border-width : 3px; background-color : #FFF; margin-left : 30px; vertical-align : middle; } #body{ margin-left : 30px; margin-right : 30px; } .body_panel{ overflow : hidden; height : 200px; width:auto; background-color : #FFF; border : solid #45bf55; border-width : 3px; margin-bottom:30px; }
-
all done with some cell padding and text-align.
-
first time i have built a site using css. just been reading up on the rest that is need to be done on the browser settings. it is now showing how i would like just got to position the text to where i want it to be. thank you for the reply on this.
-
hi i am trying to add a background image to my links in a list on my page but i cannot get the image to show how i would like. i am either going about it the wrong way or it simply cannot be done how i would like. either way here is the code that i am using at the min #P_navigation li{ list-style-type : none; margin : 0; margin-left : 0; background : url(../images/buttons/main_link_bg.png)-50px -15px no-repeat; width : 192px; height : 50px; } i will attach an image of what it is doing and one of the image i am trying to display
-
very true little point in creating a new table for each customer. thanks for the links i will read through them now.