dekon Posted December 11, 2012 Share Posted December 11, 2012 hi i am creating a website where a user can post a title,upload an image, description of image and description of ad and with this i keep getting error writing to database could somebody help me with this code thanks. also i need to insert current date into the database thanks. <?php if ( !isset($_FILES['userFile']['type']) ) { die('<p>No image submitted</p></body></html>'); } ?> You submitted this file:<br /><br /> Temporary name: <?php echo $_FILES['userFile']['tmp_name'] ?><br /> Original name: <?php echo $_FILES['userFile']['name'] ?><br /> Size: <?php echo $_FILES['userFile']['size'] ?> bytes<br /> Type: <?php echo $_FILES['userFile']['type'] ?></p> <?php require 'mysql.php'; $title=$_POST['title']; $description=$_POST['description']; // Validate uploaded image file if ( !preg_match( '/gif|png|x-png|jpeg/', $_FILES['userFile']['type']) ) { die('<p>Only browser compatible images allowed</p></body></html>'); } else if ( strlen($_POST['altText']) < 9 ) { die('<p>Please provide meaningful alternate text</p></body></html>'); } else if ( $_FILES['userFile']['size'] > 16384 ) { die('<p>Sorry file too large</p></body></html>'); // Connect to database } else if ( !($link=mysql_connect($host, $user, $passwd)) ) { die('<p>Error connecting to database</p></body></html>'); } else if ( !(mysql_select_db($dbName)) ) { die('<p>Error selecting database</p></body></html>'); // Copy image file into a variable } else if ( !($handle = fopen ($_FILES['userFile']['tmp_name'], "r")) ) { die('<p>Error opening temp file</p></body></html>'); } else if ( !($image = fread ($handle, filesize($_FILES['userFile']['tmp_name']))) ) { die('<p>Error reading temp file</p></body></html>'); } else { fclose ($handle); // Commit image to the database $image = mysql_real_escape_string($image); $alt = htmlentities($_POST['altText']); $query = 'INSERT INTO image (title,type,name,alt,img,description,date) VALUES ("' . $title . '","' . $_FILES['userFile']['type'] . '","' . $_FILES['userFile']['name'] . '","' . $alt . '","' . $image . '","' . $description . '","' . $NOW() . '")'; if ( !(mysql_query($query,$link)) ) { die('<p>Error writing image to database</p></body></html>'); } else { die('<p>Image successfully copied to database</p></body></html>'); } } ?> Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted December 11, 2012 Share Posted December 11, 2012 What "error" are you receiving? Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted December 11, 2012 Share Posted December 11, 2012 I'll bet $NOW() is at least part of the problem. Quote Link to comment Share on other sites More sharing options...
MDCode Posted December 11, 2012 Share Posted December 11, 2012 (edited) Or not closing their echoes Edited December 11, 2012 by SocialCloud Quote Link to comment Share on other sites More sharing options...
dekon Posted December 11, 2012 Author Share Posted December 11, 2012 one of the errors is without the date function that my php echo's error writing image database Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted December 11, 2012 Share Posted December 11, 2012 There's nothing wrong with the echoes. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted December 11, 2012 Share Posted December 11, 2012 You want the MySQL function NOW(), not $NOW(), and it shouldn't be in quotes in the query string. Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted December 11, 2012 Share Posted December 11, 2012 one of the errors is without the date function that my php echo's error writing image database That message is obviously useless as it explains nothing about the actual error. First off, see Pickachu's suggestion and correct your query accordingly. Second, instead of displaying "error messages" that don't tell you a heck of a lot, use mysql_error() where applicable: if ( !(mysql_query($query,$link)) ) { die(mysql_error()); } Quote Link to comment Share on other sites More sharing options...
dekon Posted December 11, 2012 Author Share Posted December 11, 2012 Duplicate entry '0' for key 'PRIMARY' the id in my sql database don't change once i submit a new image Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted December 11, 2012 Share Posted December 11, 2012 Duplicate entry '0' for key 'PRIMARY' the id in my sql database don't change once i submit a new image And what is your primary key within the table? Quote Link to comment Share on other sites More sharing options...
dekon Posted December 11, 2012 Author Share Posted December 11, 2012 my primary key is ID Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted December 11, 2012 Share Posted December 11, 2012 Post your table schema showing ID. I'm guessing ID is not set to AUTO_INCREMENT. Quote Link to comment Share on other sites More sharing options...
dekon Posted December 11, 2012 Author Share Posted December 11, 2012 yes the problem was i forgot to tick auto_increment thanks for the help Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted December 11, 2012 Share Posted December 11, 2012 As long as you're editing things, you'll probably find debugging a lot easier if you make the query string readable by getting rid of all that obnoxious concatenation. $query = "INSERT INTO image ( title, type, name, alt, img, description, date) VALUES ( '$title', '{$_FILES['userFile']['type']}', '{$_FILES['userFile']['name']}', '$alt', '$image', '$description', NOW() )"; 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.