Jump to content

[SOLVED] image upload and database issue(kinda half working)


Guber-X

Recommended Posts

okay, so I ended up using a image file upload script i found on the net, made sure it worked then started to modify it. so far the image upload works, but only about have the info is being put into the database.

 

in my database:

id (auto increments)

pic_id (not working)

file_name (works)

cat_id (not working)

comment (works)

 

ive been trying to get this to work for the past 5 hours and ive had no luck so far :(

 

upload.php page

<form enctype="multipart/form-data" action="add.php" method="POST">
Category: <select name="cat_id">

<?php
mysql_connect("localhost", "root", "") or die(mysql_error()) ;
mysql_select_db("test") or die(mysql_error()) ;

$result = mysql_query("SELECT * FROM photo_cat ORDER by id");
			if (!$result) {
			   die("query failed: " . msql_error());
			}

			while ($row = mysql_fetch_array($result)) {
			list($id, $cat_name, $cat_id, $location) = $row;

print('<OPTION VALUE="'.$cat_id.'">'.$cat_name); } ?>

</select><br />
Photo: <input type="file" name="file_name"><br>
Comment:<br />         <textarea name="comment" cols="30" rows="10"></textarea><br>
<input type="submit" value="Add">
</form>

 

add.php page

<?php

mysql_connect("localhost", "root", "") or die(mysql_error()) ;
mysql_select_db("test") or die(mysql_error()) ;

$result = mysql_query("SELECT * FROM photo_img ORDER by id");
			if (!$result) {
			   die("query failed: " . msql_error());
			}
	$pic_id = mysql_num_rows($result) + 1;

//This is the directory where images will be saved
$target = "images/";
$target = $target . basename( $_FILES['file_name']['name']);

//This gets all the other information from the form
$pic_id=$_POST['pic_id'];
$cat_id=$_POST['cat_id'];
$comment=$_POST['comment'];
$file_name=($_FILES['file_name']['name']);

// Connects to your Database
mysql_connect("localhost", "root", "") or die(mysql_error()) ;
mysql_select_db("test") or die(mysql_error()) ;

//Writes the information to the database
$query = "INSERT INTO photo_img (pic_id, cat_id, comment, file_name) VALUES ('$pic_id', '$cat_id', '$comment', '$file_name')";
mysql_query($query) or die('Error, insert query failed: '.mysql_error());

//Writes the photo to the server
if(move_uploaded_file($_FILES['file_name']['tmp_name'], $target))
{

//Tells you if its all ok
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
}
else {

//Gives and error if its not
echo "Sorry, there was a problem uploading your file.";
}
?> 

Hi Guber-X,

 

You haven't closed the option field properly - add:

 

</option>

 

Also, you're defining

 

$pic_id = mysql_num_rows($result) + 1;

 

But then further down that same page you're defining it again as;

 

$pic_id=$_POST['pic_id'];

 

But $_POST['pic_id']; doesn't exist so you're getting a NULL value.

 

Hope this helps.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.