Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 08-21-2007, 06:36 AM problem with form.
Skilled Talker

Posts: 77
Name: adam
Location: UK
Trades: 0
hi i have a couple of problems with my site which i have been creating.

Firstly i have made it so data from my database will show up on the main site page. secondly i have made some of the information show up in my form. but the problem is parts of my form dont show the information and the main problem is when i submit something though my forms it doesnt actually get inserted into my database.

Lastly i would like my form to insert data into the database but if the data is already in the database i would like it to update that row, not make a new one.

here is the action code for my forms.

PHP Code:
<?php
//connects you to your database.
include('../../_dbconnect.php');
//Only allows data to be added to the database, if the submit button has been been pressed.
if($_POST['submit']) {
//Varibles. Do Not Edit
$title=$_POST['title'];
$author=$_POST['author'];
$date=$_POST['date_added'];
$content=$_POST['content'];

//Form Validation Functions
function check_title_field($field_name_1)
 {
   if(!
preg_match("/[^a-zA-Z]+$/s",$field_name_1))
    return 
TRUE;
   else
    return 
FALSE;
 }
function 
check_author_field($field_name_2)
 {
   if(!
preg_match("/[^a-zA-Z]+$/s",$field_name_2))
    return 
TRUE;
   else
    return 
FALSE;
 }
function 
check_date_field($field_name_3)
 {
   if(!
preg_match("/[^0-9]+$/S",$field_name_3))
    return 
TRUE;
   else
    return 
FALSE;
 }
function 
check_content_field($field_name_4)
 {
   if(!
preg_match("/[^a-zA-Z0-9]+$/S",$field_name_4))
    return 
TRUE;
  else
    return 
FALSE;
 }
//Form Validation
$error=0;
//Form Validation Checking
if(!check_title_field($title))
 {
  echo 
"You have used invalid Characters Please Use A-Z";
  
$error++; // $error=$error+1
 
}
if(!
check_author_field($author))
 {
  echo 
"You have used invalid Characters Please USE A-Z";
  
$error++; // $error=$error+1
 
}
if(!
check_date_field($date))
 {
   echo 
"You have used invalid Characters Please Use 0-9";
   
$error++; // $error=$error+1
 
}
if(!
check_content_field($content))
 {
  echo 
"You have used invalid Characters Please Use 0-9";
   
$error++; // $error=$error+1
 
}
if(
$error==0)
 {
  echo
  
"
  The Data You Have Entered Is Correct.
  "
;
 }
else
 {
  echo
  
"
  Number of Errors : 
$error Please go Back To Correct Them
  "
;
 }
//Insert information into database
mysql_query("INSERT INTO about_me ('title', 'author', 'date_added', 'content') VALUES ('$title', '$author', '$date', '$content')");
//Returns Back to previous Page
}
header ("Location: ../post_aboutme.php");
?>
PS. when i have submitted the information the page stays white and shows the following error

Quote:
Warning: Cannot modify header information - headers already sent by (output started at C:\wamp\www\MintuzProductions\admin\include\engine _aboutme.php:85) in C:\wamp\www\MintuzProductions\admin\include\engine _aboutme.php on line 100
mintuz is offline
Reply With Quote
View Public Profile Visit mintuz's homepage!
 
 
Register now for full access!
Old 08-21-2007, 06:53 AM Re: problem with form.
JamieLewis's Avatar
Pretty Much a Big Deal...

Latest Blog Post:
Gooie
Posts: 385
Name: Jamie Lewis
Location: UK
Trades: 0
I need to see a copy of your form for the first question.

Second question: You need a unique record id to use to enable you to distinguish between new and existing rows.

Last Question: You are attempting to change the header after you have echoed output to the screen. You can only change headers before outputting.

Jamie
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
JamieLewis is offline
Reply With Quote
View Public Profile Visit JamieLewis's homepage!
 
Old 08-21-2007, 07:06 AM Re: problem with form.
Skilled Talker

Posts: 77
Name: adam
Location: UK
Trades: 0
PHP Code:
<?php include('../_dbconnect.php'); ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Mintuz Portfolio</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="../_style.css" rel="stylesheet" type="text/css">
</head>
<body>
<table width="100%"  border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td height="102" align="left" valign="top" background="http://www.webmaster-talk.com/images/extra_banner.jpg"><table width="100%"  border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td width="52%" height="97"><img src="http://www.webmaster-talk.com/images/index_r2_c1.jpg" width="405" height="102"></td>
        <td width="48%"><div align="center"><img src="http://www.webmaster-talk.com/images/index_r3_c6.jpg" width="233" height="53"></div></td>
      </tr>
    </table></td>
  </tr>
  <tr>
    <td height="30" background="http://www.webmaster-talk.com/images/extra_nav.jpg"><div align="center"><a href="../index.php"><img src="http://www.webmaster-talk.com/images/index_r5_c1.jpg" width="152" height="30" border="0"></a><a href="../aboutme.php"><img src="http://www.webmaster-talk.com/images/index_r5_c3.jpg" width="103" height="30" border="0"></a><a href="../webdesign.php"><img src="http://www.webmaster-talk.com/images/index_r5_c4.jpg" width="118" height="30" border="0"></a><a href="../scripts.php"><img src="http://www.webmaster-talk.com/images/index_r5_c5.jpg" width="75" height="30" border="0"></a><img src="http://www.webmaster-talk.com/images/index_r5_c7.jpg" width="84" height="30"><a href="../contactme.php"><img src="http://www.webmaster-talk.com/images/index_r5_c8.jpg" width="126" height="30" border="0"></a></div></td>
  </tr>
  <tr>
    <td height="257" align="left" valign="top" background="http://www.webmaster-talk.com/images/extra_content.jpg"><table width="100%"  border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td width="33" height="33"><img src="http://www.webmaster-talk.com/images/index_r6_c1.jpg" width="33" height="33"></td>
        <td background="http://www.webmaster-talk.com/images/index_r6_c2.jpg">&nbsp;</td>
        <td width="33" height="33"><img src="http://www.webmaster-talk.com/images/index_r6_c9.jpg" width="33" height="33"></td>
      </tr>
      <tr>
        <td background="http://www.webmaster-talk.com/images/index_r7_c1.jpg">&nbsp;</td>
        <td><table width="100%"  border="0" cellspacing="0" cellpadding="2">
          <tr>
            <td height="297" align="left" valign="top">
   <?php
   $table_query 
