Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MySQL insert with php form

Status
Not open for further replies.

Roberti

Technical User
Joined
Mar 2, 2001
Messages
96
Location
NL
Hi all,

I'm trying to make a form where you fill in a titel, and when you click on submit it stores it in the MySQL database, but I can't get it to work. It processes the entire document with an error, and stores an empty record in the database. Here is my code:

<?PHP
include(&quot;./include/header_inc.php&quot;);
include(&quot;./include/connection_inc.php&quot;);

IF ($HTTP_POST_VARS['actie'] = 'insert')

{
$titel = $HTTP_POST_VARS['titel'];

$SQL = &quot;INSERT INTO tbl_dvd (titel) VALUES ('$titel')&quot;;

$query_result = mysql_query($SQL,$strconnstring);

echo mysql_error();
}

ELSE

{
print &quot;<FORM name = toevoegen ACTION=dvd_toevoegen.php?actie=insert METHOD=post>&quot;;
print &quot;<TABLE border = 1>&quot;;
print &quot;<TR>&quot;;
print &quot; <TD><input type=text name=titel size=40></TD>&quot;;
print &quot; <TD><input type=submit name=submit value=Submit></TD>&quot;;
print &quot;</TR>&quot;;
print &quot;</TABLE>&quot;;
print &quot;</FORM>&quot;;
}

include(&quot;./include/footer_inc.php&quot;);
?>

The error is:

Undefined index: titel in c:\program files\apache\htdocs\dvd_toevoegen.php on line 8

Does anybody have an idea?

Thanx.
 
the problem may be that titel may not be set:
rty this:
if(!isset($HTTP_POST_VARS['titel']))
echo &quot;Not Set&quot;;
else
echo &quot;Set&quot;;


tell me what was echoed....

Known is handfull, Unknown is worldfull
 
Thanx for the reply.

It echoed Not set. What am I missing here?
 
the problem is that there is not field called titel or the form in the page where it is submitted from does not use the method &quot;post&quot;.

the page from which u submit the data has a form tag right? can i have it?

Known is handfull, Unknown is worldfull
 
I found the problem. the IF THEN wasn't functioning properly. No matter what condition it alway did the insert, even when nothing was filled in. so when it loaded the 'titel' was empty. Here is the right code:

<?PHP
include(&quot;./include/header_inc.php&quot;);
include(&quot;./include/connection_inc.php&quot;);

IF (!isset($HTTP_POST_VARS['titel']))

{
print &quot;<FORM name = toevoegen ACTION=dvd_toevoegen.php?actie=opslaan METHOD=post>&quot;;
print &quot;<TABLE border = 1>&quot;;
print &quot;<TR>&quot;;
print &quot; <TD><input type=text name=titel size=20></TD>&quot;;
print &quot; <TD><input type=submit name=submit value=Submit></TD>&quot;;
print &quot;</TR>&quot;;
print &quot;</TABLE>&quot;;
print &quot;</FORM>&quot;;
}

ELSE

{
$titel = $HTTP_POST_VARS['titel'];

$SQL = &quot;INSERT INTO tbl_dvd (titel) VALUES ('$titel')&quot;;

$query_result = mysql_query($SQL,$strconnstring);

echo mysql_error();
}

include(&quot;./include/footer_inc.php&quot;);
?>

Thanx for the help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top