View Single Post
  #1 (permalink)  
Old 01-30-04, 02:45 PM
Scoobler Scoobler is offline
Newbie Coder
 
Join Date: Jan 2004
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
PHP Form to update a MySQL database?

Hi people, I have a problem, I have created a page that I want to use to insert data into a MySQL database, the database name is riflemans, and the table in question is called whatson. i created the following page to use to update the entries:

PHP Code:

<?php

//This is, if you like the index page, it will be shown by default:
function indi() {
echo 
'<title>--::The Riflemans Arms - Events Entry Page::--</title>';
echo 
'<p></p>';
echo 
'<p align=center><font size=6><u>Riflemans Arms</u></font></p>';
echo 
'<p></p>';
echo 
'<p align=center><font size=4>Events Entry Page</font></p>';
echo 
'<p></p>';
echo 
'**In order to add an event you will need to have the password. If you forget ';
echo 
'the password, please contact the <a href=mailto:woody@scoobler.com>WebMaster</a>**';
echo 
'<p></p>';
echo 
'<form action=`events.php?function=process` method=POST>';
echo 
'<table>';
echo 
'<tr><td>';
echo 
'<p align=center><font size=4>Events Entry Page</font></p>';
echo 
'</tr></td>';
echo 
'<tr><td>';
echo 
'User Password: <i><a href=events.php?function=password>Help</a></i>';
echo 
'</td><td>';
echo 
'<input type=password name=PASSWORD size=33>';
echo 
'</td></tr>';
echo 
'<tr><td>';
echo 
'<b>Event Date: <i><a href=events.php?function=date>Help</a></i></b>';
echo 
'</td><td>';
echo 
'<input type=text name=DATE size=33>';
echo 
'</td></tr>';
echo 
'<b>Event Name: <i><a href=events.php?function=name>Help</a></i></b>';
echo 
'</td><td>';
echo 
'<input type=text name=NAME size=33>';
echo 
'</td></tr>';
echo 
'<tr><td>';
echo 
'<b>Event Description: <i><a href=events.php?function=description>Help</a></i></b>';
echo 
'</td><td>';
echo 
'<textarea rows="6" name=DESCRIPTION cols="25"></textarea>';
echo 
'</td></tr>';
echo 
'<tr><td>';
echo 
'<b>Event Expiry Date: <i><a href=events.php?function=expiry>Help</a></i></b>';
echo 
'</td><td>';
echo 
'<input type=text name=EXPIRY size=33>';
echo 
'</td></tr>';
echo 
'<tr><td>';
echo 
'<input type=SUBMIT value=SUBMIT>';
echo 
'</td><td>';
echo 
'<input type=RESET value=`CLEAR FORM`>';
echo 
'</td></tr>';
echo 
'</table>';
echo 
'</form>';
}
//This is the page that processes the form results:
function one() {
echo 
'<title>--::The Riflemans Arms - Events Entry **SUCCESS** Page::--</title>';
echo 
'<p></p>';
echo 
'<p align=center><font size=6><u>Riflemans Arms</u></font></p>';
echo 
'<p></p>';

if (
"$_POST[PASSWORD]1989)

{
    
$dbhost 'localhost'// access point to the database, normally "localhost"
    
$dbusername '****'// The database user name.
    
$dbpassword '********'// The database password.
    
$dbname 'riflemans'// The database's name.
    
$link mysql_connect($dbhost$dbusername$dbpasswd)
          or die(
"Could not connect: " mysql_error());
          print (
"Connected to $dbhost successfully.......");
    
$select mysql_select_db($dbname)
          or die(
"Could not connect to database: " mysql_error());
          print (
"Connected to '$_POST[dbname]' successfully.......");
    
   
$sql 'INSERT INTO whatson VALUES ( `$_POST[DATE]`, `$_POST[NAME]`, `$_POST[DESCRIPTION]`, `$_POST[EXPIRY]`);';
    
    
$result mysql_query($sql);
    if (
$result)
       {
       echo(
"Successfully executed the database manipulation!");
       }
       else
       {
       echo(
"Error executing you request, please check and try again! <a href=events.php>Events Entry Page</a>");
}
else
{
    echo(
"Sorry you entered a incorrect or inactive password, please check and try again <a href=events.php>Events Entery Page</a>");
}
}
// This is the help page for the password field:
function two() {
echo 
'<title>--::The Riflemans Arms - Events **HELP** Page::--</title>';
echo 
'<p></p>';
echo 
'<p align=center><font size=6><u>Riflemans Arms</u></font></p>';
echo 
'<p></p>';
echo 
'<b>The Password field:</b>';
echo 
'<br>This field has been added to stop unautherised personnel posting events.';
echo 
'<br></br>';
echo 
'<br>The password was issued by the <a href=mailto:woody@scoobler.com>WebMaster</a>.';
echo 
'<br>If you have forgotten the password please contact the <a href=mailto:woody@scoobler.com>WebMaster</a> via email.';
}
// This is the help page for the name field:
function three() {
echo 
'<title>--::The Riflemans Arms - Events **HELP** Page::--</title>';
echo 
'<p></p>';
echo 
'<p align=center><font size=6><u>Riflemans Arms</u></font></p>';
echo 
'<p></p>';
echo 
'<b>The Name field:</b>';
echo 
'<br>This field is where you give the up and goming event a simple name. E.G. `Disco Night`';
}
// This is the help page for the description field:
function three() {
echo 
'<title>--::The Riflemans Arms - Events **HELP** Page::--</title>';
echo 
'<p></p>';
echo 
'<p align=center><font size=6><u>Riflemans Arms</u></font></p>';
echo 
'<p></p>';
echo 
'<b>The Description field:</b>';
echo 
'<br>This field is where you can write a detailed description of the up and coming event.';
echo 
'<br>You can include HTML in your description to give the description more character.';
}
// This is the help page for the expiry field:
function three() {
echo 
'<title>--::The Riflemans Arms - Events **HELP** Page::--</title>';
echo 
'<p></p>';
echo 
'<p align=center><font size=6><u>Riflemans Arms</u></font></p>';
echo 
'<p></p>';
echo 
'<b>The Expiry field:</b>';
echo 
'<br>This field is where you give the up and goming event an EXPIRY date.';
echo 
'<br>The Events page works by asking the database of any events who`s Expiry date is after that days date.';
echo 
' Therefore set the expiry date to the last day you want the event to be shown up to. After the Expiry date';
echo 
' the event will no longer appear on the Events Page.';
}
// This is the help page for the date field:
function three() {
echo 
'<title>--::The Riflemans Arms - Events **HELP** Page::--</title>';
echo 
'<p></p>';
echo 
'<p align=center><font size=6><u>Riflemans Arms</u></font></p>';
echo 
'<p></p>';
echo 
'<b>The Date field:</b>';
echo 
'<br>This field is where you give the up and goming event an DATE.';
echo 
'<br>The Events page arranges the events by their up and coming date.';
echo 
' Therefore set the date to the day when the event will occur.';
}
// This is the switch function which tells the page which section of code to display.
switch($function)
{
    default:
    
indi();
    break;
    case 
"process":
    
one();
    break;
    case 
"password":
    
two();
    break;
    case 
"name":
    
three();
    break;
    case 
"description":
    
four();
    break;
    case 
"expiry":
    
five();
    break;
    case 
"date":
    
six();
}
?>




I think the main problems are:

if ("$_POST[PASSWORD]" = 1989)
I want the user to enter the 'password' so only people I want can enter details!

and:

PHP Code:

$sql 'INSERT INTO whatson VALUES ( `$_POST[DATE]`, `$_POST[NAME]`, `$_POST[DESCRIPTION]`, `$_POST[EXPIRY]`);'
Because I was really just guessing!

The table details are set out like this:
Field, Type, Attributes, Null, Default, Extra,
id, double, , No, , auto_increment
ord, date, , No, 0000-00-00, ,
heading, varchar(100), , No, , ,
detail, text, , No, , ,
expiry, date, , No, 0000-00-00, ,

Can anyone help at all??

Thanks in advanced!

Last edited by UnrealEd; 09-04-08 at 01:38 AM. Reason: please use the [php] wrapper when posting php code