Jump to content

Can someone show me a very simple way

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
6 replies to this topic

#1
thesquirrel16

thesquirrel16

    Newbie

  • Members
  • PipPip
  • 12 posts
Hi, can anyone show a very simple way to upload values into a database by clicking a button in a php file?

#2
v0id

v0id

    Retired

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,936 posts
It's actually pretty easy to upload files, using PHP.
First of all, you need a form. The form have to have a enctype set to multipart/form-data. The input-tag you're using is gonna have file as type.
<!-- upload.php (could even be upload.htm, because here's no PHP) -->

<form method="post" action="uploaded.php" enctype="multipart/form-data">
    <b>Your file:</b><br />
    <input type="file" name="file" /><br />
    <input type="submit" value="Upload file" />
</form>
Now the easiest part is over, and we're going to use PHP - to do the rest.
I'll show you the simplest way to do it, you can then continue further with the code.
$upload_path = "uploads/";
$upload_path = $upload_path . basename($_FILES["file"]["name"]);
$status = move_uploaded_file($_FILES["file"]["tmp_name"], $upload_path);

if($status)
    echo "<b>" . basename($_FILES["file"]["name"]) . "</b> uploaded!<br />";
else
    echo "Error while uploading <b>" . basename($_FILES["file"]["name"]) . "</b><br />";
Try using Google, or check out www.php.net.

#3
thesquirrel16

thesquirrel16

    Newbie

  • Members
  • PipPip
  • 12 posts
Thank you for the reply, I have a couple questions: How does the code know what it is submitting when the button is clicked?

is the second part of code the upload.php file? or does that go into the base file?

I am trying to have a string of texts from a textbox uploaded into a database table when the submit button is clicked. This shows how to upload a file? <-- big newb here, you gotta type slow for me =p

#4
thesquirrel16

thesquirrel16

    Newbie

  • Members
  • PipPip
  • 12 posts
Ok ok, on the <form method="post" action="uploaded.php" enctype="multipart/form-data"> line, the action= "uploaded.php" actually has nothing to do with the data being uploaded? its just the page that is called after the button is clicked?

And the post method, how does it know what to post?

And lol if you have more than one button how do you specify different actions for each?

#5
v0id

v0id

    Retired

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,936 posts
If the input-tag's type is set to file, like in my example, the browser will create an input-box AND a button, which the user press and a pop-up with all you files in it, and then the user can choose. The only thing the forms does is sending the filename to the next-page, nothing else.
All the uploading stuff is in the PHP-file.

"thesquirrel16" said:

action= "uploaded.php" actually has nothing to do with the data being uploaded? its just the page that is called after the button is clicked?
Yes.

#6
thesquirrel16

thesquirrel16

    Newbie

  • Members
  • PipPip
  • 12 posts
Ah ok, that clears things up a lot, one last thing, if I want to pass a value such as $Title to my updated.php to upload into a database, how do i do that?

#7
v0id

v0id

    Retired

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,936 posts
If you're gonna insert something into some database, you aren't going to do it in that way. Most databases, if not all, have some kind of API or framework to communicate with PHP - or another programming language. F.ex. PHP and MySQL.

I've only experience with PHP and the MySQL-database, but try take a search with Google or some other search engine to find information about the database you're using and/or programming language.