Forum Moderators: coopster

Message Too Old, No Replies

Inserting news to database.

         

electricocean

1:26 am on Apr 26, 2005 (gmt 0)

10+ Year Member



Hi,
I got this code to insert news to a database. I would like to create an html code using the values in the textarea and then post it into the database. heres my code:


$username="username";
$password="password";
$database="database";

$date = $_POST['date'];
$news = $_POST['news']
$html = "<p><blockquote>$date-</blockquote><br>$news</p>";
$posting = $_POST['$html'];

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database.");

$query = "INSERT INTO news VALUES ('','$posting')";
mysql_query($query);

mysql_close();


How could I improve my code and make it work.
I have a file that retrieves the data from the database.

Thanks,
electricocean

ironik

2:46 am on Apr 26, 2005 (gmt 0)

10+ Year Member



The problem appears to be this part:

$date = $_POST['date'];
$news = $_POST['news']
$html = "<p><blockquote>$date-</blockquote><br>$news</p>";
$posting = $_POST['$html'];

If you are just trying to put information into the database try this:

$date = $_POST['date'];
$news = $_POST['news'];
$posting = "<p><blockquote>$date-</blockquote><br>$news</p>";

You could probably improve your query to make it more readable by specifying what data goes where:

$query = "INSERT INTO news (field1, field2) VALUES ('','$posting')";

Is there any reason you've specified blank data to go into the first field? If it's always going to be blank you can tell MySQL to give it a default value, that way you don't have to explicitly set it each time.

ramoneguru

2:49 am on Apr 26, 2005 (gmt 0)

10+ Year Member



$username="username";
$password="password";
$database="database";

$date = $_POST['date'];
$news = $_POST['news']
$html = "<p><blockquote>$date-</blockquote><br>$news</p>";
$posting = $_POST['$html'];

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die(mysql_error());

$query = ("INSERT INTO news (Cloumn1, Column2) VALUES ('','$posting')");

mysql_close();

I assume that another form is creating the POST data (date, news, html) and that $posting is the only variable on this "processing" page.

Column1 and Column2 are the column names of your "news" table, but for Column2 make sure that its type is something like VARCHAR(100) or something.
--Nick

electricocean

3:45 am on Apr 26, 2005 (gmt 0)

10+ Year Member



$html and $posting are on this page. Only the form calls up date and news

electricocean

ironik

4:17 am on Apr 26, 2005 (gmt 0)

10+ Year Member



I'm not sure I understand you. To explain a little further on my last post:

$news = $_POST['news']

This part is missing the terminating ;

$posting = $_POST['$html'];

This is not a valid way of calling an array key and will give syntax errors, plus your $html variable contains data that isn't really of an 'array key type'.

electricocean

1:51 am on Apr 27, 2005 (gmt 0)

10+ Year Member



What I wanted to do is to make a pre-made html code that has the two variables date and news added together with formatting to upload it to the database and just retrieve that and have the html alredy set.

Is there a better way to do that only takes up 1 field?

electricocean

electricocean

3:43 am on Apr 27, 2005 (gmt 0)

10+ Year Member



could I do this:

$date = $_POST['date'];
$news = $_POST['news']
$html = "<p><blockquote>$date-</blockquote><br>$news</p>";

$query = "INSERT INTO news VALUES ('','$html')";
mysql_query($query);

?

any help please?

electricocean

ironik

4:47 am on Apr 27, 2005 (gmt 0)

10+ Year Member



Yes, you can do that. It should work fine depending on how you've got your database table setup. Are you having problems with it?

For your database you should have 2 fields. One should be your unique primary key (integers are best) and the other should be your html content. A better way to do it would be to store it in 3 fields (primary key, date and news), that way later on down the track you can enact on data by date (deleting old records etc).

electricocean

10:46 pm on Apr 27, 2005 (gmt 0)

10+ Year Member



I have some q's about the code:

1) If I did the code like that and preset the html would it come out as html on the page I am accessing the database?

2) In the News post could I add html to display images in for the news? would that work?

thanks,
electricocean

ironik

11:06 pm on Apr 27, 2005 (gmt 0)

10+ Year Member



You can store pretty much anything you like in the database as long as you escape characters that might cause problems (use addslashes() or mysql_real_escape_string() ).

When you retrieve the data use stripslashes() to remove any escapes on characters and then output it to the screen.

electricocean

4:28 am on Apr 29, 2005 (gmt 0)

10+ Year Member



where would I put 'mysql_real_escape_string()'?

here: $posting = mysql_real_escape_string($html);

or here: $query = "INSERT INTO news (news) VALUES ('mysql_real_escape_string($posting)')";

thanks,
electricocean

ironik

4:58 am on Apr 29, 2005 (gmt 0)

10+ Year Member



Either way is fine, but in the second example you need to concat the string with the period char when using functions as the parser only picks up on variables stored in a double quoted string:

$posting = mysql_real_escape_string($html);

$query = "INSERT INTO news (news) VALUES ('" . mysql_real_escape_string($posting) . "')";