Jump to content

Recommended Posts

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>');
}
}
?>

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());
}

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() )";

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.