Jump to content

Little Problem Sending Data Into A Database


elexion
 Share

Recommended Posts

hey guys i've been having a little problem with my uploading page.i'm using a drop down menu,

<?php$query = "SELECT * FROM vaksites		WHERE vaksite != ''";		echo "<select name=\"name\"</select>";  		echo "<option size =30 selected>select</option>";mysql_select_db($query, $db);$result = mysql_query($query, $db) or die('the query failed');while ($row = mysql_fetch_array($result)) 	{	echo($row["vaksite"]);	echo "<option>$row[vaksite]</option>";	}?>

in a form to send of to my database.now the problem is i dont really know how to tell the computer it has to take the selected option from the drop down list into the database too.

<html><head><title>uploading please wait...</title></head><body><?phpinclude("connect.php");$query="INSERT INTO `vaksites`( 		`ID`,										`vaksite`,										`bericht`,										`file`)										 VALUES ( NULL,										 '".$_POST["vaksite"]."',										 '".$_POST["bericht"]."',										 '".$_POST["file"]."');";mysql_query($query,$db) or die("blablabla error");?><meta http-equiv="Refresh" content="5;url= homework.php" /></body></html>

somehow i did manage to insert a message (bericht) into the database but until now i haventbeen able to figure out what exacly i did right heh.thanks for helping out if my code makes any sense :)edit, these 2 codes arent on the same page i have a page that has the forms and when clicked on thesubmit button it sends it of to the page where the second piece of code is. Maybe this could be cause of some isues?

Edited by elexion
Link to comment
Share on other sites

I dont understand what you want exactly, but what I do know is that the form is not the same as your form handler. For example:when you do something like:echo "<select name=\"name\"</select>";the means that there will be a $_POST['name']; in your form handler with the value of the selected option.You also forgot some quotes in the below line. echo "<option size =30 selected>select</option>";make this:echo "<option size ='30' selected>select</option>";You also have to definve values into the <option> tags like below:echo "<option value='".$row[vaksite]."'>$row[vaksite]</option>";Now the handler:

$query="INSERT INTO `vaksites`(		 `ID`,										`vaksite`,										`bericht`,										`file`)										 VALUES ( NULL,										 '".$_POST["vaksite"]."',										 '".$_POST["bericht"]."',										 '".$_POST["file"]."');";mysql_query($query,$db) or die("blablabla error");

The below code contains the vaksites table and the columns in witch you want to insert something

$query="INSERT INTO `vaksites`( `ID`, `vaksite`, `bericht`, `file`)
The below lines contain the value for each column. So NULL will be inserted into ID and $_POST["vaksite"] will be inserted into the vaksite column.
VALUES ( NULL, '".$_POST["vaksite"]."', '".$_POST["bericht"]."', '".$_POST["file"]."');";
please consider this as not save. The $_POST values have to be secured against mysql injection using mysql_real_escape_string() for exampleHope these answers help you :)
Link to comment
Share on other sites

heh thanks a lot Redroest :)i think the changes to my drop down list will fix the undefined index issue.problem is i'm not sure how to get this value into my query and then into my database. i'mprobably missing a few line(s) of code.

Link to comment
Share on other sites

for example:

<?php//ProcesForm contains the query etc.echo '<form action="ProcesForm.php" method="POST">';//Insert a name for the vaksiteecho '<input type="text" name="naam"><br />';//Some kind of message to insert belowecho '<textarea name="bericht" cols="50" rows="5"></textarea><br />';//select a vaksite from the dropdownecho '<select name="vaksite">';echo '  <option value="vaksite1">Vaksite 1</option>';echo '  <option value="vaksite2">Vaksite 2</option>';echo '  <option value="vaksite3">Vaksite 3</option>';echo '</select><br />';echo '<input type="submit" name="SubmitForm" value="Submit">';

No when you press the submit button. It will send the data to ProcesForm.php using POST:.: ProcesForm.php

