PDA

View Full Version : form to submite info mysql & still upload pic


gavimobile
06-02-2008, 06:55 PM
ive done this before but cant seem to do it again properly.
right now the form upload a pic but doesnt insert anything in to the database.
when i remove && $item_pic (see notes below) than i believe the picture uploads to the location and the info gets put in to the database except the filename of the pic which does not get put in the field of mysql database (item_pic). if you didnt follow please read my further explination below.

further explination: i want this script to add information to a database and at the same time upload a picture to a folder on the webserver. the picture doesnt actually get put in to the database, but the file location of the picture does eg. sample.jpg so later ill be able to echo
that pictures location assuming the database saved it. since picture gets uploaded to a folder
so i would say <img src="my_uploaded_images/<? echo $row['item_pic']; ?>" />. i hope i was clear.

heres what i got i cleaned it up pretty well for you and thanks in advance


<?
require_once('includes/config.php');

/* here are my variables (i have more in my config.php) so excuese me if i left something out u might want to double check with me */

$item_name = $_REQUEST['item_name'];
$item_price = $_REQUEST['item_price'];
$item_desc = $_REQUEST['item_desc'];
$item_pic = $_POST['item_pic'];
$file = $_FILES['file']['name'];
$type = $_FILES["file"]["type"];
$size = $_FILES["file"]["size"];
$error = $_FILES["file"]["error"];
$tmp = $_FILES["file"]["tmp_name"];
$submit = $_POST['submit'];

// here is where i was refering to in my post the && $item_pic

