Jump to content

Form Validation and MySQL


Pavlos1316

Recommended Posts

Hello,

 

I am trying to validate and insert some values into a db.

 

Although I did this 3-4 times succesfully, I have a problem which I don't know where it came from.

 

My validation code (partial):

 

<?php
//connect to db
require_once('dbconnection.php');

$name = mysql_real_escape_string($_POST['name']);   //LINE 3

function validateName($name){
	//if it's NOT valid
	if(strlen($name) < 3){
		return false;
	//if it's valid
	}else{
		return true;
        }
        }

$query = "INSERT INTO s_table (ID, Name) VALUES ('Null', '$name')";
mysql_query($query) or die(mysql_error());
mysql_close();

//Send email code
?>

But I get: Undefined index: name in.... on line 3. (This is a Notice and not an ERROR but I prefer to get rid of it instead of disabling the Notices and "hide" it.)

 

What am I doing wrong  :confused:

Link to comment
Share on other sites

Undefined index:

 

this means $_POST['name'] is not defined. So either your form name  is spelled wrong or your not submitting the form (aka directly running the code above)

 

so a good thing could be to check if someone pressed the submit button and than check if the value of $_POST['name'] is as expected.

for instance

<?php

if(isset($_POST['submit']) && !empty($_POST['name'])){
     //check the values... are they as expected?


    //run your code


}else{
   echo 'form was not submitted'; // do something about it
}



?>


 

 

Link to comment
Share on other sites

What do you mean by "your form name is spelled wrong"? Where?

 

or your not submitting the form Hm... I am calling the php script using require_once('validate.php') at the top where my form is. I have my

<form action=''>

because I am using JQ validation as well. I don't know/think the mistake is here.

 

My email is being sent even if it's empty...

Link to comment
Share on other sites

What do you mean by "your form name is spelled wrong"? Where?

 

Look your giving $name a value of $_POST['name'] So $_POST['name'] has to come from somewhere. This most likely is a form. for instance:

 

<form action="" method="post">
      <input type="text" name="name"  />

       <input type="submit" name="submit" value="submit this form" />
</form>

 

As you can see this form has an input field with the name of "name" When someone fills that in with for instance the name GORILLA

ones someone presses submit,..... $_POST['name'] will hold the value of GORILLA

 

But when you don't submit that form $_POST['name'] is not even set.

So if you say $name = $_POST['name'] the script doesn't know what your talking about because $_POST['name'] doesn't exist. (since you didn't submit anything)

 

So to repeat the above. IF you first check if someone submitted a form and than check the value of $_POST['name'] you won't get any error.

 

What i meant with miss-spelled is say you have a field name like this

<input type="text" name="naame" />

ones you press submit it's not $_POST['name'] that is being set but $_POST['naame']

 

So always check the spelling if you see the error "undefined index" or make sure you submitted the form

Link to comment
Share on other sites

What i meant with miss-spelled is say you have a field name like this

Oh... You said form name at first and I was confused... My spelling I have checked it and it was ok...

 

I am using the if(isset ($_POST['submit']))... I don't know.... If I wasn't submiting the form to the validation.php would I get my email send????

Link to comment
Share on other sites

Look the question you posted was: But I get: Undefined index: name in.... on line 3 ... now what?

 

The cause of that is explained above.  Your non related question, do i get email?

 

I have no idea and no one does without visionary skills. your code has a comment in it (//include mail script here) but that's it.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.