PDA

View Full Version : Code doesn't upload information to database


Adam_C
11-17-2009, 10:33 PM
This code doesnt upload the information from a html form, but it does upload the image to the folder.

<?php
///////////////////////////////////////error handling - if an error is detected in the url the appropriate error will be displayed
$error = $_GET['error'];
if($error == 1){
$error = "Unknown Image extension!";
}elseif($error == 2){
$error = "You have exceeded the size limit";
}else $error = '';
////////////////////////////////////// connection script - enter your details here
$user="root";
$password="cheddar";
$database="portfolio";
$con = mysql_connect (localhost,$user,$password);
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("logos", $con);

function getExtension($str) {// function to get file extension

$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
// define max file size, change to suit your own maximum file size
define ("MAX_SIZE","1000");
$errors=0;
//get the original file from form
if($_SERVER["REQUEST_METHOD"] == "POST")
{
$image = $_FILES["file"]["name"];// from your upload form
$uploadedfile = $_FILES['file']['tmp_name'];// from your upload form
// check for correct file extension
if ($image)
{
$filename = stripslashes($_FILES['file']['name']);
$extension = getExtension($filename);
$extension = strtolower($extension);
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))
{

header("location:upload.php?error=1");// error handling here - if the file ext is wrong the script will exit and redirect with error in the url
exit;///////////////////////////////// note that I have used adam.php as the file name, change this if you save the file differently
}
// check for correct file size
else
{
$size=filesize($_FILES['file']['tmp_name']);

if ($size > MAX_SIZE*1024)
{

header("location:upload.php?error=2");// error handling here - if the file size is wrong the script will exit and redirect with error in the url
exit;///////////////////////////////// note that I have used adam.php as the file name, change this if you save the file differently
}
// check which extension we have and create file
if($extension=="jpg" || $extension=="jpeg" )
{
$uploadedfile = $_FILES['file']['tmp_name'];
$src = imagecreatefromjpeg($uploadedfile);
}
else if($extension=="png")
{
$uploadedfile = $_FILES['file']['tmp_name'];
$src = imagecreatefrompng($uploadedfile);
}
else
{
$src = imagecreatefromgif($uploadedfile);
}
//declare destinations
$filename = "uploads/". $_FILES['file']['name'];
//write files to folders
imagejpeg($src,$filename,100);
//destroy temps to save memory
imagedestroy($src);
}
}
}
//If no errors registered, insert file name into database and print the success message
$name= $_POST['name'];
$shortinfo= $_POST['shortinfo'];
$longinfo= $_POST['longinfo'];

if(isset($_POST['Submit']) && !$errors)
{
mysql_query("INSERT INTO logos (id, name, shortinfo, longinfo, content) VALUES ('NULL', '$name', '$shortinfo', '$longinfo', '$image')");

$success = "Image Uploaded Successfully!";
}
//////////////////////////////////////////////////select file names from database
$query = sprintf("SELECT * FROM logos ");
$result = @mysql_query($query);
$row = @mysql_fetch_array($result);
?>

tux
11-18-2009, 06:03 AM
Where you have this....

mysql_query("INSERT INTO logos (id, name, shortinfo, longinfo, content) VALUES ('NULL', '$name', '$shortinfo', '$longinfo', '$image')");

Change it to this.....

mysql_query("INSERT INTO logos (content) VALUES ('$image')");

And see if that works for now.

edbr
11-18-2009, 06:23 AM
i was wondering about the null i assume its a incrementing id field, and i always use ' '.

tux
11-18-2009, 02:35 PM
Yes I agree Edbr,

If it is the tables primary key/auto increment then it doesnt need to be included at all.

Adam, as Edbr as noticed try this as your INSERT........


mysql_query ("INSERT INTO logos (name, shortinfo, longinfo, content)
VALUES ('$name', '$shortinfo', '$longinfo', '$image')");



Your ID, if its your primary key should INSERT itself.

Let me now how you get on.


Paul

Adam_C
11-18-2009, 03:10 PM
thanks guys, v much appreciated :)

will let you know how i get on.

Adam_C
11-18-2009, 03:18 PM
this is my new code, it still doesn't upload to the database, however it does upload to the folder.

<?php
///////////////////////////////////////error handling - if an error is detected in the url the appropriate error will be displayed
$error = $_GET['error'];
if($error == 1){
$error = "Unknown Image extension!";
}elseif($error == 2){
$error = "You have exceeded the size limit";
}else $error = '';
////////////////////////////////////// connection script - enter your details here
$user="root";
$password="cheddar";
$database="portfolio";
$con = mysql_connect (localhost,$user,$password);
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("logos", $con);

function getExtension($str) {// function to get file extension

$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
// define max file size, change to suit your own maximum file size
define ("MAX_SIZE","1000");
$errors=0;
//get the original file from form
if($_SERVER["REQUEST_METHOD"] == "POST")
{
$image = $_FILES["file"]["name"];// from your upload form
$uploadedfile = $_FILES['file']['tmp_name'];// from your upload form
// check for correct file extension
if ($image)
{
$filename = stripslashes($_FILES['file']['name']);
$extension = getExtension($filename);
$extension = strtolower($extension);
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))
{

header("location:upload.php?error=1");// error handling here - if the file ext is wrong the script will exit and redirect with error in the url
exit;///////////////////////////////// note that I have used adam.php as the file name, change this if you save the file differently
}
// check for correct file size
else
{
$size=filesize($_FILES['file']['tmp_name']);

if ($size > MAX_SIZE*1024)
{

header("location:upload.php?error=2");// error handling here - if the file size is wrong the script will exit and redirect with error in the url
exit;///////////////////////////////// note that I have used adam.php as the file name, change this if you save the file differently
}
// check which extension we have and create file
if($extension=="jpg" || $extension=="jpeg" )
{
$uploadedfile = $_FILES['file']['tmp_name'];
$src = imagecreatefromjpeg($uploadedfile);
}
else if($extension=="png")
{
$uploadedfile = $_FILES['file']['tmp_name'];
$src = imagecreatefrompng($uploadedfile);
}
else
{
$src = imagecreatefromgif($uploadedfile);
}
//declare destinations
$filename = "uploads/". $_FILES['file']['name'];
//write files to folders
imagejpeg($src,$filename,100);
//destroy temps to save memory
imagedestroy($src);
}
}
}
//If no errors registered, insert file name into database and print the success message
$name= $_POST['name'];
$shortinfo= $_POST['shortinfo'];
$longinfo= $_POST['longinfo'];

if(isset($_POST['Submit']) && !$errors)
{
mysql_query ("INSERT INTO logos (name, shortinfo, longinfo, content) VALUES ('$name', '$shortinfo', '$longinfo', '$image')");

$success = "Image Uploaded Successfully!";
}
//////////////////////////////////////////////////select file names from database
$query = sprintf("SELECT * FROM logos ");
$result = @mysql_query($query);
$row = @mysql_fetch_array($result);
?>

Adam_C
11-18-2009, 03:58 PM
the only thing it could be is my connection settings, not sure if i should have mentioned it before as i thought it would be irrelevant, but i am using XAMPP on my localhost.

tux
11-18-2009, 07:29 PM
Not used XAMMP before myself so wouldnt know if that would be the issue.

Try what I said before....

mysql_query("INSERT INTO logos (content) VALUES ('$image')");

Adam_C
11-18-2009, 08:54 PM
that didnt work either,

however i got it to work by using the phpmyadmin gui to insert information, then copied and pasted the code that was there and edited it to suit my needs :)

thanks guys ! :D