Jump to content

Inserting filename and date into mySQL


petenaylor

Recommended Posts

Hi all

 

I am trying to write a script that takes the filename and username from a form and adds it into a mySQL database. At the moment only the username is added? Here's my code:

 

<?php

include('includes/connection.php');

 

$username = $_POST['username'];

 

date_default_timezone_set('Europe/London'); // Change time zone to GMT

$date = date ('D M j G:i:s'); // Set date format

 

$path= "/home/cndvizp/public_html/prints2impress.co.uk/images/uploads/".$HTTP_POST_FILES['ufile']['name'];

 

 

if($ufile !=none)

{

if(copy($HTTP_POST_FILES['ufile']['tmp_name'], $path))

{

 

//insert new query

 

$filename = $_FILES['ufile']['name'];

 

$pfw_strQuery = "INSERT INTO `uploads`(`username`,`url`,`date_entered`)VALUES (\"$username\",\"$filename\",\"$date\")" ;

 

$pfw_result = mysql_query($pfw_strQuery);

if (!$pfw_result) {

die('Invalid query: ' . mysql_error());

}

 

//include ('payment.php');

echo 'done';

}

 

else

 

{

//include ('error.php');

echo 'error!';

exit;

}

}

 

?>

 

Can anyone help me get the filename and the date to be added?

 

Cheers

Pete.

Link to comment
Share on other sites

3 suggestions...

 

1. echo the date to see if it has the value you expect

2. make sure the field type and the value you are trying to insert are 'compatible'.

3. change this...

$pfw_strQuery = "INSERT INTO `uploads`(`username`,`url`,`date_entered`)VALUES (\"$username\",\"$filename\",\"$date\")" ;

 

to this...

$pfw_strQuery = "INSERT INTO uploads(username, url, date_entered)VALUES ('$username', '$filename', '$date')";

Link to comment
Share on other sites

Give this a try:

 

<?php
include 'includes/connection.php';

// Set a directory separator as a constant 
// for accessibility and compatibility
define('DS', DIRECTORY_SEPARATOR);

// Set web address
$web_addr = 'prints2impress.co.uk';

if (isset($_POST['username']) && !empty($_POST['username'])) {
  $username = trim($_POST['username']);
}
else {
  $errors[] = 'Please enter a username.';
}

// Change timezone to GMT
date_default_timezone_set('Europe/London');

// Set new date format
$date = date('D M j G:i:s');

if (isset($_FILES['ufile'])) {
  $filename = trim($_FILES['ufile']['name']);
}
else {
  $errors[] = 'Please select a file.';
}

if (!empty($errors)) {
  // Path for the file copy to server
  $path = $_SERVER['DOCUMENT_ROOT'] . DS . $web_addr . DS . '/images/uploads' . $filename;

  if (copy($_FILES['ufile']['tmp_name'], $path)) {
    $sql = sprintf("INSERT INTO `uploads` ( `username`, `url`, `date_entered` ) VALUES ( '%s', '%s', '%s' )", 
      mysql_real_escape_string($username), 
      mysql_real_escape_string($filename),
      mysql_real_escape_string($date));
      
    $result = mysql_query($sql);
    
    if ($result !== FALSE) {
      //include 'payment.php';
      echo 'Done';
    }
    else {
      die('Invalid query: ' . mysql_error());
    }
  }
}
else {
  echo '<ul>';
  foreach ($errors as $error) {
    echo '<li>' . $error . '</li>';
  }
  echo '</ul>';
}
?>

Link to comment
Share on other sites

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.