Tharanon Posted June 23, 2009 Share Posted June 23, 2009 Hi im new here, and id like to start off with an urgent question of mine, which bothers me since a while. What im trying to do is, to write a text in a normal form textfield, send this to another page where the previously written text is put into a variable and then inserted into my before created database. later all this information should be displayed on another website. The code looks like this: // FORM CODE <form action="insert_db_home.php" method="post"> <br /> <br /> <table width="600" border="0" cellpadding="0" cellspacing="0"> <tr> <td width="160" height="25" align="left" valign="top" scope="col">Your Text: </td> <th colspan="2" rowspan="2" align="left" valign="top" scope="col"><textarea name="written" rows="7" cols="45"></textarea></th> </tr> <!-- WRITTEN --> <tr> <td width="160" rowspan="2" align="left" valign="top" scope="col"> </td> </tr> <tr> <th width="67" height="25" align="left" valign="top" scope="col"><input type="submit" name="submit" value="Submit" /></th> <th width="367" align="left" valign="top" scope="col"><input type="reset" name="reset" value=" Reset " /></th> </tr> </table> </form> which is indeed not that complex. then ill send all this to "insert_db_home.php". the code looks like the following: <?php // FETCH INFORMATION FROM upload.php $written = $_REQUEST['written']; // ESTABLISH CONNECTION WITH mySQL SERVER mysql_connect("localhost", "xxxxxx", "xxxxxx") || die(mysql_error()); mysql_select_db("35345-0") || die(mysql_error()); // mysql_query("INSERT INTO home (text) VALUES("$written") ") || die(mysql_error()); ?> this should insert my stuff ive written in the form into the db "home". yet ill either get the error message "Unknown column 'test' in 'field list'" by entering simply the word "test" in the form or when written more something like "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'this test)' at line 2". at last all this should be output (as input before) through this code: <?php // Make a MySQL Connection mysql_connect("localhost", "xxxxxx", "xxxxxx") or die(mysql_error()); mysql_select_db("35345-0") or die(mysql_error()); $query = "SELECT * FROM home"; $result = mysql_query($query) || die(mysql_error()); // store the record of the "example" table into $row $row = mysql_fetch_array( $result ); // Print out the contents of the entry echo $row['text']; ?> if anybody could help me out id be so thankful. it bugs me since a while and im really clueless. i removed the username / password for accessing the db server though. i have those and they are correct. cheers Quote Link to comment https://forums.phpfreaks.com/topic/163416-insert-text-to-db/ Share on other sites More sharing options...
Tharanon Posted June 23, 2009 Author Share Posted June 23, 2009 oh and this is the script i used to create the table. <?php mysql_connect("localhost", "xxxxxx", "xxxxxx") || die(mysql_error()); echo "Connection to MySQL Server | SUCCESSFUL |<br>"; mysql_select_db("35345-0") || die(mysql_error()); echo "Connection to Database | SUCCESSFUL |"; // Create a MySQL table in the selected database $table_name = "home"; // "home" IS TABLE NAME // \/ mysql_query("CREATE TABLE home( id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(id), text VARCHAR(5000))") // <--- "text" IS FOR TEXTFIELD || die(mysql_error()); echo "<br>Table ".$table_name." has been created!"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/163416-insert-text-to-db/#findComment-862217 Share on other sites More sharing options...
gassaz Posted June 23, 2009 Share Posted June 23, 2009 TEXT is a reserved word, if you can change it do it or use `text` Quote Link to comment https://forums.phpfreaks.com/topic/163416-insert-text-to-db/#findComment-862226 Share on other sites More sharing options...
Tharanon Posted June 23, 2009 Author Share Posted June 23, 2009 TEXT is a reserved word, if you can change it do it or use `text` omg awesome. i didnt know that =) im gonna try it out right now. thx alot Quote Link to comment https://forums.phpfreaks.com/topic/163416-insert-text-to-db/#findComment-862235 Share on other sites More sharing options...
Tharanon Posted June 23, 2009 Author Share Posted June 23, 2009 Unfortunately the change didnt do anything. i still get the error Unknown column 'test' in 'field list' I have no idea what it could be : / Quote Link to comment https://forums.phpfreaks.com/topic/163416-insert-text-to-db/#findComment-862279 Share on other sites More sharing options...
xtopolis Posted June 24, 2009 Share Posted June 24, 2009 Use single quotes for the VALUES() section. INSERT INTO yourTable (`text`) VALUES ('$written'); Quote Link to comment https://forums.phpfreaks.com/topic/163416-insert-text-to-db/#findComment-862431 Share on other sites More sharing options...
adam84 Posted June 24, 2009 Share Posted June 24, 2009 Since you are inserting user data into your database, i would suggest you escape the data prior to inserting it. If you don't, the chance of something happening to your database will be much greater. Quote Link to comment https://forums.phpfreaks.com/topic/163416-insert-text-to-db/#findComment-862768 Share on other sites More sharing options...
Tharanon Posted June 25, 2009 Author Share Posted June 25, 2009 well thx for your help guys. i finally got it running. the only new problem now is that my script (and i dont know why the hell its doing that) adds empty fields into my table. for example. Insert your text here: [blablabla text] UPLOAD this text should be written inside my table in a single field. sometimes this works but sometimes im getting weird empty fields between 1 or even 20 at a time. the written text then is thrown into a random field instead of the first one. so if i wanna use my same upload form again such as: Insert your text here: [NEW blablabla text] UPLOAD then it should do the following: the text which SHOULD BE in the very first field (1,1) will be overwritten by the new text. only complication is that i dont know where to use my UPDATE function. in the same script as i use the INSERT INTO function or somewhere else? the script from above didnt really change. how do i select a specific field (like 1,1) and overwrite the data in it. like the function used for php scripts and files - fopen(test.txt, 'w') when opening the file and later writing in it, it will delete all existing data and start with the pointer at the beginning. i wanna have exactly this for my mysql script. sorry to bother that much, but maybe it is simpler than i think, and i know in this forums are smart guys who could help me out Quote Link to comment https://forums.phpfreaks.com/topic/163416-insert-text-to-db/#findComment-863233 Share on other sites More sharing options...
fenway Posted June 27, 2009 Share Posted June 27, 2009 If you're having layout problems, this is the wrong forum. Show us the INSERT/UPDATE query -- nothing more -- and we'll let you know. Quote Link to comment https://forums.phpfreaks.com/topic/163416-insert-text-to-db/#findComment-864763 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.