mysql_query("SELECT * FROM about_me WHERE ID='1'") or die("error getting info".  mysql_error());
    
$row=mysql_fetch_array($table_query);
     echo
'
   <form name="form1" method="post" action="include/engine_aboutme.php">
              <table width="100%"  border="1" cellpadding="5" cellspacing="0" bordercolor="#CCCCCC">
                <tr bgcolor="#F3F3F3">
                  <td width="14%">Title : </td>
                  <td width="86%"><input name="title" type="text" id="title" class="input" value="'
.$row['title'].'"></td>
                </tr>
                <tr>
                  <td bgcolor="#E2E2E2">Date Added : </td>
                  <td bgcolor="#E2E2E2"><input name="date_added" type="text" id="date_added" class="input" value="'
.$row['date_added'].'"></td>
                </tr>
                <tr bgcolor="#F3F3F3">
                  <td>Author : </td>
                  <td><input name="author" type="text" id="author" class="input" value="'
.$row['author'].'"></td>
                </tr>
                <tr>
                  <td align="left" valign="top" bgcolor="#E2E2E2">Content : </td>
                  <td bgcolor="#E2E2E2"><textarea name="content" cols="85" rows="15" class="input" value="'
.$row['content'].'" id="content"></textarea></td>
                </tr>
                <tr bgcolor="#E2E2E2">
                  <td colspan="3"><input name="submit" type="submit" id="submit" value="Submit">
                    <input name="reset" type="reset" id="reset" value="Clear"></td>
                </tr>
              </table>
            </form>            <p class="style2">&nbsp;</p>              </td>
   '
?>
          </tr>
        </table></td>
        <td background="http://www.webmaster-talk.com/images/index_r7_c9.jpg">&nbsp;</td>
      </tr>
      <tr>
        <td width="33" height="33"><img src="http://www.webmaster-talk.com/images/index_r8_c1.jpg" width="33" height="33"></td>
        <td align="left" valign="top" background="http://www.webmaster-talk.com/images/index_r8_c2.jpg"><div align="center" class="style1">Copyright 2007-2008 &copy; All Rights Reserved</div></td>
        <td width="33" height="33"><img src="http://www.webmaster-talk.com/images/index_r8_c9.jpg" width="33" height="33"></td>
      </tr>
    </table>    </td>
  </tr>
</table>
</body>
</html>
There is my form and i have a field called ID in my database which is unquie. how can i go about redirecting back to the previous page.

Last edited by mintuz; 08-21-2007 at 07:08 AM..
mintuz is offline
Reply With Quote
View Public Profile Visit mintuz's homepage!
 
Old 08-24-2007, 04:40 AM Re: problem with form.
Skilled Talker

Posts: 77
Name: adam
Location: UK
Trades: 0
can anybody help me please...
mintuz is offline
Reply With Quote
View Public Profile Visit mintuz's homepage!
 
Old 08-24-2007, 11:42 AM Re: problem with form.
Super Talker

Posts: 130
Trades: 0
Put your insert statement into a variable and echo it out to see what it contains. Usually the problem is something that needs to be there, isn't. Also, when you execute your query, kill it on error.

PHP Code:
mysql_query($insert) or die('Something went terribly wrong: ' mysql_error()); 
__________________
flann

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
flann is offline
Reply With Quote
View Public Profile
 
Old 08-24-2007, 12:24 PM Re: problem with form.
Skilled Talker

Posts: 77
Name: adam
Location: UK
Trades: 0
it came back with query is empty. what does this mean since my table is full.
mintuz is offline
Reply With Quote
View Public Profile Visit mintuz's homepage!
 
Old 08-24-2007, 12:26 PM Re: problem with form.
Super Talker

Posts: 130
Trades: 0
Did you put your insert statement into the $insert variable and echo it out? What is the output on that?
__________________
flann

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
flann is offline
Reply With Quote
View Public Profile
 
Old 08-24-2007, 12:29 PM Re: problem with form.
Extreme Talker

Posts: 182
Trades: 0
1)
First off, it seems like you need to be putting your insert statement inside the 'if($error==0)' brackets. Now, it is trying to insert the data even if the form validation fails.

2)
The header issue, headers have to be set before ANYTHING is sent to the browser. I believe ANYTHING includes html as well.

Again, it seems like the header location change needs to take place inside the 'if($error==0)' brackets. It is trying to redirect whether the form fails or passes.

3)
You will probably want to use an argument like:

if ( updating )
{
update database
}
else
{
insert new record into database
}


4)
As for the query not working. The syntax looks correct. Are you getting any errors? Try adding an or die() at the end so you can catch any mysql errors. It would be like this:

mysql_query ( $query ) or die ( mysql_error ( ) );
bhgchris is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to problem with form.
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.27833 seconds with 12 queries