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.";
}
?> 

Link to comment
Share on other sites

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.

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.