Creating a query with php.

Joined
Apr 29, 2005
Messages
6,400
Reaction score
130
As I'm making an admin panel for my site, I want to make an option which allows me to edit the text on the home page. I already have a form and such ready, but I now want that when I click "go" it creates a query in mysql with the text I have enterd. And gets overwritten when I enter a new message. I have searched far and wide on google, but I can't find what I need. So if anyone knows how to do it, I would be very grateful if you post it.

Any help is much appreciated.
 
Sure no problem.

You need to have a form wrapped around all of the fields and submit button, make it go to.... submit-news.php.

News File:
PHP:
<link href="style.css" rel="stylesheet" type="text/css" />

<div align="center">Just a simple news system<strong> I</strong> made, cutenews was being a pain in the ass. 
  </p>
 Anyway, enjoy. </div>
</div>
<p> </p>
<form id="form1" name="form1" method="post" action="submit.php">
  <p align="center">News:</p>
  <p align="center">
    <label>
    <textarea name="textarea2" cols="100" rows="10" wrap="physical" class="breadcrumbs">Add your news here, it will automatticly be put into a php file, and then displayed on the homepage.
Aswell, this system, will also delete the previous news.</textarea>
    </label>
  </p>
  <p align="center"> </p>
  <p align="center">
    <label>
    <input name="submit" type="submit" class="none" id="submit" value="Submit News" />
    </label>
  </p>
</form>
<p align="center"> </p>
<p align="center">HTML CODES WORK WITH THIS NEW SYSTEM.

Submit-News.php File:
PHP:
<?php
if(isset($_POST['submit'])) {
$buffer .= "<?php\n";
$buffer .= "\$news = '".$_POST['textarea2']."';\n";
$buffer .= "echo \$news\n";
$buffer .= "?>\n";
$f = fopen('../news.php', 'w');
$fd = fwrite($f, $buffer);
$fe = fclose($f);
if($fd) {
echo('News Submitted<BR>
You can go back to the homepage by clicking <a href=../../index.php>here</a>');
} else {
echo('News submitition failed');
}
}
?>

Then just use
PHP:
<?php
include('news.php');
?>
and the news will show up on the main page.
Tell me if it works for you or not.
 
Beside that, the script is far from being optimised. To have in your script $news = "whatever" and then echo $news that is just non sense.

If you want to do it the static way just save your file as *not* using PHP at all. This will save *a lot* generating time.

So instead of doing it this way:

PHP:
   <?php 
if(isset($_POST['submit'])) { 
$buffer .= "<?php\n"; 
$buffer .= "\$news = '".$_POST['textarea2']."';\n"; 
$buffer .= "echo \$news\n"; 
$buffer .= "?>\n"; 
$f = fopen('../news.php', 'w'); 
$fd = fwrite($f, $buffer); 
$fe = fclose($f); 
if($fd) { 
echo('News Submitted<BR> 
You can go back to the homepage by clicking <a href=../../index.php>here</a>'); 
} else { 
echo('News submitition failed'); 
} 
} 
?>
Why not doing it this way:

PHP:
   <?php 
if(isset($_POST['submit'])) { 
$buffer = $_POST['textarea2']; 
$f = fopen('../news.php', 'w'); 
$fd = fwrite($f, $buffer); 
$fe = fclose($f); 
if($fd) { 
echo('News Submitted<BR> 
You can go back to the homepage by clicking <a href=../../index.php>here</a>'); 
} else { 
echo('News submitition failed'); 
} 
} 
?>
 
Writing to file is ALWAYS slower then writing to a MySQL database. Don't make the mistake of writing the news in 1 field, add a new row for each newsitem.

As to database getting slower with many items: there are millions of posts on RZ, even if you add 5 news items a day it would take you over 800 years to achieve the same amount of posts, and RZ's database is still pretty fast (its the rest of it that isn't at times, but alas :wink:)
 
Back