if ($submit && $item_name && $item_desc && $item_price && $item_pic ){

// Here is my insert query

$query = sprintf("INSERT INTO item (item_name, item_desc, item_price, item_pic)
values ('$item_name', '$item_desc', '$item_price', '$item_pic' )");
mysql_query($query)or die(mysql_error());

}

// this is included in my upload_file.php

if (($type == "image/gif")
|| ($type == "image/jpeg")
|| ($type == "image/pjpeg")
|| ($type == "image/bmp")
&& ($size < 512000))
{
if ($error > 0)
{
echo "Return Code: " . $error . "<br />";
}
else
{
echo "Uploaded sucessfully!";
if (file_exists("vid/" . $file))
{
echo "A file with the same name already exists!";
}
else
{
move_uploaded_file($tmp,
"../images/item/" . $file);
}
}
}
else
{
}
?>

<form name="form1" method="POST" action="<? $_SERVER['PHP_SELF'];?>" enctype="multipart/form-data">
<label>Title</label>
<input type="text" name="item_name" id="item_name" size="32" />
<label>Description</label>
<textarea name="item_desc" id="item_desc" rows="5" cols="10"></textarea>
<label>Price</label>
<input type="text" name="item_price" id="item_price" size="32" />
<label>Picture</label>
<input type="file" name="file" id="file" size="32" />
<input type="submit" class="add_item_btn" value="Add This Item" />
<input type="hidden" name="submit" id="submit" value="1" />
</form>

davidj
06-02-2008, 09:58 PM
you need to revisit your logic

your echoing "upload successful" before the upload function is called

i also advise that you should not echo like this within the code and should assign the messages to a var which you echo in your html


if ($error > 0) {

echo "Return Code: " . $error . "<br />";

}else{

echo "Uploaded sucessfully!";

if (file_exists("vid/" . $file)){

echo "A file with the same name already exists!";
}else{

move_uploaded_file($tmp,"../images/item/" . $file);
}
}

gavimobile
06-02-2008, 11:56 PM
you need to revisit your logic

your echoing "upload successful" before the upload function is called

i also advise that you should not echo like this within the code and should assign the messages to a var which you echo in your html


if ($error > 0) {

echo "Return Code: " . $error . "<br />";

}else{

echo "Uploaded sucessfully!";

if (file_exists("vid/" . $file)){

echo "A file with the same name already exists!";
}else{

move_uploaded_file($tmp,"../images/item/" . $file);
}
}




regardless of where the echo is placed that shouldnt change the reason why the form isnt submitting anything in to the database.
is that upload file script stopping my insert into query?
thanks dj

davidj
06-03-2008, 07:06 AM
follow my upload tutorial

add the database insert after the upload function

gavimobile
06-03-2008, 07:22 AM
follow my upload tutorial

add the database insert after the upload function

ok ill let you know how this went for me later today,

thanks

gavimobile
06-14-2008, 06:10 PM
great! so i got it working, here is the code for others.

<form name="form1" method="POST" action="includes/scripts/upload_file.php" enctype="multipart/form-data">
<p>
<label>Title</label>
<input type="text" name="item_name" id="item_name" size="32" />

<label>Price</label>
<input type="text" name="item_price" id="item_price" size="32" />

<label>Description</label>
<textarea name="item_desc" id="item_desc" rows="5" cols="10"></textarea>


<label>Picture</label>
<input type="file" name="item_pic" id="item_pic" size="32" />

<br /><br />
<input type="submit" class="add_item_btn" value="Add This Item" />
</p>
</form>uploaded_file.php
<?
require_once('../config.php');

/* vars i have in my config related to this file

$item_name = $_POST['item_name'];
$item_price = $_POST['item_price'];
$item_desc = $_POST['item_desc'];
$item_pic = ($_FILES['item_pic']['name']);

*/


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

// Open the mysql connection
echo $db;

//Writes the information to the database
mysql_query("INSERT INTO item (item_name, item_price, item_desc, item_pic) values ('$item_name', '$item_price', '$item_desc', '$item_pic')") ;

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

//Tells you if its all ok
echo "<br />The file ". basename( $_FILES['item_pic']['name']). " has been uploaded, and your information has been added to the directory<br /><br /><a href='../../manage_store.php'>Go Back</a>";
}
else {

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


?>

gavimobile
06-14-2008, 06:39 PM
there is just one small problem. after the form is submitted it displays the number 1 before the message weather its an error or the file was sucessfully completed.
this is what the source looks like

1<br />The file gavi628.JPG has been uploaded, and your information has been added to the directory<br /><br /><a href='../../manage_store.php'>Go Back</a>

davidj
06-14-2008, 10:17 PM
please look at what you posted in the example

i cant believe you missed that

davidj
06-14-2008, 10:18 PM
also i wouldnt echo messages as you have

i would assign messages to variables and echo them in the HTML

gavimobile
06-18-2008, 03:45 PM
SOLVED

oops this problem i fixed a while ago! i dont remember what i did to fix it!
anyways what did i miss? post #7 is not the code, its the source, thats what the browser prints out by viewing the source. i think the problem was i was still submitting a hidden field with the value 1 just like in your tutorial, but i excluded that cause i completed my code a different way.

heres the code for others who want to print 3 mysql fields in one table row!


<?
require_once('includes/config.php');

session_start();

if ($usr_usr && $usr_pass){

$query = sprintf("SELECT * FROM usr WHERE usr_usr='$usr_usr' and usr_pass = '$usr_pass'");
$result = @mysql_query($query);
$row = @mysql_fetch_array($result);
}


if ($row){

$_SESSION['id'] = $row['usr_id'];

header("location:usr_pannel.php");
exit;


}elseif($submitted){

header("location:usr_error.php");
exit;

}

/*item database query*/
$query = sprintf("SELECT * FROM item ORDER BY item_date desc limit 9");
$results = @mysql_query($query);

// number of items per row
$item_per_row = 3;

?>

<html>
<head></head>
<body>

<table border=0 width=100%>

<?
if (@mysql_num_rows($results) == 0) {

echo "<td>Error: No items found.</td></tr>";

} else {

$count = 0;

while ($row_item = mysql_fetch_array($results)) {

echo "<td>" . "<img class='item_image' src='images/item/" .$row_item['item_pic'] . "' />" . "<br />" . "<div id='title'><strong>" . $row_item['item_name'] . "</strong></div>" . "<div id='price'><strong>₪ " . $row_item['item_price'] . "</strong></div>" . "<input class='Details-btn' type='submit' value='Details' /><input class='AddToCart-btn' type='submit' value='Add To Cart' />

" . "<br /><br />" . "</td>";

if (++$count % $item_per_row === 0) {

echo "</tr>";

}

}

if ($count % $item_per_row!== 0) {

echo "</td>";

}

}

?>
</table>


</body>
</html>

davidj
06-18-2008, 05:04 PM
nice one

i had a simlar task a while ago

this is how i did it

in the loop i increment a count by 1

i would divide the count by 3 and check the result is an integer

only those numbers (3,6,9 etc) would == an integer as any other number would create a float.

so by building some logic which detected integers i would know when to echo a <tr></tr> else echo a <td></td>