Page 1 of 1

Form help

Posted: Tue Apr 12, 2005 10:25 pm
by Smackie
does anyone have a code or could show me how to make like a form that can add like something on to a page and take off stuff on a page because im making a website for a friend and she wanted it where she doesnt have to log into the cpanel just to add alittle data onto a page so i told her i would make a form so she wouldnt have too could some show me how?

thank you Smackie

Posted: Tue Apr 12, 2005 10:38 pm
by infolock
basically, you could do something with mysql. it's actually the easiest method..

something like this would work :

1) create a table, one with a field value of text
2) then, create a page that would fill this field with data you wanna display. You could even go so far as to post what they have so far, and allow them to either a) edit the current textarea or b) delete it and post a new one. :

Code: Select all

<?php
$sql = mysql_query("select * from mytable") or die(MYSQL_Error());
$row = mysql_fetch_assoc($sql);
$field_data = $row['text_field_name'];
echo '<form method="post" action="mypage.php">';
echo '<textarea name="bob">'.$field_data.'</textarea>';
echo '<input type="submit" name="submit" value="submit changes">';
?>

Code: Select all

<?php
$text_area = $_GET['bob'];
mysql_query("INSERT into mytable (text_field_name) VALUES ('".$text_area."')") or die(MySQL_Error());
echo 'Field updated';
?>
3) then, in the display page, have something like :

Code: Select all

<?php
$sql = mysql_query("select * from mytable") or die(MySQL_Error());
$row = mysql_fetch_assoc($sql);
echo '<h3>The current page is to display the following information </h3>';
echo $row['text_field_name'];
?>
of course this is very cut and dry. you could setup a number of different methods to go along with this (ie, could make it to where the page is a news page, menaing it keeps track of all entries in the database based on the date, you could add session (and should) checks to verify she has admin rights before information is posted ot hte page, and a number of other methods.

if this is the type of thing you want to do, i'd suggest looking into pre-made scripts (usually found on hotscripts) that does this type of thing, and also checking mysql's online manual for field types and query statements..

hope this helps.

Posted: Tue Apr 12, 2005 11:26 pm
by Smackie
thank you that was kinda what i was looking for :) thank you very much.....

Posted: Tue Apr 12, 2005 11:43 pm
by Smackie
i have a problem i am trying to make it where it adds more the one field

race.php

Code: Select all

<?php
include'config.php';
$sql = mysql_query("select * from race") or die(MYSQL_Error());
$row = mysql_fetch_assoc($sql);
$race = $row['race'];
$outcome = $row['outcome'];
echo '<form method="post" action="mypage2.php">';
echo '<textarea name="race">'.race.'</textarea>';
echo '<textarea name="outcome">'.outcome.'</textarea>';
echo '<input type="submit" name="submit" value="submit changes">';
?>
mypage2.php

Code: Select all

<?php
include'config.php';
$race = $_GET['race'];
$outcome =$_GET['outcome'];
mysql_query("INSERT into race (race, outcome) VALUES ('".$message.$outcome."')") or die(MySQL_Error());
echo 'Field updated';
?>
Column count doesn't match value count at row 1

Posted: Wed Apr 13, 2005 12:10 am
by infolock
which page is giving you the error?

secondly, try this instead for your page 2...

Code: Select all

//other code...
mysql_query("INSERT into race (race, outcome) VALUES ('".$message."','".$outcome."')") or die(MySQL_Error());
//other code...
edit :

on page one, you need to update this too...

Code: Select all

//other code
echo '<textarea name="race">'.$race.'</textarea>';
echo '<textarea name="outcome">'.$outcome.'</textarea>';
//other code

Posted: Wed Apr 13, 2005 12:16 am
by infolock
better question, what the hell is $message being set as? I see it no where in the script being defined as anything???

Posted: Wed Apr 13, 2005 12:16 am
by Smackie
ok that fixed that problem it goes into the database but its doesnt show anything its just blank :S so i think there is a code wrong on the first one :S

Posted: Wed Apr 13, 2005 12:24 am
by infolock
as i said, might do good to look at mysql manual heh..

first, you are trying to insert $message, but nothing is defined for $message...

Second, you may want to do (instead of INSERT) an UPDATE <clause> where <this is true> for a query...

as i said, may want to look into mysql's online manual, or even take a look at the manual included with your mysql documentation.

i guess i might have confused you just a little, but was just trying to give you an idea of how to do this... i didn't actually post a working method heh.. so you may want to rewrite this script to do what you need how you need it,a nd then post that. cuz my method just merely inserts a new row everytime. it doesn't update the table at all where a variable (in your case race) is needin to be updated...

Posted: Wed Apr 13, 2005 12:36 am
by Smackie
well i think it might be right around this area..

Code: Select all

$race = $row['race'];
$outcome = $row['outcome'];
echo '<form method="post" action="mypage2.php">';
echo '<textarea name="race">'.race.'</textarea>';
echo '<textarea name="outcome">'.outcome.'</textarea>';
that is screwing up because on the first one i did worked just right and it was mainly what you showed me but it was a one field but this is two fields and i think my problem is around here........

Posted: Wed Apr 13, 2005 12:55 am
by infolock
first, i already posted the correct thing to do with that portion of hte script (look at my message above).

secondly, the reason only one field (for the 3rd time now) is because you are trying to insert $message... but $message is NOT being defined.

Therefore, this query

Code: Select all

mysql_query("INSERT into race (race, outcome) VALUES ('".$message.$outcome."')") or die(MySQL_Error());
should actually be this :

Code: Select all

mysql_query("INSERT into race (race, outcome) VALUES ('".$race."', '".$outcome."')") or die(MySQL_Error());

Posted: Wed Apr 13, 2005 9:01 pm
by Smackie
ok i got it all working but i need it where it will loop it instead of deleting it after every post...

Posted: Sun Apr 17, 2005 2:08 am
by infolock
then you should look into these pages available on php's online manual:

Arrays
MySQL Fetch Assoc
For Loops
While Loops

and the MySQL Manual

there is also a MySQL manual located in the distribution on your computer (that is, of course, if you have mysql installed on your machine). It can be reached at //path//to//MySQL/doc/


that should be enough to get you where you need.

Posted: Sun Apr 17, 2005 2:19 am
by thallish
Smackie wrote:well i think it might be right around this area..

Code: Select all

$race = $row['race'];
$outcome = $row['outcome'];
echo '<form method="post" action="mypage2.php">';
echo '<textarea name="race">'.race.'</textarea>';
echo '<textarea name="outcome">'.outcome.'</textarea>';
Hmm this looks odd to me. Can you use variable names without the $?

thallish

Posted: Sun Apr 17, 2005 6:53 am
by feyd
thallish wrote:Can you use variable names without the $?
they'd have to be constants. So, no.

PHP will likely fire a notice of unknown constant race, assuming 'race' ... I wish that was an error instead of a notice..