473,411 Members | 2,184 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

Null isn't empty

Hi All
I'm new to php my problem is on sending the form if a specific field is empty how would I stop it being sent. Or would I need to translate empty to NULL or is this a mysql prob with a query. If so could a mod move this to the relevant place pls.
Any help much appreciated.
<?php
$db_host = "localhost";
$db_user = "user";
$db_pwd = "password";
$db_name = "users";
mysql_connect($db_host, $db_user, $db_pwd);
mysql_select_db($db_name);
?>
<html>
<head>
<title>My first MySQL form submission</title>
</head>
<body>
<?php
if (!isset($_POST['submit'])) {
?>
<form action="" method="post">
Name: <input type="text" name="name"><br>
Password: <input type="password" name="pass"><br>
Re-enter: <input type="password" name="pass"><br>
Email: <input type="text" name="email"><br>
<input type="submit" name="submit" value="Submit!">
</form>
<?php
} else {
$name = $_POST['name'];
$pass = $_POST['pass'];
$enc = md5($pass);
$enc1 = md5($name);
$encf = md5($enc1, $enc);
$email = $_POST['email'];
mysql_query("INSERT INTO `info` (name, pass, email) VALUES ('$name', '$encf', '$email')");
echo "Success! You've been added!";
}
?>
</body>
</html></body>
</html>
Could I use something like ($_POST = "") | also whats the correct syntax to close if is it ]
also to compare a string would this be correct ($pass = $pass1) |
Thanks before hand.
Stef
Jul 12 '06 #1
5 4684
sashi
1,754 Expert 1GB
Hi there,

coming up with a simple client side validation using javascript would be a good idea.. you can invoke the validation funtion on the OnSubmit event.. look at the below example..

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <script type="text/javascript">
  4. function validate_required(field,alerttxt)
  5. {
  6. with (field)
  7. {
  8. if (value==null||value=="")
  9.   {alert(alerttxt);return false}
  10. else {return true}
  11. }
  12. }function validate_form(thisform)
  13. {
  14. with (thisform)
  15. {
  16. if (validate_required(email,"Email must be filled out!")==false)
  17.   {email.focus();return false}
  18. }
  19. }
  20. </script>
  21. </head>
  22. <body>
  23. <form action="submitpage.htm" onsubmit="return validate_form(this)" method="post">
  24. Email: <input type="text" name="email" size="30">
  25. <input type="submit" value="Submit"> 
  26. </form>
  27. </body>
  28. </html>
  29.  
Jul 12 '06 #2
Thanks Sashi
Took a couple of secs to get the page it went to right. Tried to incorparate my php into the java page but all works fine now, thanks :)
Stef
Jul 13 '06 #3
Just a quick addition, could you tell me do I compare two strings in php like this (!isset($_POST['pass'])=($_POST['pass1'])) |
Stef

Thanks
Jul 13 '06 #4
sashi
1,754 Expert 1GB
Hi there,

am not a php kind of person.. yet i found some tips & tricks for you.. see below.. hope it helps you to get started.. good luck my fren.. :)

isset -- Determine whether a variable is set
Description
bool isset ( mixed var [, mixed var [, ...]] )


Returns TRUE if var exists; FALSE otherwise.

If a variable has been unset with unset(), it will no longer be set. isset() will return FALSE if testing a variable that has been set to NULL. Also note that a NULL byte ("\0") is not equivalent to the PHP NULL constant.

strcmp -- Binary safe string comparison
Description
int strcmp ( string str1, string str2 )

Returns < 0 if str1 is less than str2; > 0 if str1 is greater than str2, and 0 if they are equal.

Note that this comparison is case sensitive.

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $pw1 = "yeah";
  3. $pw2 = "yeah";
  4.  
  5. if (strcmp($pw1, $pw2)) {  // This returns false.
  6.    // $pw1 and $pw2 are NOT the same.
  7. } else {
  8.    // $pw1 and $pw2 are the same.
  9. }
  10.  
  11. Where the use of the == operator would give us.:
  12. if ($pw1==$pw2) {    // This returns true.
  13.    // $pw1 and $pw2 are the same.
  14. } else {
  15.    // $pw1 and $pw2 are NOT the same.
  16. }
  17. ?>
  18.  
http://my2.php.net/manual/en/
Jul 13 '06 #5
Banfa
9,065 Expert Mod 8TB
Just a quick addition, could you tell me do I compare two strings in php like this (!isset($_POST['pass'])=($_POST['pass1'])) |
Stef

Thanks
What sashi says about using strcmp is good, you can get lots of help from

www.php.net

in the document section, in fact you can download a help file (which I have) which contains everything so you don't keep having to go back to your web-browser.

comparison in php is one of

== equals
=== precisely the same as

as an example

Expand|Select|Wrap|Line Numbers
  1. $a = false;
  2. $b = 0;
  3.  
  4. if ( $a == $b)
  5. {
  6.     echo("A == B");
  7. }
  8.  
  9.  
  10. if ( $a === $b)
  11. {
  12.     echo("A === B");
  13. }
  14.  
this will only output the first echo statement. This is because false is a boolean value and 0 is a integer value. false when convert to a integer takes the value 0 so $a is equal to $b, however $a and $b are not precisely the same because they are of different types.

This is quite an import distinction when you call some library functions which return a value or false if they fail.
Jul 13 '06 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: David Shorthouse | last post by:
Hey folks, This one's probably been asked before but I'm tired of trying to find a solution (if there is one!). I have an asp update page and I'd like to convert all empty fields to NULL prior...
7
by: BlueDragon | last post by:
I don't know enough math to demonstrate that any numerical operation with a null should yield a null; although I would guess that it's true. I just don't buy it, however, when dealing with strings...
6
by: Tino Wildenhain | last post by:
Hi, SELECT 'abc'::text || 'def'::text; returns 'abcdef' as we know. SELECT 'abc'::text || ''::text; returns 'abc'
8
by: Lyn | last post by:
I am trying to get my head around the concept of default, special or empty values that appear in Access VBA, depending on data type. The Access Help is not much (help), and the manual that I have...
8
by: sugaray | last post by:
Hi, I just came upon this code snippet which parses a string stored in buf with comma(,) as delimiter and store each substring into args, the question I'm having here is that I don't get why in the...
5
by: David Sworder | last post by:
Hi, I've created a UserControl-derived class called MyUserControl that is able to persist and subsequently reload its state. It exposes two methods as follows: public void Serialize(Stream...
7
by: kumar.senthil | last post by:
Hi, I'm using XmlSerializer to create an object from the XML string. I would like to know whether I can get a null value for an empty XML element. Actually the XmlSerializer assigns "" (empty...
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
6
by: linq936 | last post by:
Hi, I have the following code: #include <stdio.h> int main(void){ char* str1 = "abc"; char* str2 = '\0'; if ( strstr(str1, str2) == NULL ){ printf("yes\n");
7
by: Peter K | last post by:
Hi - should the following compile? I ask because a Guid is a struct and can never be null... The worst the compiler throws at me is an "unreachable code" warning. public string GetData(Guid id)...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.