Forum Moderators: coopster

Message Too Old, No Replies

Ive made an upload form so i can store an image location in mysql db

Ive made an upload form so i can store an image location in mysql db

         

riggerz29

8:06 pm on Jan 16, 2009 (gmt 0)

10+ Year Member



ive made a form so i can upload news to my website. i have 4 feilds in the database photo, header, description, (pid which is set to auto increment) and i try to upload my details to the fields in the db but nothing adds in db the only thing that works is the photo moves to the correct folder in my server. Now if i delete the field pid everything is fine and gets added to the database. I'm very new to php so any help is greatly appreciated. here is my upload script

<?php

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

//This is our size condition
if ($uploaded_size > 650000)
{
echo "Your file is too large.<br>";
$ok=0;
}

//This is our limit file type condition
if ($uploaded_type =="text/php")
{
echo "No PHP files<br>";
$ok=0;
}

//Here we check that $ok was not set to 0 by an error
if ($ok==0)
{
Echo "Sorry your file was not uploaded";
}

//This gets all the other information from the form
$header=$_POST['header'];
$description=$_POST['description'];
$pic=($_FILES['photo']['name']);

// Connects to your Database
mysql_connect("host", "user", "password") or die(mysql_error()) ;
mysql_select_db("mwzzzcyg_news") or die(mysql_error()) ;

//Writes the information to the database
mysql_query("INSERT INTO `index_news` VALUES ('$header', '$description', '$pic')") ;

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

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

//Gives and error if its not
echo "Sorry, there was a problem uploading your file.";
}
print ". To upload another article <a href=http://www.example.com/newsite/uploadform.php> Click Here</a>";
?>

[edited by: eelixduppy at 8:10 pm (utc) on Jan. 16, 2009]
[edit reason] exemplified [/edit]

surrealillusions

11:11 pm on Jan 16, 2009 (gmt 0)

10+ Year Member



Welcome to WebmasterWorld

If the insert is failing, try this and see what errors come up that can help you fix the problem.

//Writes the information to the database
mysql_query("INSERT INTO `index_news` VALUES ('$header', '$description', '$pic')") or die(mysql_error());

:)

riggerz29

12:09 am on Jan 17, 2009 (gmt 0)

10+ Year Member



thanks for welcome and the fast reply, i will try tommorow and post back with the results once i solve this with all your help i need some suggestions on displaying the results in a nicely formated table .

riggerz29

12:21 pm on Jan 17, 2009 (gmt 0)

10+ Year Member



Its seems to have worked fine now so i can now upload correctly the only think i need help with is pulling image location from the db and loading it on my webpage and have the header and description to the right of the image in a nicely formated table or if there is a better option for displaying my news i will give that ago instead.

here is my script so far for out putting the data but its not displaying how i want it to

<?php
// Connects to your Database
mysql_connect("localhost", "user", "password") or die(mysql_error()) ;
mysql_select_db("mwzzzcyg_news") or die(mysql_error()) ;

//Retrieves data from MySQL
$data = mysql_query("SELECT * FROM index_news ORDER BY pid DESC") or die(mysql_error());
//Puts it into an array
while($info = mysql_fetch_array( $data ))
{

//Outputs the image and other data
Echo "<img src=http://www.example.com/newsite/News/".$info['photo'] ."> <br>";
Echo "<b></b> ".$info['header'] . "<br> ";
Echo "<b></b> ".$info['description'] . " <br>";
}
?>

[edited by: coopster at 1:19 pm (utc) on Jan. 17, 2009]
[edit reason] please use example.com, thanks! [/edit]

surrealillusions

6:11 pm on Jan 17, 2009 (gmt 0)

10+ Year Member



Just mess around with the CSS to get the page layout to how you want it.

If theres a specific CSS problem you're having, then please post in the CSS section - [webmasterworld.com...]

:)

riggerz29

7:19 pm on Jan 17, 2009 (gmt 0)

10+ Year Member



ok great so i can use the css to style the output of mysql query results. thanks for all your help i have a look into this