neogemima Posted April 23, 2009 Share Posted April 23, 2009 I've been tearing my hair out over this one for two days. I have an html form that takes the user's input and inserts it into a database. The form also takes three images of their choosing and uploads them into an upload folder on GoDaddy's Server. For some reason the uploads work just fine to the upload folder, but the query is returning false. I can't for the life of me figure this out. Here is a snippet of my code: <?php $date= date("Y-m-d"); $title=$_POST['title']; $type=$_POST['type']; $email=$_POST['email']; $price=$_POST['price']; $equipmentcontent= str_replace("\n", "<br>", $_POST['equipmentcontent']); $img1=time().$_FILES['img1']['name']; $img2=time().$_FILES['img2']['name']; $img3=time().$_FILES['img3']['name']; if (!$title || !equipmentcontent || !$type || !$email) { echo "Please fill out the title, type of equipment, email, and the content of your post before proceeding.<br>"; exit; } if (!get_magic_quotes_gpc()) { $title = addslashes($title); $equipmentcontent = addslashes($equipmentcontent); $type = addslashes($type); $email = addslashes($email); $price = addslashes($price); } @ $db = mysqli_connect('xxxxx', 'xxxx', 'xxxxx', 'xxxxxx'); if ((($_FILES['img1']['type'] == "image/gif") || ($_FILES['img1']['type'] == "image/jpeg") || ($_FILES['img1']['type'] == "image/pjpeg")) && (($_FILES['img2']['type'] == "image/gif") || ($_FILES['img2']['type'] == "image/jpeg") || ($_FILES['img2']['type'] == "image/pjpeg")) && (($_FILES['img3']['type'] == "image/gif") || ($_FILES['img3']['type'] == "image/jpeg") || ($_FILES['img3']['type'] == "image/pjpeg")) && ($_FILES['img1']['size'] <= 150000) && ($_FILES['img2']['size'] <= 150000) && ($_FILES['img3']['size'] <= 150000)) { if (mysqli_connect_errno()) { echo "Error: Could not connect to database. Please try again later."; exit; } if (($_FILES['img1']['error'] || $_FILES['img2']['error'] || $_FILES['img3']['error']) > 0) { echo "Error: ".$_FILES['img1']['error']."<br> ".$_FILES['img2']['error']."<br>".$_FILES['img3']['error']."."; } else { move_uploaded_file($_FILES['img1']['tmp_name'], "uploads/".time().$_FILES['img1']['name']); move_uploaded_file($_FILES['img2']['tmp_name'], "uploads/".time().$_FILES['img2']['name']); move_uploaded_file($_FILES['img3']['tmp_name'], "uploads/".time().$_FILES['img3']['name']); } //I assume this is where everything goes wrong. $query = "INSERT INTO Equipmentposting (date, type, title, price, email, content, img1, img2, img3) VALUES ('$date', '$type', '$title', '$price', '$email', '$equipmentcontent', '$img1', '$img2', '$img3')"; $result = mysqli_query($db, $query); if ($result) { echo '<font color="#666666">'; echo 'Thank you.<br><br>'; echo '<font color="#000000">'.mysqli_affected_rows($db).'</font> posting has been uploaded to the site.<br><br>'; echo 'This posting will remain on the site for 30 days and can be seen in our <a href="http://www.biotechposting.com/pages/equipment.html" target="_parent">Equipment Classifieds</a> section.'; echo '<br><br>'; echo '<font size="-1">'; echo 'This service will remain free for the first few months of activity on BioTechPosting after which it will be a flat fee of $20 to post regardless of your location. Accompanying a post of this kind, you will also be able to post free industry news, a regular $20 value.<br>'; echo '</font></font>'; echo '<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>'; } else { echo "An error has occurred. Your post was not added.<br>"; echo "".$query."<br>."; } mysqli_close($db); } else { echo "Image files exceed maximum upload size of 150K or are an invalid format.<br>"; } ?> My echo statement comes out like this: An error has occurred. Your post was not added. INSERT INTO Equipmentposting (date, type, title, price, email, content, img1, img2, img3) VALUES ('2009-04-23', 'General Laboratory', 'asdf', 'asdf', 'asdf', 'asdf', '1240517000BETA-UNDER-CONSTRUCTION.jpg', '1240517000discussion.jpg', '1240517000reviews.jpg') I echoed the $query to see exactly what it was trying to pass and everything appears to be fine. What am I doing wrong here? Quote Link to comment Share on other sites More sharing options...
ram4nd Posted April 23, 2009 Share Posted April 23, 2009 $result = mysqli_query($db, $query); Shouldn't it be: $result = mysql_query($db, $query); Quote Link to comment Share on other sites More sharing options...
Bauer418 Posted April 23, 2009 Share Posted April 23, 2009 Place ticks around your column names, so you change: INSERT INTO Equipmentposting (date, type, title, price, email, content, img1, img2, img3) VALUES ('2009-04-23', 'General Laboratory', 'asdf', 'asdf', 'asdf', 'asdf', '1240517000BETA-UNDER-CONSTRUCTION.jpg', '1240517000discussion.jpg', '1240517000reviews.jpg') to the following: INSERT INTO Equipmentposting (`date`, `type`, `title`, `price`, `email`, `content`, `img1`, `img2`, `img3`) VALUES ('2009-04-23', 'General Laboratory', 'asdf', 'asdf', 'asdf', 'asdf', '1240517000BETA-UNDER-CONSTRUCTION.jpg', '1240517000discussion.jpg', '1240517000reviews.jpg') The reason you need this is because date (and possibly type, though I don't remember exactly) are reserved MySQL keywords. The ticks around the column names indicate that it is a reference to the column, and not the MySQL keyword. @ram4nd: no. He using the mysqli extension, not the mysql extension. Quote Link to comment Share on other sites More sharing options...
revraz Posted April 23, 2009 Share Posted April 23, 2009 Put mysqli_error($db) after your query to see if you get an error. Neither Type nor Date is reserved. http://dev.mysql.com/doc/refman/5.1/en/reserved-words.html Quote Link to comment Share on other sites More sharing options...
Bauer418 Posted April 23, 2009 Share Posted April 23, 2009 Good call. I figured because DATE is a data type it would be reserved, but evidently they allow it (as seen in the list of acceptable unquoted strings below the reserved keywords). Likely a column/table identifier error then. As always, mysqli_error will have your answer. Quote Link to comment Share on other sites More sharing options...
neogemima Posted April 23, 2009 Author Share Posted April 23, 2009 revraz, that did it for me. When I echoed the mysqli_error it said I made a stupid mistake and didn't autoincrement my row id. This whole time I've been getting false because of a duplicate entry in the id column. I feel like an idiot, thank you all for the help. Quote Link to comment 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.