PDA

View Full Version : php newbie seeking help on creating php to store and retrieve data in table...


marvc
09-16-2002, 03:31 PM
I do not know PHP nor do I see myself becoming a full fledge php programmer anytime soon, but I am trying to follow along wiith this tutorial on adding blobs to a database:
http://www.devarticles.com/content....cleId=68&page=5
and I'm SOS = stuck on stupid...
I've created a form that allows users to upload data or files to a table I've created in a database. The table contains the following fields:
id
author
city
state
email
title
copyright
submitdate
category
filename
filedata mediumblob
filetype

Can someone assist me in modifying this so that it allows the user to submit their data or upload a file, store that info into the database and then post it for public viewing in either a link or table format? These would be poems , essays, or short stories.
I've copied the text for the getphiles.php and tried to modify it but kept getting errors.

Again I don't know php, but I have been reading, and I still don't get it.
Any help on this is appreciated.

Spookster
09-16-2002, 04:10 PM
Instead of storing the file in the database it is usually a better idea to just store the path to the file in the database and store the files in some directory.

marvc
09-16-2002, 04:44 PM
ok you just confused the heck out of me. I thought the point was to have everything stored inside of the database so as to keep from having to manage a slew of files and directories?

Spookster
09-16-2002, 07:29 PM
By storing the files in the database you will increase the database size tremendously. Queries can become much longer. The point to a database is not to store files but to store information in an organized fashion. When you store the path to the file you simply need to retrieve that path and filename which will then become the link in your page. When the files are uploaded you can control the naming scheme of the files and where they are stored so things won't get messy. When the person uploads the file you can rename the file to some naming scheme that will allow for growth.

marvc
09-16-2002, 07:53 PM
Meant to post back to your original response. Turns out after reading over at mysql.com I get what you're saying about the database. Another strange thing is that this is exactly what I want to be able to do. Have users upload files and then make them available via a link that points to the file. Sounds simple in theory, but my application sucks.
Anybody know how I can do this with the table below?

Spookster
09-16-2002, 10:48 PM
For the filedata field change it to varchar and rename it to filepath.

marvc
09-17-2002, 12:35 AM
Man I'm burned out.OK...This is what I've come up with from my readings thus far. Again this is my first attempt at ever creating a database using mysql & php, so be king.
As a reminder I'm trying to allow users to submit files or text to a database to be made available for public viewing.
I've created a submitform that has the fields I want stored.
I've also created a database = dbnubiint and a table for this particular page = nuwoez. I've also created a user = dbnubiint_user.
I take this code and add it to my submitform under <body>
and get parse errors. The exact error is: Parse error: parse error, unexpected T_IF in c:\program files\php home edition 2\www\nuwoez\submitform.php on line 49.
The Code:
<?php
// If the user wants to add a file
if (isset($addfile)):
?>
<!--Form to submit filedata-->
<FORM ACTION="<?php echo($PHP_SELF); ?>" METHOD=POST>
<P>Enter your work here:<BR>
<TEXTAREA NAME="filedata" ROWS=10 COLS=40WRAP>
</TEXTAREA><BR>
<INPUT TYPE=SUBMIT NAME="submitform" VALUE="SUBMIT">
</FORM>
<!---End of Form-->

