Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

Validate Input Forms Using PHP


ghost's Avatar
0 0

Hey,

I have recently learned PHP and MySQL and have written a series of scripts many which require form input. However I haven't been validating the input data. Basically all I want to do is to check if the variables $title and $author are empty. My current code is:

switch ($_GET["do"]) {
case "add":
include("dbconnect.php");
if(count($_POST) > 0) {
$title = mysql_real_escape_string(trim($_POST["title"]));
$author = mysql_real_escape_string(trim($_POST["author"]));
$sql = "INSERT INTO books(name, author) VALUES('$title', '$author')";
$result = mysql_query($sql) or die(mysql_error());
$message = "<p>Your Book Has Been Added</p>
<br/ >
<a href='index.php'>Go Back</a>
"; }
$title = "<h1>Add Book</h1>";
$html = "<form action='index.php?do=add' method='post'>
<p><strong>Book Title:</strong> <input type='text' name='title' /></p>
<p><strong>Author Name:</strong> <input type='text' name='author' /></p>
<p><input type='submit' value='Add Book' /></p>
</form>";
break;
default:
include("dbconnect.php");
$title = "<h1>Books I Own</h1>";
$html = "
<a href='index.php?do=add'>Add Books</a> | <a href='index.php?do=delete'>Delete Books</a>
<br />
<br />
<table border= 1>
<th>ID</th>
<th>Book Name</th>
<th>Author</th>";
$sql = "SELECT * FROM books";
$result = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_array($result)) {
$html .= "<tr><td>".$row['id']."</td><td>".$row['name']."</td><td>".$row['author']."</td></tr>";
}
$html .= "</table>";
break;
}
?>
<html>
<head><title>Books I Own</title></head>
<body>
<?php
print $title;
print $message;
print $html;
?>
</body>
</html>```

Your probably thinking damn thats some crap code but hey I am new :)

yours31f's Avatar
Retired
10 0

why not use the isset command?


ghost's Avatar
0 0

Sorry to be a pest but could you be kind enough to add this into my code as I am having a problem.


spyware's Avatar
Banned
0 0

PHPDan wrote: Sorry to be a pest but could you be kind enough to add this into my code as I am having a problem.

What's the problem? Post errors.


ghost's Avatar
0 0

Just replace your if conditional with a test for "not empty(variable)" for both the variables you're testing. Join the tests with &&. Basic PHP knowledge; if you don't possess knowledge of conditionals, you definitely need to learn that before continuing.


ghost's Avatar
0 0

like:

$message = "test"; } else {
$sql = "INSERT INTO books(name, author) VALUES('$title', '$author')";
$result = mysql_query($sql) or die(mysql_error());
$message = "<p>Your Book Has Been Added</p>
<br/ >
<a href='index.php'>Go Back</a>
"; }```

