What have I done wrong?

Status
Not open for further replies.

Xenous

o shi
Nov 15, 2011
383
101
So I'm a noobie at php and I'm trying to some basic input forms, and here's my problem.
Notice: Undefined index: user in C:\xampp\htdocs\index.php on line 13

Notice: Undefined index: pass in C:\xampp\htdocs\index.php on line 14
and my code is
PHP:
<html>
 
<head>
 
<title>
 
Okay lets do this
 
</title>
 
</head>
 
<body>
 
<?php
 
include ('db.php');
 
?>
 
<?php
 
$user = strip_tags(mysql_real_escape_string($_POST['user']));
 
$pass = strip_tags(mysql_real_escape_string($_POST['pass']));
 
mysql_query ("INSERT INTO `users` (`username`, `password`) VALUES ('{$user}', '{$pass}')");
 
?>
 
<form method="post"/>
 
Username: <input type="textbox" name="user" />
 
Password: <input type="textbox" name="pass"/>
 
</form>
 
</body>
 
</html>
so what have I done wrong?
 

Markshall

Русский Стандарт
Contributor
Dec 18, 2010
2,637
2,389
For starters, change
Code:
<form method="post"/>
to
Code:
<form method="post">

Also, why are you executing the database query on the page load, you have not included a submit button and anything to handle the button so when you click it, it will reload the page and enter the information into the database, try:

PHP:
<html>
<head>
<title>Okay lets do this</title>
</head>
<body>
<?php
include ('db.php');
if ( isset( $_POST['my_button'] ) )
{
    $user = strip_tags( mysql_real_escape_string( $_POST['user'] ) );
    $pass = strip_tags( mysql_real_escape_string( $_POST['pass'] ) );
    mysql_query( "INSERT INTO `users` (`username`, `password`) VALUES ('{$user}', '{$pass}')" );
}
?>
<form method="post">
Username: <input type="textbox" name="user" /><br />
Password: <input type="textbox" name="pass" /><br /><br />
<input type="submit" name="my_button" value="My Button to do whatever" />
</form>
</body>
</html>
 

Xenous

o shi
Nov 15, 2011
383
101
thank you very much I see what I have done wrong and have now learnt a little more thanks :up:
 

Markshall

Русский Стандарт
Contributor
Dec 18, 2010
2,637
2,389
No problems; has it fixed the issue you had?

Also, referring to the '/>' thing. You only need to put /> on HTML tags that don't have an end tag. For example:

<p> does not need '/>' because it has an ending tag which is </p>
<div> does not need '/>' because it has an ending tag which is </div>
<br /> requires '/>' because it does not have an ending tag such as </br>
<input type=" [text|password|email|url|tel] " /> requires '/>' because it does not have an ending tag such as </input>

If you don't understand then I'll try to explain in more depth.
Basically, you only need '/>' on tags that don't have an ending tag.
 

Xenous

o shi
Nov 15, 2011
383
101
No problems; has it fixed the issue you had?

Also, referring to the '/>' thing. You only need to put /> on HTML tags that don't have an end tag. For example:

<p> does not need '/>' because it has an ending tag which is </p>
<div> does not need '/>' because it has an ending tag which is </div>
<br /> requires '/>' because it does not have an ending tag such as </br>
<input type=" [text|password|email|url|tel] " /> requires '/>' because it does not have an ending tag such as </input>

If you don't understand then I'll try to explain in more depth.
Basically, you only need '/>' on tags that don't have an ending tag.
ya I partially knew that just I hadnt had a reason to do the html forms (meaning I knew nothing about them) since I had never used php until today thanks anyways you've been a big help
 

Markshall

Русский Стандарт
Contributor
Dec 18, 2010
2,637
2,389
ya I partially knew that just I hadnt had a reason to do the html forms (meaning I knew nothing about them) since I had never used php until today thanks anyways you've been a big help
You was probably rushing the code and put /> by accident, but anyway - at least you know now.

No worries.
 
Status
Not open for further replies.

Users who are viewing this thread

Top