//Check if the form is sendif(isset($_POST['SubmitForm'])){  //Secure the values to prevent sql injection  $naam = mysql_real_escape_string($_POST['naam']);  $bericht = mysql_real_escape_string($_POST['bericht']);  $vaksite = mysql_real_escape_string($_POST['vaksite']); //connect to databaseinclude("connect.php");//insert stuff into the vaksites table$query="INSERT INTO `vaksites`( 										`vaksite`, 										`bericht`,										`naam`)										 VALUES ( NULL,										 '".$_POST["vaksite"]."', //first value will be inserted to the vaksite column										 '".$_POST["bericht"]."', //second value will be inserted into bericht column										 '".$_POST["naam"]."');"; //last value will be inserted into naam column//execute the query and return error when failedmysql_query($query,$db) or die("Something went wrong while inserting the values into the database");In the above query I left ID blanc because when the table is made correctly into the database, there will be a new unique ID inserted automaticly.By reading your script it contains some dutch words so I expect you to be dutch. I am also dutch. Try to teach yourself to only use englisch words in your php code so that it will be easier for other people to understand what you want. }

Link to comment
Share on other sites

By reading your script it contains some dutch words so I expect you to be dutch. I am also dutch. Try to teach yourself to only use englisch words in your php code so that it will be easier for other people to understand what you want.
very true heh unfortunatly my current project leader wants me to program everything in dutch which blows :)anyway thanks a lot for the help i hope i can figure the rest out without bothering you all :)
Link to comment
Share on other sites

alright so i fixed the query the form now correctly inserts data into the database however it doesnt work that nicely for files. So basicly what i'm asking is what are essential parts to write to make this work? Right now it does accept the file doesnt crash it just doesnt reach the database or show up in the temperary directory.

Link to comment
Share on other sites

//First of all in your mysql database table -you need to have the file column to be a BLOB type. (for example MEDIUMBLOB) = 64 kilobytes//In the form-The <form> tag has to contain: enctype="multipart/form-data" -<input type="hidden" name="MAX_FILE_SIZE" value="2000000"> is necessary to prevent errorsYou can read more about it trough the following link:Uploading files to databaseI myself never upload the files to a database. I always store them in a map and only store the meta-data in the database. It will save time when you have to get information from the database when using many and/or large files

Link to comment
Share on other sites

I myself never upload the files to a database. I always store them in a map and only store the meta-data in the database. It will save time when you have to get information from the database when using many and/or large files
Hmm, yes that seems to be the best thing to do, with meta-data i assume you mean just the file name? Right now my uploading scripts also creates a directory for the files it's suppose to be uploading. Still giving a little error for when the directory already excists but that's solved with a simple if statement atleast i think haha. Only problem is the files are still not getting uploaded there must be something i'm missing.
<!DOCTYPE html	public "-//W3C//DTD XHTML 1.0 strict //EN"	"http://www.w3.org/TR/xhtmll/DTD/xhtmll-strict.dtd"><html><head><title>content</title><?php//a written functioninclude("function.php");date_switcher();//the menu barinclude("menu.php");//and finally the connection to the database.include("connect.php");?></head><body><form method="POST" enctype="multipart/form-data" action="uploadscript.php" name="input" >Select a file you wish to upload<br /><?phpecho '<select name="listOfOptions">';echo '  <option name="select">select</option>';echo '  <option value="content">content</option>';//there's more but for this small example i removed those no need for a big listecho '</select>';?><!--<label for="file">fileName:</label>-->//this line below was made to solve a lay out issue i was having i made it hidden so not sure if it can be causing any bugs<input type="hidden" name="file" id="file" /><br /><br /><label for="file">fileName:</label>//yeah the naming is kinda bad <input type="file" name="file" value="file" /><br />leave a message<br /><textarea rows="5" cols="40" name="message" value="message" ></textarea><br /><input type="hidden" name="send"/><hr><input type="submit" name="send" value="insert" /><input type="reset" name="reset" value="reset" /></form></body></html>

