Jump to content

Recommended Posts

I have a form that i can enter a name and it inserts the name into a database. I am trying to add validation now. right now, if i push the submit button, it puts in an empty value into the list. So I tried two things

 

if(isset($_POST[name])){
insert name into database
}
else
{
echo "you must put in a name"
}

I also tried 

[code]
if(!isset($_POST[name]))
return false;
[code]

neither worked. It still inserts a null value. What am I doing wrong?

Link to comment
https://forums.phpfreaks.com/topic/178510-form-validation/
Share on other sites

I keep getting an error from that

 

Parse error: parse error, expecting `','' or `')'' in C:\wamp\www\dbtest\insert.php on line 5

 

if(isset($_POST['name'] && !empty($_POST['name'])){
$sql="INSERT INTO mytable (names)
VALUES
('$_POST[name]')";
}
else
{
echo "you must put in a name";
}

Link to comment
https://forums.phpfreaks.com/topic/178510-form-validation/#findComment-941388
Share on other sites

I just cought on to that. But now it creates a new problem, the code errors out on the sql check.

 

if(isset($_POST['name']) && !empty($_POST['name'])){
$sql="INSERT INTO mytable (names)
VALUES
('$_POST[name]')";
}
else
{
echo "you must put in a name";
}

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }

 

now, if I just push the submit button, it gives me the echo error, but it also says

 

Undefined variable: sql

 

the mysql_query is not part of the else statement, so why would it now error out?

Link to comment
https://forums.phpfreaks.com/topic/178510-form-validation/#findComment-941393
Share on other sites

You lost me. The sql is defined in the above insert statement. To insert the name into the database. then if if works, it shows the name in the list. The code worked fine until I added the check to see if the name field was empty. So I don't understand why it would error out now.

 

Thanks for your help by the way.

Link to comment
https://forums.phpfreaks.com/topic/178510-form-validation/#findComment-941404
Share on other sites

let's have a look at your form.

 

you are getting the 'Undefined variable: sql' error because if the condition isn't met (if $_POST['name'] has no value or has not been set), $sql doesn't get defined.  so, when you throw in this code with $_POST['name'] not being set:

 

if (!mysql_query($sql,$con))
{
  die('Error: ' . mysql_error());
}

 

the system will try and execute: if (!mysql_query ($sql, $con)) .. but $sql hasn't been set yet.  get it?

 

post your form and i'll see what i can do.

Link to comment
https://forums.phpfreaks.com/topic/178510-form-validation/#findComment-941413
Share on other sites

I see, that makes sense. Well then if I throw in another check for the query to only perform if the post is set then it should work. That is my code posted above. I have all the code split up into several files, so each part is very small. There is insert, delete, connection, and index. The insert code has been posted

Link to comment
https://forums.phpfreaks.com/topic/178510-form-validation/#findComment-941424
Share on other sites

A method I find usefull for handling validation is this...

 

$errors = array();

if(!isset($_POST['forename']) || empty($_POST['forename'])) {
   $errors['forename'] = "Error with forename";
}

if(!isset($_POST['surname']) || empty($_POST['surname'])) {
   $errors['surname'] = "Error with surname";
}

// ... rest of fields.

if(empty($errors)) {
   // proceed with processing
}

Link to comment
https://forums.phpfreaks.com/topic/178510-form-validation/#findComment-941432
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.