Hi, i started working with php about a week ago so please go easy on me. I’m writing a little address book script so that i get to grips with the basic ideas in PHP. I wanted to check my input boxes so that if a valid email isn’t entered it will spit out an error and not submit the data to the database until correct info has been entered.
I’ve managed to do the regex checking that will tell me if a valid email has been entered or not but the script still carries on regardless and prints the error once the data has been submitted. I’m guessing i need to print the error in the html bit of the code so that it’s actually displayed next to the box it’s just how do i go about doing it. Please could someone out there help me as i’d love to get this working.
Code
[php]<?php
if (isset($_POST[‘submitted’])) {
$name = mysql_escape_string(trim($_POST[‘name’]));
$address = mysql_escape_string(trim($_POST[‘address’]));
$postcode = mysql_escape_string(trim($_POST[‘postcode’]));
$telephone = mysql_escape_string(trim($_POST[‘telephone’]));
$fixedemail = mysql_escape_string(trim($_POST[‘email’]));
$imglocation = mysql_escape_string(‘http://www.mysite.com/uploader1/images/’);
// This bit of code checks the form for correctly entered information
if (eregi(’^([-a-z0-9.]+)@([-a-z0.9]+.+[a-z]{2,6})$’,$fixedemail,$email))
{ print “” ;}
else { print "You entered an invalid email address. Please enter username@domain
" ; }
// This bit puts the file in the $file variable into the folder on the server and changes its name so it isn't overwritten
$file_name = uniqid("img").".jpg";
if ($file_name !="") {
copy ("$file", "/var/www/vhosts/mysite.com/httpdocs/uploader1/images/$file_name")
or die ("Sorry there was a problem");}
// This is the SQL part of the code
$dbid = mysql_connect ('localhost', 'address', 'password');
mysql_select_db('addresses',$dbid)
or die ("Cannot find database");
$query = "INSERT INTO `book` (`aid`, `name`, `address`, `postcode`, `telephone`, `email`, `picture`) VALUES ('', '$name', '$address', '$postcode', '$telephone', '$email', '$imglocation$file_name')";
$result = mysql_query($query,$dbid)
or die("INSERT error:".mysql_error());
echo 'Row inserted and image uploaded';
exit;
}
?>
Name: | |
Address: | |
Postcode: | |
Telephone: | |
Email: | |
Image: |
|