Link to comment
Share on other sites

A main advantage of moving file data from the filesystem to the database is portability, so you do not have to worry about backing up or replicating multiple structures. I wonder if anyone has done any benchmarks between retrieving large BLOBs from a database and reading a file?

Link to comment
Share on other sites

So let me get this straight. If i upload a file should it apear in my C:\wamp\tmp folder? if so should the actual file be there or would it be stored in some crazy session file?

Link to comment
Share on other sites

It will appear in PHP's upload_tmp_dir (which is "c:/wamp/tmp" if you are using WampServer). It will be as an actual file, however the name will be something random.

Link to comment
Share on other sites

It will appear in PHP's upload_tmp_dir (which is "c:/wamp/tmp" if you are using WampServer). It will be as an actual file, however the name will be something random.
yes using wampserver however, for some reason i dont get any files in my tmp folder after uploading something. Can this be a problem with wampserver itself?
Link to comment
Share on other sites

They will disappear quickly (I think it's when the script ends), you need to move the file to a more permanent location using move_uploaded_file() (or read the contents and store that in the DB, etc.).

Link to comment
Share on other sites

They will disappear quickly (I think it's when the script ends), you need to move the file to a more permanent location using move_uploaded_file() (or read the contents and store that in the DB, etc.).
so this would be the general idea?
<!DOCTYPE html	public "-//W3C//DTD XHTML 1.0 strict //EN"	"http://www.w3.org/TR/xhtmll/DTD/xhtmll-strict.dtd"><html><head><title>uploading please wait...</title></head><body><?php//at the moment not using this variable i was just testing if i could use it to give the directory$dir_base = "/C:/wamp/www/uploadTryOuts/upload/";if ($_FILES["file"]["error"] > 0)  {  echo "Error: " . $_FILES["file"]["error"] . "<br />";  }else  {  echo "Upload: " . $_FILES["file"]["name"] . "<br />";  echo "Type: " . $_FILES["file"]["type"] . "<br />";  echo "Size: " . ($_FILES["file"]["size"] / 5024) . " Kb<br />";  echo "Stored in: " . $_FILES["file"]["tmp_name"];  }move_uploaded_file($_FILES["file"]["name"], "/upload/");?> <meta http-equiv="Refresh" content="15;url= index.php" /><h1>your files has been uploaded</h1></body></html>

Edited by elexion
Link to comment
Share on other sites

Almost, but you're using the wrong values for move_uploaded_file() - have a look at http://www.w3schools.com/PHP/php_file_upload.asp.
thanks a milion for the help Synook and definalty for the patience heh. Right now files are being uploaded to the directory my script sends them to butthe file names dont show up in the database for example "picture.png". Question is is the error in the code below which is my uploading script.
<html><head><title>uploading please wait...</title></head><body><?phpinclude("connect.php");  //Secure the values to prevent sql injection  $message= mysql_real_escape_string($_POST['message']);  $subPage = mysql_real_escape_string($_POST['subpage']);$query="INSERT INTO `table`( 		`ID`,										`subpage`,										`message`,										`upload`,										`date`)																				VALUES ( NULL,										'".$_POST["subpage"]."',										'".$_POST["message"]."',										'".$_POST["file"]."',										'".$_POST["date"]."');";										mysql_query($query,$db) or die("there was a error inserting the data into the database");mkdir("subpages/uploads/" .$_POST["subpage"]);if ($_FILES["bestand"]["error"] > 0)  {  echo "Error: " . $_FILES["upload"]["error"] . "<br />";  }else  {  echo "Upload: " . $_FILES["upload"]["name"] . "<br />";  echo "Type: " . $_FILES["upload"]["type"] . "<br />";  echo "Size: " . ($_FILES["upload"]["size"] / 1024) . " Kb<br />";  echo "Stored in: " . $_FILES["upload"]["tmp_name"];  }move_uploaded_file($_FILES["upload"]["tmp_name"],	  "subpages/uploads/" . $_POST["subpage"] . $_FILES["upload"]["name"]);	  	if (file_exists("subpages/uploads/" . $_POST["subpage"] . $_FILES["upload"]["name"]))	  {	  echo $_FILES["upload"]["name"] . " already exists. ";	  }	else	  {	  move_uploaded_file($_FILES["upload"]["tmp_name"],	  "subpages/uploads/" . $_FILES["upload"]["name"]);	  echo "Stored in: " . "uploads/" . $_POST["subpage"]["upload"];	  }  ?><meta http-equiv="Refresh" content="10;url= uploadingForm.php" /></body></html>

Edited by elexion
Link to comment
Share on other sites

quick bump before the weekend :)probably a simple one:How would i go about making a page to update for example text that is displayed from i database.I know i should use the Mysql update function but should i make a button or something with a query?

Edited by elexion
Link to comment
Share on other sites

Is the upload field where the file name is suppose to go? If so:

$query="INSERT INTO `table`(		 `ID`,										`subpage`,										`message`,										`upload`,										`date`)																				VALUES ( NULL,										'".$_POST["subpage"]."',										'".$_POST["message"]."',										'".$_FILES["upload"]["name"]."',										'".$_POST["date"]."');";

The way I would of done it though, is upload the picture first then insert the information into the database. What happens if the image fails to upload? The database still has an entry, but without an file name.

quick bump before the weekend :)probably a simple one:How would i go about making a page to update for example text that is displayed from i database.I know i should use the Mysql update function but should i make a button or something with a query?
You can do it both ways. I personally make a button if the update function is going to be used a lot. Edited by RahXephon
Link to comment
Share on other sites

