form to add data into a database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lostinphp
Forum Commoner
Posts: 44
Joined: Wed Jul 27, 2005 5:10 am
Location: Paris,France.

form to add data into a database

Post by lostinphp »

hey people
im new to php
im making the greatest effort to understand it..
well i have this form where i enter the data and it shud get added it its database..
if i manually insert data and view it using another php file it works..but sending the same data through the form doesnt seem to work.ie the database isnt updated.

Code: Select all

<?php
include("dbconnect.php");
if(!empty($title))
{
$title=addslashes($title);
$user=addslashes($user);
$message=addslashes($message);
//$date=date("F,j,Y");
//$date2=mktime();

$sql="INSERT INTO mynews(id,title,user,date,message)VALUES('NULL','$title','$user','$date','$message')";
$query=mysql_query($sql) or die("cannot query the database" .mysql_error());
echo("database updated.");}
else{
?>

<form action="<?php echo $PHP_SELF;?>" method="POST" enctype="multipart/form-data" name="news">
  <p>please fill out the following fields:</p>
  <table width="800" border="0" cellpadding="0" cellspacing="0">
    <tr> 
      <td width="117"><font size="1">news topic/title :</font></td>
      <td width="577"> <font size="1"> 
        <input type="text" name="$title" size="50">
        </font> </td>
    </tr>
    <tr> 
      <td width="117"><font size="1">username:</font></td>
      <td width="577"><font size="1">
        <input type ="text" name ="$user" size ="50">
        </font> </td>
    </tr>
    <tr>
      <td width="117"><font size="1">message:</font></td>
      <td width="577"> <font size="1"> 
        <textarea name="$message" rows=10 cols=43></textarea>
        </font> </td>
    </tr>
  </table>
  <p align="left">
<font size="1">
<input type="submit" name="submit" value="submit"></font>
</p>
</form> <?php }?>
any kind of suggestions or help is welcome big time.
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

Add a print statement to you php to help debugging:

Code: Select all

include("dbconnect.php");
if(!empty($title))
{
   print $title."<br/>".$user."<br/>".$message;
   exit;
   ...
Do you see your form inputs?
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

However, the problem is probably that REGISTER GLOBALS is turned off, which means PHP will ignore any data it gets from forms or URLs. If you want to fake it to being on (which is okay as long as you are careful to initialise all your variables) add this to the top of each page:

Code: Select all

extract($HTTP_POST_VARS);
extract($HTTP_GET_VARS);
extract($HTTP_COOKIE_VARS);
lostinphp
Forum Commoner
Posts: 44
Joined: Wed Jul 27, 2005 5:10 am
Location: Paris,France.

thanks guys but...

Post by lostinphp »

hey thanks so much. i tried both.still not working. :(
is there any thing else i can do? :?: :roll:
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

'NULL' :arrow: NULL
lostinphp
Forum Commoner
Posts: 44
Joined: Wed Jul 27, 2005 5:10 am
Location: Paris,France.

Post by lostinphp »

u guys really think there is nothing else wrong wid it? :?:
i have the feeling that something is seriously wrong but unfortunately cant figure it out..
i tried everything dat was suggested,still doesnt work though.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

date is also a reserved word. Backticks!

all your field names don't see $ in them. Unless you have register_globals on or you use extract(), $title will not exist. Look for $_POST['title'] instead (after you remove the $ from the names)
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

someone should make a detailed tutorial on storing submitted form data on a database or text file, i know it would get its use...

just my 2 cents... :D
lostinphp
Forum Commoner
Posts: 44
Joined: Wed Jul 27, 2005 5:10 am
Location: Paris,France.

Post by lostinphp »

solved!
the syntax for the sql query was wrong.
thanks people.
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

can you post your completed, working script please. I want to take a look at it so I can try to do this with my forms...

thanks :D
lostinphp
Forum Commoner
Posts: 44
Joined: Wed Jul 27, 2005 5:10 am
Location: Paris,France.

Post by lostinphp »

first one is submit.php
the second script is to view wat we have submitted view.php
it isnt super dooper aligned..plus im very excited abt posting the code cos its the first time im doing so..
databases were created using easyphp i have one database wid one table.
u also have a dbconnect.php where u define all the database and localhost connections..i guess u wont need dat.

Code: Select all

<?php 
extract($HTTP_POST_VARS); 
extract($HTTP_GET_VARS); 
extract($HTTP_COOKIE_VARS); 
include("dbconnect.php");
if(!empty($text))
{
$text=addslashes($text);
$sql ="INSERT INTO homepage(text,date,links)VALUES('$text','$date','$links')";
$query=mysql_query($sql) or die("Unable to query the database" .mysql_error());
echo("database updated.")
;}
else{
?>

<form action="<?php echo $PHP_SELF;?>" method="POST" enctype="multipart/form-data" name="news">
  <p>enter text and upload image</p>
  <table width="800" border="0" cellpadding="0" cellspacing="0">
    <tr> 
      <td width="117"><p><font size="1">text :</font></p>
       
        <p>&nbsp;</p></td>
      <td width="577"> <p><font size="1"> 
          <textarea name="text" rows=10 cols=43></textarea>
          </font></p>
        <p>&nbsp; </p></td>
		
		<tr><td><font size="1">date :</font></td><td><input type="text" name="date" value="0000-00-00"></td></tr>
  <tr><td><font size="1">links</font></td><td><input name="links" type="text" value="http://www."></td>
    </tr>
  
  </table>
  <p align="left">
<font size="1">
<input type="submit" name="submit" value="submit"></font>
</p>
</form> <?php }?>

Code: Select all

<?php
extract($HTTP_POST_VARS); 
extract($HTTP_GET_VARS); 
extract($HTTP_COOKIE_VARS); 
include("dbconnect.php");
$get=mysql_query("SELECT * FROM homepage ORDER BY text ASC");
while($r = mysql_fetch_array($get)){
extract($r);
echo("
<font size=3>$text</font><br> 
<font size=3>$links</font><br>
<font size=3>$date</font><br>");
}?>

feyd | ....
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

very cool,

i am going to try to do this with my forms..

thanks for posting it! :D
Post Reply