Jump to content

Insert text to DB


Tharanon

Recommended Posts

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

Link to comment
Share on other sites

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!";

 

?>

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.