hellboundhackersok's Avatar
Banned
0 0
{
//do whatever if it's not equal to blank space
}
else
{
//do something if it **is** equal to blank space
}```

ghost's Avatar
0 0

Close. Put ! in front of the empty function for each… you're testing for "not empty", not "empty".


ghost's Avatar
0 0

Thanks guys I have now got it working :) Now I need some more challenges, anyone got any?


spyware's Avatar
Banned
0 0

hellboundhackersok wrote: Some code

Hiya. Coding standards would like to have a word with you.


ghost's Avatar
0 0

spyware wrote: [quote]hellboundhackersok wrote: Some code

Hiya. Coding standards would like to have a word with you.[/quote]

Ha ha im just starting with PHP but I to thought his code was a bit messed up.


hellboundhackersok's Avatar
Banned
0 0

spyware wrote: [quote]hellboundhackersok wrote: Some code

Hiya. Coding standards would like to have a word with you.[/quote]

Edit: fine… I'll use empty().. I guess I'm just too used to being a completely 1337 C++ coder… or not…?


ghost's Avatar
0 0

hellboundhackersok wrote: [quote]spyware wrote: [quote]hellboundhackersok wrote: Some code

Hiya. Coding standards would like to have a word with you.[/quote]

ahah I don't really want to clean my code =D

Dan: he was talking to me :angry:[/quote]

I know……..


hellboundhackersok's Avatar
Banned
0 0

PHPDan wrote: [quote]hellboundhackersok wrote: [quote]spyware wrote: [quote]hellboundhackersok wrote: Some code

Hiya. Coding standards would like to have a word with you.[/quote]

ahah I don't really want to clean my code =D

Dan: he was talking to me :angry:[/quote]

I know……..[/quote]

whoa I totally read what you type wrong. whatever.


spyware's Avatar
Banned
0 0

hellboundhackersok wrote: Edit: fine… I'll use empty().. I guess I'm just too used to being a completely 1337 C++ coder… or not…?

In C++ you would use Var.empty().


ghost's Avatar
0 0

PHPDan wrote: Ha ha im just starting with PHP but I to thought his code was a bit messed up. No offense, but that is a bit presumptuous of you. His code was incomplete for the exact need specified but, as it was, it would've solved your problem. Testing a string for != '' and testing a string with !empty() are synonymous when looking for empty strings. However, empty will also capture null values, which is essential for testing MySQL DB values for empty / null values.

Basically, it can be boiled down to this:

  1. != is fine for testing for '' only (an empty string).
  2. empty() is good for testing for both an empty string and a null value.
  3. isset() is meant to test the existence of a variable… if a value is not POSTed at all, it will fail this. If it is POSTed, it will possibly pass this; this is not a good way to test for empty POST values.

hellboundhackersok's Avatar
Banned
0 0

well I was taught to use if (var != "") {//whatever}… Using empty() what Includes do you need? … actually Just googled it:

nevermind, thanks!

and thanks Zephyr_Pure for clarifying that to ..us..:p


ghost's Avatar
0 0

Zephyr_Pure wrote: [quote]PHPDan wrote: Ha ha im just starting with PHP but I to thought his code was a bit messed up. No offense, but that is a bit presumptuous of you. His code was incomplete for the exact need specified but, as it was, it would've solved your problem. Testing a string for != '' and testing a string with !empty() are synonymous when looking for empty strings. However, empty will also capture null values, which is essential for testing MySQL DB values for empty / null values.

Basically, it can be boiled down to this:

  1. != is fine for testing for '' only (an empty string).
  2. empty() is good for testing for both an empty string and a null value.
  3. isset() is meant to test the existence of a variable… if a value is not POSTed at all, it will fail this. If it is POSTed, it will possibly pass this; this is not a good way to test for empty POST values.[/quote]

Thanks for the information. I am now planning my CMS.


clone4's Avatar
Perl-6 Wisdom Seeker
0 0

So am I, but oldschoold perl style :D:D sry for the hijack, couldn't resist. But anyway good luck with that…


ghost's Avatar
0 0

Zephyr_Pure wrote: Testing a string for != '' and testing a string with !empty() are synonymous when looking for empty strings. However, empty will also capture null values, which is essential for testing MySQL DB values for empty / null values.

Basically, it can be boiled down to this:

  1. != is fine for testing for '' only (an empty string).
  2. empty() is good for testing for both an empty string and a null value.
  3. isset() is meant to test the existence of a variable… if a value is not POSTed at all, it will fail this. If it is POSTed, it will possibly pass this; this is not a good way to test for empty POST values.

This is some good stuff. Do you think there is anyway that we can create a thread containing pointers for programming languages (maybe even 1 thread per language or a forum category)where people can post pointers and mistakes to avoid that would be too short for an article yet still helpful for novice programmers so that such advice doesn't get lost?


fashizzlepop's Avatar
Member
0 0

…and to keep it spam free? Yah right. Not to mention flame free.

Won't happen.


ghost's Avatar
0 0

Pwnzall wrote: Do you think there is anyway that we can create a thread containing pointers for programming languages (maybe even 1 thread per language or a forum category)where people can post pointers and mistakes to avoid that would be too short for an article yet still helpful for novice programmers so that such advice doesn't get lost? I've already got a few ideas on something very much like this. Also, another HBH member brought up a different perspective that would yield similar results. So, ideally, something like this will be in the future. Near or far? Hard to say… there are a few more pressing items before that happens. For those of you that do have ideas like this (that would benefit the community as a whole, please feel free to PM me or point me to a thread where you initially proposed the idea. If my PM box is full, hold the idea and give me a shout so I can clean it out.

fashizzlepop wrote: …and to keep it spam free? Yah right. Not to mention flame free.

Won't happen. It's not like I can't delete such posts or anything. Mods and admins are freely able to do so. If an idea that could help the community as much as that one was put in place, I'm reasonably certain that it would warrant at least one more moderator. It would also encourage responsible behavior and a unified mindset in the community… both of which have been lacking for a good while now.

If you stay stuck on reasons that something can't be done, it won't be.


ghost's Avatar
0 0

Zephyr_Pure wrote: If my PM box is full, hold the idea and give me a shout so I can clean it out.

Don't you have unlimited PM space? That should come standard if you are an admin, imho.


ghost's Avatar
0 0

SwartMumba wrote: Don't you have unlimited PM space? That should come standard if you are an admin, imho. Mine was on backorder. For now, I don't. :P


ghost's Avatar
0 0

WOW I am pretty amazed that my thread sparked up this little discussion.


yours31f's Avatar
Retired
10 0

I like the idea. If i could get some help with moderation and such, I would be willing to change CTheCode.com up a little bit to support all languages.


ghost's Avatar
0 0

yours31f wrote: I like the idea. If i could get some help with moderation and such, I would be willing to change CTheCode.com up a little bit to support all languages. No offense, but I believe the goal here is to have this be an addition to this site. That would also lend itself to moderation and community-based submissions, rather than content provided by your users. This would improve our community involvement and prevent this from becoming a conflict of interest, so to speak.


yours31f's Avatar
Retired
10 0

Understood. I an see how something of that magnitude could become a conflict but, If I may say so this could help this site significantly.


spyware's Avatar
Banned
0 0

yours31f wrote: Understood. I an see how something of that magnitude could become a conflict but, If I may say so this could help this site significantly.

Symbiote. If I may say so, I most certainly don't think your community can add value to HBH.


yours31f's Avatar
Retired
10 0

Sorry, I was unclear of what I meant, I meant that having that extra category in the forums here, would be good for this community.