FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


simple php to mysql form doesn't work





rein
Hey people,

a simple form to submit things to my database is proving to be more difficult than i had hoped.
The form looks something like this:
Code:

<form action="toevoegen.php" method="POST">
Title<input size="50" maxlength="250" type="text" name="titel"><br />
Message<input size="50" maxlength="250" type="text" name="message">< br />
By<input size="50" maxlength="250" type="text" name="by"><br />
<input type="Submit" name="send" value="Add">
         &nbsp;&nbsp;&nbsp; <input type="Reset" value="clear">
</form>


And the toevoegen.php file looks like this:
Code:

<?php 

include('Connections/scoutwrite.php');

      $scoutwrite = mysql_pconnect($hostname_scoutwrite, $username_scoutwrite, $password_scoutwrite)
      or trigger_error(mysql_error());

      mysql_select_db($database_scoutwrite)
        or die ("Kan geen database selecteren!");


      $query = "INSERT INTO $tabel_scoutwrite (title, message, by)
         VALUES ('" . $_POST['title'] . "', '" . $_POST['message'] . "', '" . $_POST['by']."', '";
      mysql_query($query);

      mysql_close();
?>

The scoutwrite.php file holds my connection variables:
Code:
<?php
$hostname_scoutwrite = "localhost";
$database_scoutwrite = "rein_scouts";
$username_scoutwrite = "rein_main";
$password_scoutwrite = "*******";
$tabel_scoutwrite = "berichten";
?>


The form adds rows to my table with the desired data in them, but my browser goes -and stays- at toevoegen.php when i press the send buttonw of my form. How do i fix this? I tried a <?php header("location:index.php"); ?> but that gives an error.
hexkid
rein wrote:
The form adds rows to my table with the desired data in them, but my browser goes -and stays- at toevoegen.php when i press the send buttonw of my form. How do i fix this? I tried a <?php header("location:index.php"); ?> but that gives an error.

What error? The "output already started" (or similar) one?

The header() is one of the usual ways to accomplish what you want.
You need to make sure there is absolutely no output before the header function is called (make sure that "<?php" are the first characters in the toevoegen.php file with no empty space before it)
rein wrote:
Code:

<?php

include('Connections/scoutwrite.php');

Maybe that first blank line was an effect of posting in the FRIHOST forums, but that code should be
Code:
<?php

include('Connections/scoutwrite.php');


Also you might want to increase the level of error reporting in your scripts by adding these lines right after your first <?php tag
Code:
error_reporting(E_ALL);
ini_set('display_errors', '1');
rein
you were right; i had a white line before my php-tags in the scoutwrite file. Thanks for the tip Cool

now with register globals turned off, how do i pass variables around without having them visible in the url of my page?

For example, now that the form is filled in i don't want to show it anymore.
hexkid
rein wrote:
with register globals turned off, how do i pass variables around without having them visible in the url of my page?

Use session variables.

rein wrote:
For example, now that the form is filled in i don't want to show it anymore.


Code:
<?php // index.php
session_start(); // you need to start the session in *every* script that uses session variables
if (!isset($_SESSION['form_already_dealt_with'])) {
  echo <<<HTML_FORM
<form action="toevoegen.php" method="POST">
Title<input size="50" maxlength="250" type="text" name="titel"><br />
Message<input size="50" maxlength="250" type="text" name="message">< br />
By<input size="50" maxlength="250" type="text" name="by"><br />
<input type="Submit" name="send" value="Add">
         &nbsp;&nbsp;&nbsp; <input type="Reset" value="clear">
</form>
HTML_FORM;
}
// rest of index.php
?>

Code:
<?php // toevoegen.php
session_start(); // you need to start the session in *every* script that uses  session variables

include('Connections/scoutwrite.php');

// ... the rest of the script


$_SESSION['form_already_dealt_with'] = true;
header('Location: http://www.yoursite.com/index.php');
exit('Redirected to the <a href="http://www.yoursite.com/index.php">main page</a>.');
?>


When somebody accesses index.php for the first time, $_SESSION['form_already_dealt_with'] is not set and the form will be printed. When they submit and the information has been saved, the toevoegen.php script sets $_SESSION['form_already_dealt_with'] and redirects to index.php. Now, in index.php, the session variable is set and the form is not printed.
Related topics
PHP, MySQL...
Simple PHP Navigation
Need some php/mysql dlls
What is useful way in this php+mysql problem?
A very good PHP MySQL Tutorial
 EASY 40 frih$ IF YOU KNOW PHP+MySQL 
Anyone care to help me with a simple PHP Form ?
Project to join - php,mysql??
PHP/MYSQL Poll?
Free PHP/MySQL login script
How to build simple PHP upload form
Php mysql and stuff
need help in a simple php register script based on flat file
Need a Registration form PHP/Mysql with email confirmation
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.