You can do it both ways. I personally make a button if the update function is going to be used a lot.
So, I'll make a button like "edit this" and send them of to a form where they insert the new data and i use to update method there?PS, sorry if that's a stupid way to put it or anything monday just isnt my day :)
Link to comment
Share on other sites

ok, a tuesday feeling much sharper already lol, Time for some brainstorming; I have a long list of messages and next to every message is the "edit me" button now i think i should send the visitors to a new form and let them insert the new date, and use the update function WHERE the ID matches the message that was clicked, but i'm not completely sure if this would work and how exacly, would i just use $_POST to get the ID number?on the other hand i was thinking, What if i make a form in which the user can just select a ID number from a list without clicking the "edit me" button next to every message on the long list. I hope this doesnt sound to confusing hehthanks for the feedback guys appreciate it.

Link to comment
Share on other sites

You could construct the button's hyperlink reference to have a querystring indicating the relevant records primary key, and use the $_GET superglobal array to retrieve the value on the following page.

...echo "<a href=\"edit.php?id={$row['id']}\">Edit me</a>";...

Link to comment
Share on other sites

You could construct the button's hyperlink reference to have a querystring indicating the relevant records primary key, and use the $_GET superglobal array to retrieve the value on the following page.
...echo "<a href=\"edit.php?id={$row['id']}\">Edit me</a>";...

thanks Synook the links to the update form are written i'm positive i can handle from there :).Unfortunatly downloading files from server...the terror heh not sure how to go about this onedo i use the fopen() function? and if so how do i browse through directory's?
Link to comment
Share on other sites

You can retrieve the contents of files using file_get_contents(), to list the contents of a directory you can use glob().

Link to comment
Share on other sites

You can retrieve the contents of files using file_get_contents(), to list the contents of a directory you can use glob().
well i do get the content of the files including a ton of random symbols heh. but i want people tobe able to click on the file name in the list and be able to download it or simplely just read it out of the site.So far i've written this:
$download = fopen($data["file"], "r");

it's not quite working yet i need to look for the files in another directory and for the file that is in the same directory as the php file theoutput is for example Resource id #10.

Edited by elexion
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...