PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
preetgowda
Forum Newbie
Posts: 7 Joined: Wed Mar 31, 2004 4:35 am
Post
by preetgowda » Wed Mar 31, 2004 4:35 am
Hi, i have a problem in trying to validate the data so that if a user does not submit a certain field then an error message will come up. I have started to do this( if empty....) but I am stuck on the coding.......
Code: Select all
<html>
<head><title>Submit form For Album Titles</title></head>
<body text="#000000" bgcolor="#feebcc">
<style type="text/css">
body { margin-left: 10%; margin-right: 10%; }
</style>
<center><img src="unicrest.gif" width="154" height="79"
alt="University crest of Liverpool" longdesc="unicrest.html"></center>
<HR SIZE=3 style="background-color: blue">
<body>
<font face = "verdana">
<font size=4>
<center>
<a href = "http://www.csc.liv.ac.uk/~u1phg/UserLogin.php">
Home Page
</a>
</center>
<center>
<a href = "http://www.csc.liv.ac.uk/~u1phg/ContactPage.html">
Contact Page
</a>
</center>
<center>
<a href = "http://www.csc.liv.ac.uk/~u1phg/InfoPage.html">
Information Page
</a>
</center>
</font size=4>
<?
$albumId=$HTTP_POST_VARS['albumId'];
$albumTitle=$HTTP_POST_VARS['albumTitle'];
$classicalTrack=$HTTP_POST_VARS['classicalTrack'];
$cdNumber=$HTTP_POST_VARS['cdNumber'];
$artistBandName=$HTTP_POST_VARS['artistBandName'];
$genre=$HTTP_POST_VARS['genre'];
$label=$HTTP_POST_VARS['label'];
$year=$HTTP_POST_VARS['year'];
$rating=$HTTP_POST_VARS['rating'];
if (empty ($albumId) or empty ($albumTitle) or empty ($artistBandName) or empty ($genre) or empty($year) or empty ($rating))
$query = "INSERT INTO Album(albumId,AlbumTitle,classicalTrack,cdNumber,artistBandName,genre,label,year,rating)
VALUES('$albumId','$albumTitle','$classicalTrack','$cdNumber','$artistBandName','$genre','$label','$year', '$rating')";
$result = mysql_query($query) or die ("Execution failed.");
?>
<form method="post"action="<?php echo $PHP_SELF?>">
<table cellpadding=2 cellspacing=0 border=0>
<td>Album Id :</td><td> <input type="text" size="5" maxlength="10" name="albumId" value="<?echo $albumId; ?>"></td><tr>
<td>Album Title:</td><td> <input type="text" size="30" maxlength="30" name="albumTitle" value="<?echo $albumTitle; ?>"></td></tr>
<td>Classical Track:</td><td> <input type="text" size="30" maxle2th="30" name="classicalTrack"value="<?echo $classicalTrack;?> "></td></tr>
<td>Cd Number:(same as Album ID)</td><td> <input type="text" size="2" maxlength="2" name="cdNumber" value="<?echo $cdNumber;?>"></td></tr>
<td>Artist / Band Name:</td><td> <input type="char" size="30" maxlength="30" name="artistBandName" value="<?echo $artistBandName;?>"> </td><tr>
<td> Genre:(e.g pop, rock, R &B...) </td><td> <input type="char" size="20" maxlength="20" name="genre" value="<?echo $genre;?>"></td><tr>
<td>Label:</td><td> <input type="char" size="20" maxlength="20" name="label" value="<?echo $label;?>"></td><tr>
<td>Year (e.g.yyyy-mm-dd):</td><td> <input type="char" size="10" maxlength="10" name="year"value="<?echo $year;?>"></td><tr>
<td>Rating:(e.g use- bad, ok, good, great)</td><td> <input type="char" size="10" maxlength="10" name="rating" value="<?echo $rating;?>"></td><tr>
</table>
<input type="Submit" name="class" value="Enter Album Title Info.">
<input type="Reset">
</form>
<br> </br>
<p>You Can view the updated General Music Collection with information that you have entered <a href = "http://www.csc.liv.ac.uk/~u1phg/selectAlbum.php"> - Here!</a></p>
<br> </br>
<center><p>To submit or query the other collections choose from below:</p></center>
<center><p> <a href = "http://www.csc.liv.ac.uk/~u1phg/selectClassical.php">Classical Music</a></p>
<p> <a href = "http://www.csc.liv.ac.uk/~u1phg/selectDVD.php">DVDs</a></p>
<p> <a href = "http://www.csc.liv.ac.uk/~u1phg/selectBook.php">Books</a></p></center>
<?
mudkicker
Forum Contributor
Posts: 479 Joined: Wed Jul 09, 2003 6:11 pm
Location: Istanbul, TR
Contact:
Post
by mudkicker » Wed Mar 31, 2004 5:19 am
it should be that way (as an example) :
if(empty($foo) || empty($boo)) {
print "error!";
exit;
}
else
{
// mysql insert.
}
preetgowda
Forum Newbie
Posts: 7 Joined: Wed Mar 31, 2004 4:35 am
Post
by preetgowda » Wed Mar 31, 2004 5:26 am
Thanks, for that I will try this and see if it works!
preetgowda
Forum Newbie
Posts: 7 Joined: Wed Mar 31, 2004 4:35 am
Post
by preetgowda » Wed Mar 31, 2004 9:51 am
I tried doing this but it does not work.Could you please show me using my form of how to do this please?
lostboy
Forum Contributor
Posts: 329 Joined: Mon Dec 30, 2002 8:12 pm
Location: toronto,canada
Post
by lostboy » Wed Mar 31, 2004 9:52 am
i tend to like
Code: Select all
if (isset($$HTTP_POST_VARS['albumId']){
$albumId=$HTTP_POST_VARS['albumId'];
}else{
$err_msg.="Album id not input";
}
then checking to see if the $err_msg is set to something other than and empty string...if it is set, then halt the process and show the form with the $err_msg notes, otherwise proceed with the db query....
Note that you can also set the DB fields to NOT NULL to prevent empty records from getting in...
Also: your EMPTY check does not cover spaces (" ") since that field is no longer empty....Regex is something that you may want to look at since one regex function can accomadate most of your inputs...
hth
magicrobotmonkey
Forum Regular
Posts: 888 Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA
Post
by magicrobotmonkey » Wed Mar 31, 2004 9:52 am
use javascript and don't let them submit the form until its all filled in
JayBird
Admin
Posts: 4524 Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:
Post
by JayBird » Wed Mar 31, 2004 9:57 am
have you taken that code from the guy in this post?
viewtopic.php?t=20215
it looks awfully similar and VERY wrong!
What the hell is going on here
Code: Select all
if (empty ($albumId) or empty ($albumTitle) or empty ($artistBandName) or empty ($genre) or empty($year) or empty ($rating))
Basically, you are saying, if nothing was enetered in the form, put it in the database, but nothing was entered in the form.
The code isn't even structured correclty
mark
JayBird
Admin
Posts: 4524 Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:
Post
by JayBird » Wed Mar 31, 2004 10:04 am
okay, give this a try.
Code: Select all
<html>
<head>
<title>Submit form For Album Titles</title>
</head>
<body text="#000000" bgcolor="#feebcc">
<style type="text/css">
body { margin-left: 10%; margin-right: 10%; }
</style>
<center>
<img src="unicrest.gif" width="154" height="79" alt="University crest of Liverpool" longdesc="unicrest.html">
</center>
<body>
<font face = "verdana"> <font size=4>
<center>
<a href = "http://www.csc.liv.ac.uk/~u1phg/UserLogin.php"> Home Page </a>
</center>
<center>
<a href = "http://www.csc.liv.ac.uk/~u1phg/ContactPage.html"> Contact Page </a>
</center>
<center>
<a href = "http://www.csc.liv.ac.uk/~u1phg/InfoPage.html"> Information Page </a>
</center>
</font size=4>
<?
if (isset($_POST)) { // Only do this bit if the form has been submitted
$albumId=$_POST['albumId'];
$albumTitle=$_POST['albumTitle'];
$classicalTrack=$_POST['classicalTrack'];
$cdNumber=$_POST['cdNumber'];
$artistBandName=$_POST['artistBandName'];
$genre=$_POST['genre'];
$label=$_POST['label'];
$year=$_POST['year'];
$rating=$_POST['rating'];
// If any of the following variables are empty, DO NOT insert in Database
if (!empty($albumId) || !empty($albumTitle) || !empty($artistBandName) || !empty($genre) || !empty($year) || !empty($rating)) {
$query = "INSERT INTO Album(albumId,AlbumTitle,classicalTrack,cdNumber,artistBandName,genre,label,year,rating)
VALUES('$albumId','$albumTitle','$classicalTrack','$cdNumber','$artistBandName','$genre','$label','$year', '$rating')";
$result = mysql_query($query) or die ("Execution failed.");
} else {
echo "Make sure you filled out all the required fields";
}
}
?>
<form method="post"action="<?php echo $PHP_SELF?>">
<table cellpadding=2 cellspacing=0 border=0>
<td>Album Id :</td>
<td> <input type="text" size="5" maxlength="10" name="albumId" value="<? echo $albumId; ?>"></td>
<tr>
<td>Album Title:</td>
<td> <input type="text" size="30" maxlength="30" name="albumTitle" value="<? echo $albumTitle; ?>"></td>
</tr>
<td>Classical Track:</td>
<td> <input type="text" size="30" maxle2th="30" name="classicalTrack"value="<? echo $classicalTrack; ?> "></td>
</tr> <td>Cd Number:(same as Album ID)</td>
<td> <input type="text" size="2" maxlength="2" name="cdNumber" value="<? echo $cdNumber;?>"></td>
</tr> <td>Artist / Band Name:</td>
<td> <input type="char" size="30" maxlength="30" name="artistBandName" value="<? echo $artistBandName; ?>"> </td>
<tr>
<td> Genre:(e.g pop, rock, R &B...) </td>
<td> <input type="char" size="20" maxlength="20" name="genre" value="<? echo $genre; ?>"></td>
<tr>
<td>Label:</td>
<td> <input type="char" size="20" maxlength="20" name="label" value="<?e cho $label;?>"></td>
<tr>
<td>Year (e.g.yyyy-mm-dd):</td>
<td> <input type="char" size="10" maxlength="10" name="year"value="<? echo $year; ?>"></td>
<tr>
<td>Rating:(e.g use- bad, ok, good, great)</td>
<td> <input type="char" size="10" maxlength="10" name="rating" value="<? echo $rating; ?>"></td>
<tr>
</table>
<hr size=3 style="background-color: blue">
<input type="Submit" name="class" value="Enter Album Title Info.">
<input type="Reset">
</form>
<br>
</br>
<p>You Can view the updated General Music Collection with information that you
have entered <a href = "http://www.csc.liv.ac.uk/~u1phg/selectAlbum.php"> -
Here!</a></p>
<br>
</br>
<center>
<p>To submit or query the other collections choose from below:</p>
</center>
<center>
<p> <a href = "http://www.csc.liv.ac.uk/~u1phg/selectClassical.php">Classical
Music</a></p>
<p> <a href = "http://www.csc.liv.ac.uk/~u1phg/selectDVD.php">DVDs</a></p>
<p> <a href = "http://www.csc.liv.ac.uk/~u1phg/selectBook.php">Books</a></p>
</center>
Mark
preetgowda
Forum Newbie
Posts: 7 Joined: Wed Mar 31, 2004 4:35 am
Post
by preetgowda » Wed Mar 31, 2004 10:09 am
so could someone please tell me what is thre right way of structuring the code to check for empty fields- please, thanks
JayBird
Admin
Posts: 4524 Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:
Post
by JayBird » Wed Mar 31, 2004 10:12 am
I just did in the previous post
Mark
markl999
DevNet Resident
Posts: 1972 Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)
Post
by markl999 » Wed Mar 31, 2004 11:07 am
I prefer to use a method such as :
Code: Select all
<?php
if(!empty($_POST)){
$required = array(
'albumId', 'albumTitle', 'classicalTrack',
'cdNumber', 'artistBandName', 'genre',
'label', 'year', 'rating'
);
foreach($required as $req){
if(empty($_POST[$req])){
$missing[] = $req;
} else {
$values[] = $_POST[$req];
}
}
if(empty($missing)){
mysql_connect('localhost', 'user', 'pass') or die(mysql_error());
mysql_select_db('thedb') or die(mysql_error());
$sql = "INSERT INTO Album (".join(',', $required).") VALUES (".
join(',', $values).")";
mysql_query($sql) or die(mysql_error());
} else {
echo 'The following fields need to be filled in : '.join(' ', $missing);
}
}
?>
<!-- put the form here -->
The code above has a few errors.
if (isset($_POST)) { <-- $_POST is always set regardless of if the form was submitted or not.
$albumId=$_POST['albumId']; <-- You don't really need to created separate vars to hold the data, you already have them in $_POST. Also if the field wasn't posted (not filled in) then you'll generate an Undefined Index warning.