<?php
else:
// Connect to the database server
$dbcnx = @mysql_connect("localhost", "dbnubiint_user", "dbusernu");
if (!$dbcnx) {
echo( "<P>Unable to connect to the " . "database server at this
time.</P>" ); exit();
}
// Select the nubiint database
if (! @mysql_select_db("dbnubiint") ) {
echo( "<P>Unable to locate the Nubiint " . "database at this time.</P>" );
exit();
}
// If a File has been submitted,
// add it to the database.
if ("SUBMIT" == $submitform) {
$sql = "INSERT INTO nuwoez SET " .
"Filedata='$Filedata', " . "Author='$Author', " .
"City='$City', " .
"State='$State', " .
"Email='$Email', " .
"Title='$Title', " .
"Copyright=DATE()", " .
"Submitdate=CURDATE" .
"Category='$Category', " .
"Filename='$Filename', " .
"Filetype='$Filetype', " .
if (mysql_query($sql)) {
echo("<P>Your file has been added.</P>");
} else {
echo("<P>Error adding submitted file: " . mysql_error() .

"</P>");
}
}
echo("<P> Here are all the file " .
"in our database: </P>");
// Request the text of all the files
$result = mysql_query(
"SELECT Filedata FROM nuwoez");
if (!$result) {
echo("<P>Error performing query: " . mysql_error() . "</P>");
exit();
}
// Display the text of each file in a paragraph
while ( $row = mysql_fetch_array($result) ) { echo("<P>" . $row["Filedata"] .

"</P>");
}
// When clicked, this link will load this page
// with the file submission form displayed.
echo("<P><A HREF='$PHP_SELF?addfile=1'>" .
"Add a File!</A></P>");
endif;
?>

My submitform contains both a file upload box and a textarea box. Not sure if this has anything to do with anything, just thought I'd post it.
This is modified code from the Jokes database tutorial mysql.com. Again my hands on knowledge is entry level, so please don't assume I know what I'm doing.
I appreciate any assistance I can get from the list.
TIA

Spookster
09-17-2002, 02:46 AM
Your if/else statements should be structured as:


<?php

if(blah){

?>

stuff


<?php

}
else {

?>

stuff


<?php

}

?>

marvc
09-18-2002, 04:55 AM
I think I've made some serious progress on this issue as I'm now able to connect to the db, submit the article and see it posted in the table in the db.
Can someone take a look at this page, click on submit writings, click the submit button, and then click the display button. I get an error that states the following:
The query: SELECT * FROM nuwoez WHERE type='" did not return any data.
This comes after trying to have it so that the submitted data shows up under the category that was selected. So if userA submits a poem and selects Poetry from the category list, the poem is stored under the Poetry list and userA can go to poertry, and poetry only, to view that poem. In other words, the submitted data goes to the category that was selected and can only be viewed in that category. Clicking one of the links in the menu to the left either displays that data or a link to that data. The way I have it set now, it displays the data.
Here's the code from my displaycat.php file.
<table width="100%">
<?
require("conn.inc");
?>
<?
$type = $_POST['type'];
$sql = "SELECT * FROM nuwoez WHERE type='$type'";
$result = mysql_query($sql);
$num_results = mysql_num_rows($result) or die ("The query: '$sql' did not return any data");
for ($i=0; $i<$num_results; $i++)
{
$row = mysql_fetch_row($result);
$author = mysql_result($result, $i, "author");
$city = mysql_result($result, $i, "city");
$state = mysql_result($result, $i, "state");
$email = mysql_result($result, $i, "email");
$title = mysql_result($result, $i, "title");
$copyright = mysql_result($result, $i, "copyright");
$submitdate = mysql_result($result, $i, "submitdate");
$filename = mysql_result($result, $i, "filename");
$filedata = mysql_result($result, $i, "filedata");
echo "<tr bgcolor=white><td> </td>";
echo "<td>" . $author ."</td>";
echo "<td>".$city . "</td>";
echo "<td>".$state . "</td>";
echo "<td>".$email . "</td>";
echo "<td>".$title . "</td>";
echo "<td>".$copyright . "</td>";
echo "<td>".$submitdate . "</td>";
echo "<td>".$filename . "</td>";
echo "<td>".$filedata . "</td></tr>";
}
?>
</table>

I've tried and can't seem to get this $type variable to act right.
Also can anyone tell me why my upload button doesn't seem to work?
<TEXTAREA NAME="filedata" rows="20" cols="55" WRAP>
</TEXTAREA><BR>
<INPUT TYPE=SUBMIT NAME="Submit" VALUE="Submit">
<input type=reset name="clear" value="Clear">
</center><br>

ANY help on this is appreciated.

mordred
09-19-2002, 01:38 AM
What does var_dump($_POST); tell you about the values of the POST variables? Does 'type' show up in there?

Make sure your PHP version is 4.1 at least.

You lost on on your second question though. What do you mean with upload button? And what should it be expected to do? :confused:

marvc
09-19-2002, 04:00 AM
I'm using php 4.2.2 on this server. The upload button on that page should work like the submit button in that it should allow the user to upload a file, if they so choose to, and that file gets placed in the table in the db.
My eyes are dotted like needles and I see dots all over the screen.
$type = $_POST['type'];

It doesn't post the article like it should.

mordred
09-19-2002, 02:02 PM
Have you also set enctype="multipart/form-data" like in the example at http://www.php.net/manual/en/features.file-upload.php ?