I have a form I need to create in PHP that needs to validate the data that is entered and send it through if everything is in order. It took me awhile to get to it but it redirects if all forms are filled out properly. However, the reception page does not get the data from the form page. Can I apply two actions to the form? How else do I send the data both to itself ($_SERVER["PHP_SELF"]) and another page? I am getting the Undefined index error on all my variable definitions in the reception page.
Here is the code for the form page:
<!DOCTYPE html>
<!-- Jonathan DeMars
4/20/2017
http://chelan.highline.edu/~jon_demars3/116/magazine.html
-->
<html lang="en">
<head>
<meta charset="utf-8">
<title>GQ - Subscription</title>
<meta name="description" content="Register to GQ">
<meta name="author" content="CSCI 116 Student: Jonathan DeMars">
<link rel="stylesheet"
type="text/css"
href="styles.css">
</head>
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
?>
<body>
<h1>Register to GQ</h1>
<p>GQ or "Gentleman's Quarterly" is an international monthly men's magazine based in New York City. The publication focuses on fashion, style,
and culture for men; though articles on food, movies, fitness, sex, music, travel, sports, technology,
and books are also featured.</p>
<hr>
<br>
<p><strong>Please complete the following form:</strong></p>
<?php
$firstnameErr = $lastnameErr = $addressErr = $cityErr = $zipcodeErr = $monthsErr = "";
$first_name = $last_name = $address = $city = $zipcode = $months = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$first_name = test_input($_POST["first_name"]);
$last_name = test_input($_POST["last_name"]);
$address = test_input($_POST["address"]);
$city = test_input($_POST["city"]);
$zipcode = test_input($_POST["zipcode"]);
$months = test_input($_POST["months"]);
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["first_name"])) {
$firstnameErr = "First name is required";
} else {
$first_name = test_input($_POST["first_name"]);
if (!preg_match("/^[a-zA-Z ]*$/",$first_name)) {
$firstnameErr = "Only letters and white space allowed";
}
}
if (empty($_POST["last_name"])) {
$lastnameErr = "Last name is required";
} else {
$last_name = test_input($_POST["last_name"]);
if (!preg_match("/^[a-zA-Z ]*$/",$last_name)) {
$lastnameErr = "Only letters and white space allowed";
}
}
if (empty($_POST["address"])) {
$addressErr = "Address is required";
} else {
$address = test_input($_POST["address"]);
}
if (empty($_POST["city"])) {
$cityErr = "City is required";
} else {
$city = test_input($_POST["city"]);
if (!preg_match("/^[a-zA-Z ]*$/",$city)) {
$cityErr = "Only letters and white space allowed";
}
}
if (empty($_POST["zipcode"])) {
$zipcodeErr = "Zipcode is required";
} else {
$zipcode = test_input($_POST["zipcode"]);
if (!preg_match("/^([0-9]{5})(-[0-9]{4})?$/i",$zipcode)) {
$zipcodeErr = "Please enter a valid zipcode.";
}
}
if (empty($_POST["months"])) {
$monthsErr = "You must subscribe for 1 or more months.";
} else {
$months = test_input($_POST["months"]);
if (!preg_match("/^[1-9][0-9]*$/",$months)) {
$monthsErr = "Must enter a valid number.";
}
}
}
if($firstnameErr == "" && $lastnameErr == "" && $addressErr == "" && $cityErr == "" && $zipcodeErr == "" && $monthsErr == ""){
header('Location: magazinevalidation_post.php');
exit();
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<p>First Name: <input type="text" name="first_name" size="15" value="<?php echo $first_name;?>" <span class="error">* <?php echo $firstnameErr;?></span>
Last Name: <input type="text" name="last_name" size="15" value="<?php echo $last_name;?>" <span class="error">* <?php echo $lastnameErr;?></span></p>
<br>
<p>Address: <input type="text" name="address" size="20" value="<?php echo $address;?>" <span class="error">* <?php echo $addressErr;?></span></p>
<p>City: <input type="text" name="city" size="15" value="<?php echo $city;?>" <span class="error">* <?php echo $cityErr;?></span> </p>
<p>State: <select name="state">
<option value="AL">Alabama</option>
<option value="AK">Alaska</option>
<option value="AZ">Arizona</option>
<option value="AR">Arkansas</option>
<option value="CA">California</option>
<option value="CO">Colorado</option>
<option value="CT">Connecticut</option>
<option value="DE">Delaware</option>
<option value="DC">District of Columbia</option>
<option value="FL">Florida</option>
<option value="GA">Georgia</option>
<option value="HI">Hawaii</option>
<option value="ID">Idaho</option>
<option value="IL">Illinois</option>
<option value="IN">Indiana</option>
<option value="IA">Iowa</option>
<option value="KS">Kansas</option>
<option value="KY">Kentucky</option>
<option value="LA">Louisiana</option>
<option value="ME">Maine</option>
<option value="MD">Maryland</option>
<option value="MA">Massachusetts</option>
<option value="MI">Michigan</option>
<option value="MN">Minnesota</option>
<option value="MS">Mississippi</option>
<option value="MO">Missouri</option>
<option value="MT">Montana</option>
<option value="NE">Nebraska</option>
<option value="NV">Nevada</option>
<option value="NH">New Hampshire</option>
<option value="NJ">New Jersey</option>
<option value="NM">New Mexico</option>
<option value="NY">New York</option>
<option value="NC">North Carolina</option>
<option value="ND">North Dakota</option>
<option value="OH">Ohio</option>
<option value="OK">Oklahoma</option>
<option value="OR">Oregon</option>
<option value="PA">Pennsylvania</option>
<option value="RI">Rhode Island</option>
<option value="SC">South Carolina</option>
<option value="SD">South Dakota</option>
<option value="TN">Tennessee</option>
<option value="TX">Texas</option>
<option value="UT">Utah</option>
<option value="VT">Vermont</option>
<option value="VA">Virginia</option>
<option value="WA">Washington</option>
<option value="WV">West Virginia</option>
<option value="WI">Wisconsin</option>
<option value="WY">Wyoming</option>
</select>
<p>Zip Code: <input type="text" name="zipcode" size="4" value="<?php echo $zipcode;?>" <span class="error">* <?php echo $zipcodeErr;?></p>
<br>
<br>
<p>How many months would you like to subscribe? <input type="text" name="months" size="1" value="<?php echo $months;?>" <span class="error">* <?php echo $monthsErr;?></span></p>
<br>
<br>
<input type="submit" name="submit" value="Continue" />
</form>
<p>Return to index page</p>
</body>
</html>
And here is the code for the reception page:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>GQ - Thank you!</title>
<meta name="description" content="Thank you for Registering to GQ">
<meta name="author" content="CSCI 116 Student: Jonathan DeMars">
<link rel="stylesheet"
type="text/css"
href="styles.css">
</head>
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
?>
<body>
<?php
echo "<h1>Order Summary</h1><hr>";
define("TAX", "0.10");
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$address = $_POST['address'];
$city = $_POST['city'];
$state = $_POST['state'];
$zipcode = $_POST['zipcode'];
$months = $_POST['months'];
$name = $first_name . ' ' . $last_name;
$monthlyrate = 9.99;
$subtotal = $months * $monthlyrate;
$taxtotal = $subtotal * TAX;
$grandtotal = $subtotal + $taxtotal;
print "<p><strong>$name</strong></p>";
print "<strong>$address</strong><br>";
print "<strong>$city, $state $zipcode</strong><br>";
print "<p>Your Subscription: <strong>$months months</strong></p>";
print "Monthly Payments: <strong>$$subtotal</strong><br>";
print "Tax:";
echo "<strong> $";
echo round($taxtotal, 2, PHP_ROUND_HALF_UP);
echo "</strong>";
print "<br>Total:";
echo "<strong> $";
echo round($grandtotal, 2, PHP_ROUND_HALF_UP);
echo "</strong>";
print "<div><p>Thank you, $name, for your subscription. You will recieve your first copy of GQ within the week!
</p></div>";
?>
<p>Return to index page</p>
</body>
</html>
You can use the session for pass the data to another form. Before redirect set the values in session after you can access these data in form2 with using session variables.
$_SESSION - probably best way
<?php
session_start();
$_SESSION['first_name'] = $first_name;
$_SESSION['last_name'] = $last_name;
$_SESSION['address'] = $address;
$_SESSION['city'] = $city;
$_SESSION['address'] = $address;
$_SESSION['zipcode'] = $zipcode;
$_SESSION['months'] = $months;
header('Location: magazinevalidation_post.php');
exit();
In magazinevalidation_post.php file you can access the value with
<?php
session_start();
echo $_SESSION['first_name'];
echo $_SESSION['last_name']
echo $_SESSION['address'];
echo $_SESSION['city'];
echo $_SESSION['address'];
echo $_SESSION['zipcode'];
echo $_SESSION['months'];
?>
Related
Html Form:
<form>
<select name="country[]" id="country" multiple>
<option value="any">any</option>
<option value="India">India</option>
<option value="Canada">Canada</option>
<option value="UK">UK</option>
<option value="USA">USA</option>
<option value="Australia">Australia</option>
</select>
</form>
PHP Code
<?php
$country = $_REQUEST['country'];
if($country=="")
$countrysql = "";
else
{
if($country == "Any") $countrysql = "";
else
{
$country = str_replace(",","','",$country);
$countrysql = " and Country in ('$country')";
}
}
$queryString = "SELECT * FROM register where $countrysql";
?>
I have created a form in PHP and I want to search multiple options. I already created table Register and a column Country. I am getting the result If I give single value. If I give multiple I am not getting the result. Please help.
You evaluate in if($country == "Any") the word Any is not equal to any in option <option value="any">any</option>
But I suggest this php code:
<?php
$country="";
$countryError="";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["country"])){
$countryError = "Country is required";
}else{
$country = $_POST["country"];
}
if($country == "Any") {
$queryString = "SELECT * FROM register";
}else{
$queryString = "SELECT * FROM register where Country in ('$country')";
}
// Print the SQL string:
echo $queryString;
}
?>
The html tags:
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<select name="country" id="country" multiple>
<option value="Any">any</option>
<option value="India">India</option>
<option value="Canada">Canada</option>
<option value="UK">UK</option>
<option value="USA">USA</option>
<option value="Australia">Australia</option>
</select>
<input type="submit" name="submit" value="Submit">
</form>
<span class="error"><?php echo $countryError;?></span>
I am trying to create an html search form using a similar code as posted below.
When I submit the form, I want to submit to PHP_SELF
I want to use php validation code to filter the data.
When I submit the form, I cannot figure out how to get the results to post to a new page without displaying the form.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "xyz_database";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$showHtml = true;
$month = $day = $year = "";
$monthErr = $dayErr = $yearErr = "";
$errorMessage = "Oops..Please correct the item(s) highlighted in red on the form below and re-submit";
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Month error & filter check code....
if (empty($_POST["month"])) {
$month = "";
} else {
$month = test_input($_POST["month"]);
if (!preg_match("/^[a-zA-Z ]*$/",$month)) {
$monthErr = "An invalid entry has been detected. Please reset this form and re-submit.";
}
}
// Day error & filter check code....
if (empty($_POST["day"])) {
$day = "";
} else {
$day = test_input($_POST["day"]);
if (!is_numeric($day)) {
$dayErr = "Day Found - An invalid entry has been detected. Please reset this form and re-submit.";
}
}
// Year error & filter check code....
if (empty($_POST["year"])) {
$year = "";
} else {
$year = test_input($_POST["year"]);
if (!is_numeric($year)) {
$yearErr = "Year Found - An invalid entry has been detected. Please reset this form and re-submit.";
}
}
if (empty($monthErr) and empty($dayErr) and empty($yearErr)) {
$showHtml = false;
$value1 = $_POST['month'];
$value2 = $_POST['day'];
$value3 = $_POST['year'];
$sql = "SELECT * FROM xyz_test_database WHERE month = ('$value1') AND day = ('$value2') AND year = ('$value3')";
$result = $conn->query($sql);
if ($result->num_rows > 0) {echo "<br><br><h2>Search Results</h2>
<table><tr>
<th>ID</th>
<th>Time Stamp</th>
<th>Month</th>
<th>Day</th>
<th>Year</th>
</tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr>
<td>".$row["id"]."</td>
<td>".$row["time_stamp"]."</td>
<td>".$row["month"]."</td>
<td>".$row["day"]."</td>
<td>".$row["year"]."</td>
</tr>";
}
echo "</table>";
} else {
echo "<p id='no_results'>Sorry - No Results Found :( </p>";
}
}
}
$conn->close();
exit ();
?>
<?php
if ($showHtml)
{
?>
<!DOCTYPE html>
<meta charset="UTF-8">
<html>
<head>
</head>
<body>
<form name="form1" method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<select id="item_select" name="month">
<option value="">Select Month</option>
<option value="January">January</option>
<option value="February">February</option>
<option value="March">March</option>
<option value="April">April</option>
<option value="May">May</option>
<option value="June">June</option>
<option value="July">July</option>
<option value="August">August</option>
<option value="September">September</option>
<option value="October">October</option>
<option value="November">November</option>
<option value="December">December</option>
</select>
<select id="item_select" name="day">
<option value="">Day</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
<select id="item_select" name="year">
<option value="">Year</option>
<option value="2015">2015</option>
<option value="2014">2014</option>
<option value="2013">2013</option>
<option value="2012">2012</option>
<option value="2011">2011</option>
<option value="1975">1975</option>
</select>
<br>
<span class="error"><?php echo $monthErr;?></span>
<span class="error"><?php echo $dayErr;?></span>
<span class="error"><?php echo $yearErr;?></span>
<br>
<input type="Submit" id="submit" name="submit" value="Submit Search" style="width: 120px; color: blue;"/>
</form>
</body>
</html>
<?php
}
?>
There are a number of ways to achieve this. You can put an if statement around your html code so that it only displays if certain conditions (e.g. results aren't returned) are met.
One really simple way of doing this is to set a boolean value if results are returned. For example:
<?php
$showHtml = true;
...
if($result->num_rows > 0)
{
$showHtml = false;
...
}
...
$conn->close();
if($showHtml)
{
?>
<!DOCTYPE html>
...
</html>
<?php
}
?>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "xyz_database";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$showHtml = true;
$month = $day = $year = "";
$monthErr = $dayErr = $yearErr = "";
$errorMessage = "Oops..Please correct the item(s) highlighted in red on the form below and re-submit";
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Month error & filter check code....
if (empty($_POST["month"])) {
$month = "";
} else {
$month = test_input($_POST["month"]);
if (!preg_match("/^[a-zA-Z ]*$/",$month)) {
$monthErr = "An invalid entry has been detected. Please reset this form and re-submit.";
}
}
// Day error & filter check code....
if (empty($_POST["day"])) {
$day = "";
} else {
$day = test_input($_POST["day"]);
if (!is_numeric($day)) {
$dayErr = "Day Found - An invalid entry has been detected. Please reset this form and re-submit.";
}
}
// Year error & filter check code....
if (empty($_POST["year"])) {
$year = "";
} else {
$year = test_input($_POST["year"]);
if (!is_numeric($year)) {
$yearErr = "Year Found - An invalid entry has been detected. Please reset this form and re-submit.";
}
}
if (empty($monthErr) and empty($dayErr) and empty($yearErr)) {
$showHtml = false;
$value1 = $_POST['month'];
$value2 = $_POST['day'];
$value3 = $_POST['year'];
$sql = "SELECT * FROM xyz_test_database WHERE month = ('$value1') AND day = ('$value2') AND year = ('$value3')";
$result = $conn->query($sql);
if ($result->num_rows > 0) {echo "<br><br><h2>Search Results</h2>
<table><tr>
<th>ID</th>
<th>Time Stamp</th>
<th>Month</th>
<th>Day</th>
<th>Year</th>
</tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr>
<td>".$row["id"]."</td>
<td>".$row["time_stamp"]."</td>
<td>".$row["month"]."</td>
<td>".$row["day"]."</td>
<td>".$row["year"]."</td>
</tr>";
}
echo "</table>";
} else {
echo "<p id='no_results'>Sorry - No Results Found :( </p>";
}
}
}
$conn->close();
exit ();
?>
<?php
if ($showHtml)
{
?>
<!DOCTYPE html>
<meta charset="UTF-8">
<html>
<head>
</head>
<body>
<form name="form1" method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<select id="item_select" name="month">
<option value="">Select Month</option>
<option value="January">January</option>
<option value="February">February</option>
<option value="March">March</option>
<option value="April">April</option>
<option value="May">May</option>
<option value="June">June</option>
<option value="July">July</option>
<option value="August">August</option>
<option value="September">September</option>
<option value="October">October</option>
<option value="November">November</option>
<option value="December">December</option>
</select>
<select id="item_select" name="day">
<option value="">Day</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
<select id="item_select" name="year">
<option value="">Year</option>
<option value="2015">2015</option>
<option value="2014">2014</option>
<option value="2013">2013</option>
<option value="2012">2012</option>
<option value="2011">2011</option>
<option value="1975">1975</option>
</select>
<br>
<span class="error"><?php echo $monthErr;?></span>
<span class="error"><?php echo $dayErr;?></span>
<span class="error"><?php echo $yearErr;?></span>
<br>
<input type="Submit" id="submit" name="submit" value="Submit Search" style="width: 120px; color: blue;"/>
</form>
</body>
</html>
<?php
}
?>
I have a Tour Search application where user can search for available tours based on three different parameters- Region, Country and Duration. Currently the code which I am using is showing the Output Result in the same page. I want the output result to show in a different page.
Below is my PHP Code:
<?php
mysql_connect("localhost", "root", "");
mysql_select_db("byp");
if(isset($_POST['submit'])){
$region=$_POST['region'];
$country=$_POST['country'];
$duration=$_POST['duration'];
//define the index for the All option
$optionAllValue = 0; //add here the option index value used for the 'All' option
//define the where clause for the query
//in order to avoid many conditions verifications, we start it as 1=1
$whereClause = "1=1";
//now we check if the option selected for each field is not the value defined for the option 'All'
//this is just an example, and the best would be to create a function to avoid the replication of code
if($region != $optionAllValue)
{
$whereClause = $whereClause." and region='$region'";
}
if($country != $optionAllValue)
{
$whereClause = $whereClause." and country='$country'";
}
if($duration != $optionAllValue)
{
$whereClause = $whereClause." and duration='$duration'";
}
$query = "select * from byp_tour where ".$whereClause;
//original query select * from byp_tour where region='$region' and country='$country' and duration='$duration'"
$tour = mysql_query($query);
$tourNum = mysql_num_rows($tour);
if($tourNum >0){
while($result=mysql_fetch_array($tour)){
$tour_name = $result['tour_name'];
$tour_detail = $result['tour_detail'];
echo "Tour Name: $tour_name"; // HERE IS THE OUTPUT RESULT
echo "<br />";
echo "Tour Detail: $tour_detail";
echo "<br />";
echo "<br />";
echo "<br />";
}
}
else{
echo "No Tour Found";
echo "<br />";
echo "<br />";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>BYP Test</title>
</head>
<body>
<form action="searchtest.php" method="post">
<div>
<label>Region</label>
<select id="region" name="region">
<option value="0">All</option>
<option value="1">South East Asia</option>
<option value="2">Africa</option>
<option value="3">Europe</option>
<option value="4">America</option>
<option value="5">Australia</option>
</select>
</div>
<div>
<label>Country</label>
<select id="country" name="country">
<option value="0">All</option>
<option value="1">Cambodia</option>
<option value="2">Thailand</option>
<option value="3">Vietnam</option>
<option value="4">Myanmar</option>
<option value="5">Laos</option>
<option value="6">Ethiopia</option>
<option value="7">France</option>
<option value="8">New York City</option>
<option value="9">Melbourne</option>
</select>
</div>
<div>
<label>Duration</label>
<select id="duration" name="duration">
<option value="0">All</option>
<option value="1">5 Days</option>
<option value="2">10 Days</option>
</select>
</div>
<input type="submit" name="submit" value="submit" />
</form>
</body>
</html>
You need to add target="_blank" to <form>:
<form action="searchtest.php" method="post" target="_blank">
hey guys need to pick your brains, i currently have a form set up that uses 6 radio buttons which do 2 things.
a, is supposed to set my account field in mysqli which uses a enym field with the values of a,b,c ect
b, shows a div that includes my resgistration form showing the fields i need based on the account.
the problem i have faced is getting the the data into mysqli of which radio button is selected or "checked". all the other fileds in the form post into mysql fine its just getting the radio button to post its value.
ok php is as follows
<?php
$errorMsg = "";
// First we check to see if the form has been submitted
if (isset($_POST['firstname'])){
//Connect to the database through our include
include_once "connect_to_mysql.php";
// Filter the posted variables
$username = preg_replace("[^A-Za-z0-9]", "", $_POST['username']); // filter everything but numbers and letters
$firstname = preg_replace("[^A-Za-z]", "", $_POST['firstname']); // filter everything but letters
$surname = preg_replace("[^A-Za-z]", "", $_POST['surname']); // filter everything but letters
$accounttype = preg_replace("[^a-z]", "", $_POST['accounttype']); // filter everything but lowercase letters
$b_m = preg_replace('#[^0-9]#i', '', $_POST['birth_month']); // filter everything but numbers
$b_d = preg_replace('#[^0-9]#i', '', $_POST['birth_day']); // filter everything but numbers
$b_y = preg_replace('#[^0-9]#i', '', $_POST['birth_year']); // filter everything but numbers
$email = stripslashes($_POST['email']);
$email = strip_tags($email);
$email = mysql_real_escape_string($email);
$password = preg_replace("[^A-Za-z0-9]", "", $_POST['password']); // filter everything but numbers and letters
$pf = preg_replace("[^a-z]", "", $_POST['pf']);
$sa = preg_replace("[^a-z]", "", $_POST['sa']);
$ba = preg_replace("[^a-z]", "", $_POST['ba']);
$ve = preg_replace("[^a-z]", "", $_POST['be']);
$bu = preg_replace("[^a-z]", "", $_POST['bu']);
$se = preg_replace("[^a-z]", "", $_POST['se']);
// Check to see if the user filled all fields with
// the "Required"(*) symbol next to them in the join form
// and print out to them what they have forgotten to put in
if((!$username) || (!$firstname) || (!$surname) || (!$accounttype) || (!$b_m) || (!$b_d) || (!$b_y) || (!$email) || (!$password)){
$errorMsg = "You did not submit the following required information!<br /><br />";
if(!$username){
$errorMsg .= "--- User Name";
} else if(!$accounttype){
$errorMsg .= "--- Account Type";
} else if(!$b_m){
$errorMsg .= "--- Birth Month";
} else if(!$b_d){
$errorMsg .= "--- Birth Day";
} else if(!$b_y){
$errorMsg .= "--- Birth year";
} else if(!$firstname){
$errorMsg .= "--- First Name";
} else if(!$surname){
$errorMsg .= "--- Surname";
} else if(!$email){
$errorMsg .= "--- Email Address";
} else if(!$password){
$errorMsg .= "--- Password";
}
} else {
// Database duplicate Fields Check
$sql_username_check = mysql_query("SELECT id FROM memberstable WHERE username='$username' LIMIT 1");
$sql_email_check = mysql_query("SELECT id FROM memberstable WHERE email='$email' LIMIT 1");
$username_check = mysql_num_rows($sql_username_check);
$email_check = mysql_num_rows($sql_email_check);
if ($username_check > 0){
$errorMsg = "<u>ERROR:</u><br />Your User Name is already in use inside our system. Please try another.";
} else if($email_check > 0){
$errorMsg = "<u>ERROR:</u><br />Your Email address is already in use inside our system. Please try another.";
} else {
// Add MD5 Hash to the password variable
$hashedPass = md5($password);
// Convert Birthday to a DATE field type format(YYYY-MM-DD) out of the month, day, and year supplied
$full_birthday = "$b_y-$b_m-$b_d";
// Add user info into the database table, claim your fields then values
$sql = mysql_query("INSERT INTO memberstable (username, firstname, surname, accounttype, email, birthday, password)
VALUES('$username','$firstname','$surname','$accounttype','$email','$full_birthday','$hashedPass')") or die (mysql_error());
// Get the inserted ID here to use in the activation email
$id = mysql_insert_id();
// Create directory(folder) to hold each user files(pics, MP3s, etc.)
mkdir("memberFiles/$id", 0755);
// Start assembly of Email Member the activation link
$to = "$email";
// Change this to your site admin email
$from = "admin#getscene.com";
$subject = "Complete your registration";
//Begin HTML Email Message where you need to change the activation URL inside
$message = '<html>
<body bgcolor="#FFFFFF">
Hi ' . $firstname . ',
<br /><br />
You must complete this step to activate your account with us.
<br /><br />
Please click here to activate now >>
<a href="http://www.getscene.com/activation.php?id=' . $id . '">
ACTIVATE NOW</a>
<br /><br />
Your Login Data is as follows:
<br /><br />
E-mail Address: ' . $email . ' <br />
Password: ' . $password . '
<br /><br />
Thanks!
</body>
</html>';
// end of message
$headers = "From: $from\r\n";
$headers .= "Content-type: text/html\r\n";
$to = "$to";
// Finally send the activation email to the member
mail($to, $subject, $message, $headers);
// Then print a message to the browser for the joiner
header( 'Location: http://localhost/urshow/registrationsuccess.php' ) ;
// Exit so the form and page does not display, just this success message
} // Close else after database duplicate field value checks
} // Close else after missing vars check
} //Close if $_POST
?>
for the html i have
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Getscene registration</title>
<link href="css/style.css" rel="stylesheet" type="text/css" />
<script src="js/jquery-1.7.1.min.js" type="text/javascript"></script>
</script>
</head>
<body>
<?php include_once "header_template.php"; ?>
<style type="text/css">
#account_types > div { display: none; }
</style>
<div id="signupwrapper">
<div id="signupinner">
<h3 align="left"> GETSCENE REGISTRATION ! </h3>
<hr />
<div id="signup" style="border:thin; border-color:#666">
<h4 align="left">Please Choose One of The Following Account Types</h4>
<div id="accountswrapper">
<form id="accountchoice" name="accountchoice" method="post" action="">
<label for="personalfan">personal/fan</label>
<input type="radio" name="pf" id="personalfan" value="radio1" checked="checked" />
<label for="soloartist">Solo artist</label>
<input type="radio" name="sa" id="soloartist" value="radio2" />
<label for="band">band</label>
<input type="radio" name="ba" id="band" value="radio3" />
<label for="venue">venue</label>
<input type="radio" name="ve" id="venue" value="radio4" />
<label for="business">business</label>
<input type="radio" name="bu" id="business" value="radio5" />
<label for="service">service</label>
<input type="radio" name="se" id="service" value="radio6" />
</form>
<hr />
<div id="account_types">
<div class="personalfan">
<table width="400" border="0" align="center">
<form action="regpersonal.php" method="post" enctype="multipart/form-data">
<tr>
<td colspan="2"><?php echo "$errorMsg"; ?></td>
</tr>
<tr>
<td><div align="right">Username:</div></td>
<td><label for="username"></label>
<input name="username" type="text" id="username" size="30" /></td>
</tr>
<tr>
<td width="146"><div align="right">First Name:</div></td>
<td width="244"><label for="firstname"></label>
<input name="firstname" type="text" id="firstname" size="30" /></td>
</tr>
<tr>
<td><div align="right">Surname:</div></td>
<td><label for="surname"></label>
<input name="surname" type="text" id="surname" size="30" /></td>
</tr>
<tr>
<td><div align="right">Email Address:</div></td>
<td><label for="email"></label>
<input name="email" type="text" id="email" size="30" /></td>
</tr>
<tr>
<td><div align="right">Password:</div></td>
<td><label for="password"></label>
<input name="password" type="password" id="password" size="30" /></td>
</tr>
<tr>
<td><div align="right">Date Of Birth:</div></td>
<td>
<select name="birth_day" class="formFields" id="birth_day">
<option value='01'>01</option>
<option value='02'>02</option>
<option value='03'>03</option>
<option value='04'>04</option>
<option value='05'>05</option>
<option value='06'>06</option>
<option value='07'>07</option>
<option value='08'>08</option>
<option value='09'>09</option>
<option value='10'>10</option>
<option value='11'>11</option>
<option value='12'>12</option>
<option value='13'>13</option>
<option value='14'>14</option>
<option value='15'>15</option>
<option value='16'>16</option>
<option value='17'>17</option>
<option value='18'>18</option>
<option value='19'>19</option>
<option value='20'>20</option>
<option value='21'>21</option>
<option value='22'>22</option>
<option value='23'>23</option>
<option value='24'>24</option>
<option value='25'>25</option>
<option value='26'>26</option>
<option value='27'>27</option>
<option value='28'>28</option>
<option value='29'>29</option>
<option value='30'>30</option>
<option value='31'>31</option>
</select>
<select name="birth_month" class="formFields" id="birth_month">
<option value='01'>January</option>
<option value='02'>February</option>
<option value='03'>March</option>
<option value='04'>April</option>
<option value='05'>May</option>
<option value='06'>June</option>
<option value='07'>July</option>
<option value='08'>August</option>
<option value='09'>September</option>
<option value='10'>October</option>
<option value='11'>November</option>
<option value='12'>December</option>
</select>
<select name="birth_year" class="formFields" id="birth_year">
<option value='2012'>2012</option>
<option value='2011'>2011</option>
<option value='2010'>2010</option>
<option value='2009'>2009</option>
<option value='2008'>2008</option>
<option value='2007'>2007</option>
<option value='2006'>2006</option>
<option value='2005'>2005</option>
<option value='2004'>2004</option>
<option value='2003'>2003</option>
<option value='2002'>2002</option>
<option value='2001'>2001</option>
<option value='2000'>2000</option>
<option value='1999'>1999</option>
<option value='1998'>1998</option>
<option value='1997'>1997</option>
<option value='1996'>1996</option>
<option value='1995'>1995</option>
<option value='1994'>1994</option>
<option value='1993'>1993</option>
<option value='1992'>1992</option>
<option value='1991'>1991</option>
<option value='1990'>1990</option>
<option value='1989'>1989</option>
<option value='1988'>1988</option>
<option value='1987'>1987</option>
<option value='1986'>1986</option>
<option value='1985'>1985</option>
<option value='1984'>1984</option>
<option value='1983'>1983</option>
<option value='1982'>1982</option>
<option value='1981'>1981</option>
<option value='1980'>1980</option>
<option value='1979'>1979</option>
<option value='1978'>1978</option>
<option value='1977'>1977</option>
<option value='1976'>1976</option>
<option value='1975'>1975</option>
<option value='1974'>1974</option>
<option value='1973'>1973</option>
<option value='1972'>1972</option>
<option value='1971'>1971</option>
<option value='1970'>1970</option>
<option value='1969'>1969</option>
<option value='1968'>1968</option>
<option value='1967'>1967</option>
<option value='1966'>1966</option>
<option value='1965'>1965</option>
<option value='1964'>1964</option>
<option value='1963'>1963</option>
<option value='1962'>1962</option>
<option value='1961'>1961</option>
<option value='1960'>1960</option>
<option value='1959'>1959</option>
<option value='1958'>1958</option>
<option value='1957'>1957</option>
<option value='1956'>1956</option>
<option value='1955'>1955</option>
<option value='1954'>1954</option>
<option value='1953'>1953</option>
<option value='1952'>1952</option>
<option value='1951'>1951</option>
<option value='1950'>1950</option>
<option value='1949'>1949</option>
<option value='1948'>1948</option>
<option value='1947'>1947</option>
<option value='1946'>1946</option>
<option value='1945'>1945</option>
<option value='1944'>1944</option>
<option value='1943'>1943</option>
<option value='1942'>1942</option>
<option value='1941'>1941</option>
<option value='1940'>1940</option>
<option value='1939'>1939</option>
<option value='1938'>1938</option>
<option value='1937'>1937</option>
<option value='1936'>1936</option>
<option value='1935'>1935</option>
<option value='1934'>1934</option>
<option value='1933'>1933</option>
<option value='1932'>1932</option>
<option value='1931'>1931</option>
<option value='1930'>1930</option>
<option value='1929'>1929</option>
<option value='1928'>1928</option>
<option value='1927'>1927</option>
<option value='1926'>1926</option>
<option value='1925'>1925</option>
<option value='1924'>1924</option>
<option value='1923'>1923</option>
<option value='1922'>1922</option>
<option value='1921'>1921</option>
<option value='1920'>1920</option>
<option value='1919'>1919</option>
<option value='1918'>1918</option>
<option value='1917'>1917</option>
<option value='1916'>1916</option>
<option value='1915'>1915</option>
<option value='1914'>1914</option>
<option value='1913'>1913</option>
<option value='1912'>1912</option>
<option value='1911'>1911</option>
<option value='1910'>1910</option>
<option value='1909'>1909</option>
<option value='1908'>1908</option>
<option value='1907'>1907</option>
<option value='1906'>1906</option>
<option value='1905'>1905</option>
<option value='1904'>1904</option>
<option value='1903'>1903</option>
<option value='1902'>1902</option>
<option value='1901'>1901</option>
<option value='1900'>1900</option>
</select></td>
</tr>
<tr>
<td> </td>
<td>
<input type="submit" name="submit" id="submit" value="Submit" /></td>
</tr>
</form>
</table></div>
<div class ="soloartist"></div>
<div class="band"></div>
<div class="venue"></div>
<div class="business"></div>
<div class="service"></div>
</div>
</div>
</div>
</div>
</div>
<script type="text/javascript">
$(document).ready(function () {
$('#accountchoice').change(function() {
var divToShow = $(this).find('input:checked').attr('id');
$('#account_types > div').each(function() {
if($(this).hasClass(divToShow)) { $(this).show(); }
else { $(this).hide();}
});
});
$('#accountchoice').trigger('change');
});
</script>
<?php include_once "footer_template.php"; ?>
</body>
</html>
ad in mysql my table has a field called accounttype, which as i said uses an enum format this is the row
accounttype enum('a', 'b', 'c', 'd', 'e', 'f', 'g' one set spare for admin
what would i need for php to get the selected radio button to set account type based on the radios value. i have little to no previous use of radio buttons and have tried a few things but they all fail.
any help here would be a godsend
edited to show all code
1) You have to have one form - you don't have any action associated with the first form - those radio buttons should be within the form that has an action associated (php file). action="" means it's processed by current page - which actually doesn't do any processing.
2) You do keep THE SAME name for all radio buttons.
3) You make that name 'accounttype' as that's what you use to assign a value. (and you don't need any preg_replace there).
4) Run all of your data through mysql_real_escape_string, not just the email.
if you are using any library (jquery) you can do this easily.
(function($){
$('#accountchoice').find('input').change(function(){
if(this.checked){
if($(this).val() == 'radio1'){
// do the needed ajax for radio1 is selected
}
// add conditions for all the radio buttons here, or you can use a swicth case too.
}
});
})(jQuery);
<input type="radio" name="luckynumber" value="1" />
<input type="radio" name="luckynumber" value="2" />
In php
print_r($_POST['luckynumber']);
Am not sure how to ask this question but for most of you more experienced php programmers this is going to sound trivial... So I have successfully set up a register and login system and also an admin section where the administrator can see all the users that are currently registered. What I am wanting is for the admin to be able to edit the user's details, and also have the capability to delete/block them. Actually I have just successfully implemented a delete function.
This code here is of the page where the admin can see the users:
<?php
require('admin_sessions.php');
require("../includes/dbconnect.php");
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Admin - iTrade Users Details</title>
<meta name="robots" content="NOFOLLOW,NOINDEX">
<link rel="stylesheet" href="../stylesheet.css" type="text/css">
<!--[if IE 7]>
<link rel="stylesheet" href="../ie7.css" type="text/css">
<script type="text/javascript">
document.createElement('nav');
document.createElement('article');
document.createElement('aside');
document.createElement('header');
document.createElement('footer');
</script>
<![endif]-->
<!--[if IE 8]>
<link rel="stylesheet" href="../ie8.css" type="text/css">
<script type="text/javascript">
document.createElement('nav');
document.createElement('article');
document.createElement('aside');
document.createElement('header');
document.createElement('footer');
</script>
<![endif]-->
<style type="text/css">
table{
border:1px solid black;
width:2508px;
}
table th{
border-bottom:1px solid black;
border-left:1px solid black;
border-top:1px solid black;
border-right:1px solid black;
}
.header_tables{
background-color:black;
color:white;
text-align:center
}
</style>
</head>
<body>
<header style="width:2570px;"><span class="header_img"><img src="../img/itradeheader.png" width="465" height="200" alt="iTrade - The Leading Online Trading Portal"></span></header>
<nav style="width:2470px;">
<ul>
<li><p>Home</p></li>
<li><p>About iTrade</p></li>
<li><p>Browse Auctions</p></li>
<li><p>F.A.Q's</p></li>
<li><p>Support</p></li>
<?php if($_SESSION['authorisation'] != 'knownuser'){
echo("<li class=\"spacer\"><p class=\"loginlogoutregister\">User Log In</p></li><li><p class=\"loginlogoutregister\">User Register</p></li>");
}
else{
echo("<li class=\"spacer\"><p class=\"loginlogoutregister\">User Logout</p></li>");
$loggedin = "<p>You are logged in.</p>";
}
if($_SESSION['authorisation'] != 'known_admin_user'){
echo("<li><p class=\"loginlogoutregister\">Admin</p></li>");
}
else{
echo("<li><p class=\"loginlogoutregister\">Admin</p></li><li><p class=\"loginlogoutregister\">Admin Logout</p></li>");
}
?>
</ul>
</nav>
<article style="width:2550px; height:100%;">
<h1>Admin - iTrade Users Details</h1>
<p>« Back to main admin page</p>
<ul>
<li>Users on iTrade</li>
<li>Admin Users on iTrade</li>
<li>Register a New Admin User</li>
</ul>
<p>Here is a table containing details about the currently registered users on iTrade:</p>
<fieldset style="width:2480px; height:100%; margin:0 auto;">
<legend style="font-size:30px; color:#000000; background-color:inherit;">A record of users on iTrade</legend>
<table>
<tr class="header_tables">
<th><u>User I.D</u></th>
<th><u>User Level</u></th>
<th><u>User-name</u></th>
<th><u>First Name</u></th>
<th><u>Last Name</u></th>
<th><u>Email</u></th>
<th><u>Password</u></th>
<th><u>Gender</u></th>
<th><u>Birthday</u></th>
<th><u>Landline Number</u></th>
<th><u>Mobile Number</u></th>
<th><u>Street Address</u></th>
<th><u>Suburb</u></th>
<th><u>City/Town</u></th>
<th><u>Province</u></th>
<th><u>Postcode</u></th>
<th><u>Closest Town</u></th>
<th><u>Confirmed?</u></th>
<th><u>Date Registered</u></th>
</tr>
<?php
$query = "SELECT * FROM user_list;";
$result = mysql_query($query, $connection) or die(mysql_error());
$users_on_itrade = " ";
while($row = mysql_fetch_array($result)){
echo("<tr>");
echo('<td>' . $row['user_id'] . '</td>');
echo('<td>' . $row['user_level'] . '</td>');
echo('<td>' . $row['user_username'] . '</td>');
echo('<td>' . $row['firstname'] . '</td>');
echo('<td>' . $row['lastname'] . '</td>');
echo('<td>' . $row['user_email'] . '</td>');
echo('<td>' . $row['user_pass'] . '</td>');
echo('<td>' . $row['gender']. '</td>');
echo('<td>' . $row['birthdate_day'] . $row['birthdate_month'] . $row['birthdate_year'] . '</td>');
echo('<td>' . $row['phoneNumber'] . '</td>');
echo('<td>' . $row['mobileNumber'] . '</td>');
echo('<td>' . $row['addressline1'] . '</td>');
echo('<td>' . $row['addressline2'] . '</td>');
echo('<td>' . $row['addressline3'] . '</td>');
echo('<td>' . $row['stateprovince'] . '</td>');
echo('<td>' . $row['postcode'] . '</td>');
echo('<td>' . $row['closest_town'] . '</td>');
echo('<td>' . $row['confirmed'] . '</td>');
echo('<td>' . $row['user_date'] . '</td>');
echo('<td>Edit</td>');
echo('<td>Delete</td>');
echo("<tr>");
}
?>
</table>
</fieldset>
</article>
<footer style="width:2550px;"><p>© Copyright 2011 iTrade LTD. Website design and development by InterKiwi Web Developers. Special thanks to the Computer Science department at the University of Otago, and the lecturers and lab administrators/demonstrators of the universities' Advanced Web Development paper.</p></footer>
</body>
</html>
This code is for the "edit" page link in "<td>Edit</td>":
<?php
require('admin_sessions.php');
require("../includes/dbconnect.php");
require('../includes/itradeuser.php');
?>
<?php
/*
This php script allows the administrator to edit a specific itrade user in the database.
*/
// creates the edit record form
// since this form is used multiple times in this file, I have made it a function that is easily reusable
function renderForm($user_id, $user_email, $user_pass, $user_pass_confirm, $user_username, $firstname, $lastname, $gender, $birthdate_day, $birthdate_month, $birthdate_year, $phoneNumber, $mobileNumber, $addressline1, $addressline2, $addressline3, $stateprovince, $postcode, $closest_town, $error)
{
?>
<!DOCTYPE HTML>
<html>
<head>
<title>Edit Record</title>
</head>
<body>
<?php
// if there are any errors, display them
if ($error != '')
{
echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
}
?>
<form action="" method="post">
<input type="hidden" name="id" value="<?php echo $user_id; ?>"/>
<fieldset class="userdetails">
<legend>User Credentials</legend>
<p><strong>ID:</strong> <?php echo $user_id; ?></p>
<ul>
<li><label for="user_email">Email Address:</label><input name="user_email" maxlength="50" id="user_email" size="40" type="text" value="<?php echo $user_email; ?>"><br></li>
<li><label for="user_pass">Your Password:</label><input name="user_pass" maxlength="50" id="user_pass" size="30" type="password" value="<?php echo $user_pass; ?>"><br></li>
<li><label for="user_pass_confirm">Password Again:</label><input name="user_pass_confirm" maxlength="50" id="user_pass_confirm" size="30" type="password" value="<?php echo $user_pass_confirm; ?>"><br></li>
<li><label for="user_username">Your Username:</label><input name="user_username" maxlength="50" id="user_username" size="30" type="text" value="<?php echo $user_username; ?>"></li>
</ul>
</fieldset>
<fieldset>
<legend>Contact Details</legend>
<ul>
<li><label for="firstname">First Name:</label><input name="firstname" type="text" id="firstname" size="25" maxlength="200" value="<?php echo $firstname; ?>" /><br></li>
<li><label for="lastname">Last Name:</label><input name="lastname" type="text" id="lastname" size="25" maxlength="200" value="<?php echo $lastname; ?>" /><br></li>
<li><label for="gender">Gender:</label>
<select name="gender" id="gender">
<option selected="" value=""> </option>
<option value="male">Male</option>
<option value="female">Female</option>
<option value="unspecified">Unspecified</option>
</select><br></li>
<li><label for"birthdate_day">Date of birth:</label>
<select name="birthdate_day" id="birthdate_day">
<option selected="selected" value=""></option>
<option value="1st">1</option>
<option value="2nd">2</option>
<option value="3rd">3</option>
<option value="4th">4</option>
<option value="5th">5</option>
<option value="6th">6</option>
<option value="7th">7</option>
<option value="8th">8</option>
<option value="9th">9</option>
<option value="10th">10</option>
<option value="11th">11</option>
<option value="12th">12</option>
<option value="13th">13</option>
<option value="14th">14</option>
<option value="15th">15</option>
<option value="16th">16</option>
<option value="17th">17</option>
<option value="18th">18</option>
<option value="19th">19</option>
<option value="20th">20</option>
<option value="21st">21</option>
<option value="22nd">22</option>
<option value="23rd">23</option>
<option value="24th">24</option>
<option value="25th">25</option>
<option value="26th">26</option>
<option value="27th">27</option>
<option value="28th">28</option>
<option value="29th">29</option>
<option value="30th">30</option>
<option value="31st">31</option>
</select>
<select name="birthdate_month" id="birthdate_month">
<option selected="selected" value=""></option>
<option value="January">January</option>
<option value="February">February</option>
<option value="March">March</option>
<option value="May">May</option>
<option value="June">June</option>
<option value="July">July</option>
<option value="August">August</option>
<option value="September">September</option>
<option value="October">October</option>
<option value="November">November</option>
<option value="December">December</option>
</select><br></li>
<li><label for="birthdate_year">Birth Year:</label><input name="birthdate_year" type="number" maxlength="4" id="birthdate_year" size="4" value="<?php echo $birthdate_year; ?>"><br>
</li>
<li>
<label for="phoneNumber">Home Phone:</label>
<input name="phoneNumber" type="number" maxlength="9" id="phoneNumber" size="9" value="<?php echo $phoneNumber; ?>"><br></li>
<li><label for="mobileNumber">Mobile Number:</label>
<input name="mobileNumber" type="number" maxlength="11" id="mobileNumber" size="11" value="<?php echo $mobileNumber; ?>"><span class="informational">e.g: 0276123456</span><br></li>
<li><label for="addressline1">Street Address:</label>
<input name="addressline1" maxlength="100" id="addressline1" size="40" type="text" value="<?php echo $addressline1; ?>"><br></li>
<li><label for="addressline2">Suburb:</label>
<input name="addressline2" maxlength="100" id="addressline2" size="40" type="text" value="<?php echo $addressline2; ?>"><br></li>
<li><label for="addressline3">City:</label>
<input name="addressline3" maxlength="100" id="addressline3" size="40" type="text" value="<?php echo $addressline3; ?>"><br></li>
<li><label for="stateprovince">State/Province:</label>
<select name="stateprovince" id="stateprovince">
<option selected="selected" value="<?php echo $stateprovince; ?>"></option>
<option value="northland">Northland</option>
<option value="auckland">Auckland</option>
<option value="waikato">Waikato</option>
<option value="bayofplenty">Bay of Plenty</option>
<option value="eastcoast">East Coast</option>
<option value="hawkesbay">Hawkes Bay</option>
<option value="taranaki">Taranaki</option>
<option value="kingcountry">King Country</option>
<option value="wanganui">Wanganui</option>
<option value="manawatu">Manawatu</option>
<option value="wairarapa">Wairarapa</option>
<option value="wellington">Wellington</option>
<option value="nelsonbays">Nelson Bays</option>
<option value="marlborough">Marlborough</option>
<option value="buller">Buller</option>
<option value="westland">Westland</option>
<option value="northcanterbury">North Canterbury</option>
<option value="canterbury">Canterbury</option>
<option value="midcanterbury">Mid Canterbury</option>
<option value="southcanterbury">South Canterbury</option>
<option value="northotago">North Otago</option>
<option value="otago">Otago</option>
<option value="southland">Southland</option>
<option value="chathamisl">Chatham Islands</option>
</select><br></li>
<li><label for="postcode">Post Code:</label>
<input name="postcode" type="number" maxlength="4" id="postcode" size="4" value="<?php echo $postcode; ?>"><span class="informational">Find your post code</span><br></li>
<li><label for="closest_town">Closest Town:</label>
<select name="closest_town" id="closest_town">
<option selected="selected" value=""></option>
<option value="dargaville">Northland - Dargaville</option>
<option value="kaikohe">Northland - Kaikohe</option>
<option value="kaitaia">Northland - Kaitaia</option>
<option value="kawakawa">Northland - Kawakawa</option>
<option value="kerikeri">Northland - Kerikeri</option>
<option value="maungaturoto">Northland - Maungaturoto</option>
<option value="paihia">Northland - Paihia</option>
<option value="whangarei">Northland - Whangarei</option>
<option value="0"> </option>
<option value="aucklandcity">Auckland - Auckland City</option>
<option value="franklin">Auckland - Franklin</option>
<option value="greatbarrierisland">Auckland - Great Barrier Island</option>
<option value="helensville">Auckland - Helensville</option>
<option value="hibiscuscoast">Auckland - Hibiscus Coast</option>
<option value="manukaucity">Auckland - Manukau City</option>
<option value="northshore">Auckland - North Shore</option>
<option value="papakuracity">Auckland - Papakura City</option>
<option value="waihekeisl">Auckland - Waiheke Island</option>
<option value="waitakerecity">Auckland - Waitakere City</option>
<option value="warkworth">Auckland - Warkworth</option>
<option value="wellsford">Auckland - Wellsford</option>
<option value="0"> </option>
<option value="cambridge">Waikato - Cambridge</option>
<option value="coromandel">Waikato - Coromandel</option>
<option value="hamilton">Waikato - Hamilton</option>
<option value="huntly">Waikato - Huntly</option>
<option value="matamata">Waikato - Matamata</option>
<option value="morrinsville">Waikato - Morrinsville</option>
<option value="otorohanga">Waikato - Otorohanga</option>
<option value="paeroa">Waikato - Paeroa</option>
<option value="raglan">Waikato - Raglan</option>
<option value="taumarunui">Waikato - Taumarunui</option>
<option value="teawamutu">Waikato - Te Awamutu</option>
<option value="tekuiti">Waikato - Te Kuiti</option>
<option value="thames">Waikato - Thames</option>
<option value="tokoroa_putaruru">Waikato - Tokoroa/Putaruru</option>
<option value="waihi">Waikato - Waihi</option>
<option value="waihi_beach">Waikato - Waihi Beach</option>
<option value="whangamata">Waikato - Whangamata</option>
<option value="0"> </option>
<option value="katikati">Bay of Plenty - Katikati</option>
<option value="mt_maunganui">Bay of Plenty - Mt. Maunganui</option>
<option value="opotiki">Bay of Plenty - Opotiki</option>
<option value="rotorua">Bay of Plenty - Rotorua</option>
<option value="taupo">Bay of Plenty - Taupo</option>
<option value="tauranga">Bay of Plenty - Tauranga</option>
<option value="te_puke">Bay of Plenty - Te Puke</option>
<option value="turangi">Bay of Plenty - Turangi</option>
<option value="whakatane">Bay of Plenty - Whakatane</option>
<option value="0"> </option>
<option value="gisborne">Gisborne - Gisborne</option>
<option value="ruatoria">Gisborne - Ruatoria</option>
<option value="0"> </option>
<option value="dannevirke">Hawkes Bay - Dannevirke</option>
<option value="hastings">Hawkes Bay - Hastings</option>
<option value="napier">Hawkes Bay - Napier</option>
<option value="waipukurau">Hawkes Bay - Waipukurau</option>
<option value="wairoa">Hawkes Bay - Wairoa</option>
<option value="0"> </option>
<option value="hawera">Taranaki - Hawera</option>
<option value="mokau">Taranaki - Mokau</option>
<option value="new_plymouth">Taranaki - New Plymouth</option>
<option value="opunake">Taranaki - Opunake</option>
<option value="stratford">Taranaki - Stratford</option>
<option value="0"> </option>
<option value="ohakune">Wanganui - Ohakune</option>
<option value="taihape">Wanganui - Taihape</option>
<option value="waiouru">Wanganui - Waiouru</option>
<option value="wanganui">Wanganui - Wanganui</option>
<option value="0"> </option>
<option value="bulls">Manawatu - Bulls</option>
<option value="fielding">Manawatu - Feilding</option>
<option value="levin">Manawatu - Levin</option>
<option value="manawatu">Manawatu - Manawatu</option>
<option value="marton">Manawatu - Marton</option>
<option value="palmerston_north">Manawatu - Palmerston North</option>
<option value="0"> </option>
<option value="carterton">Wairarapa - Carterton</option>
<option value="featherston">Wairarapa - Featherston</option>
<option value="greytown">Wairarapa - Greytown</option>
<option value="martinborough">Wairarapa - Martinborough</option>
<option value="masterton">Wairarapa - Masterton</option>
<option value="paihiatua">Wairarapa - Pahiatua</option>
<option value="woodville">Wairarapa - Woodville</option>
<option value="0"> </option>
<option value="kapiti">Wellington - Kapiti</option>
<option value="lower_hutt">Wellington - Lower Hutt City</option>
<option value="porirua">Wellington - Porirua</option>
<option value="upper_hutt">Wellington - Upper Hutt City</option>
<option value="wellington">Wellington - Wellington City</option>
<option value="0"> </option>
<option value="golden_bay">Nelson Bays - Golden Bay</option>
<option value="motueka">Nelson Bays - Motueka</option>
<option value="murchison">Nelson Bays - Murchison</option>
<option value="nelson">Nelson Bays - Nelson</option>
<option value="picton">Nelson Bays - Picton</option>
<option value="0"> </option>
<option value="blenheim">Marlborough - Blenheim</option>
<option value="marlborough_sounds">Marlborough - Marlborough Sounds</option>
<option value="0"> </option>
<option value="greymouth">West Coast - Greymouth</option>
<option value="hokitika">West Coast - Hokitika</option>
<option value="westport">West Coast - Westport</option>
<option value="0"> </option>
<option value="akaroa">Canterbury - Akaroa</option>
<option value="amberley">Canterbury - Amberley</option>
<option value="ashburton">Canterbury - Ashburton</option>
<option value="cheviot">Canterbury - Cheviot</option>
<option value="christchurch">Canterbury - Christchurch City</option>
<option value="darfield">Canterbury - Darfield</option>
<option value="fairlie">Canterbury - Fairlie</option>
<option value="geraldine">Canterbury - Geraldine</option>
<option value="hamnersprings">Canterbury - Hanmer Springs</option>
<option value="kaiapoi">Canterbury - Kaiapoi</option>
<option value="kaikoura">Canterbury - Kaikoura</option>
<option value="mtcook">Canterbury - Mt Cook</option>
<option value="rangiora">Canterbury - Rangiora</option>
<option value="0"> </option>
<option value="timaru_oamaru_kurow">Timaru - Oamaru - Kurow</option>
<option value="timaru_oamaru_oamaru">Timaru - Oamaru - Oamaru</option>
<option value="timaru_oamaru_timaru">Timaru - Oamaru - Timaru</option>
<option value="timaru_oamaru_twizel">Timaru - Oamaru - Twizel</option>
<option value="timaru_oamaru_waimate">Timaru - Oamaru - Waimate</option>
<option value="0"> </option>
<option value="alexandra">Otago - Alexandra</option>
<option value="balclutha">Otago - Balclutha</option>
<option value="cromwell">Otago - Cromwell</option>
<option value="dunedin">Otago - Dunedin</option>
<option value="lawrence">Otago - Lawrence</option>
<option value="milton">Otago - Milton</option>
<option value="palmerston">Otago - Palmerston</option>
<option value="queenstown">Otago - Queenstown</option>
<option value="ranfurly">Otago - Ranfurly</option>
<option value="roxburgh">Otago - Roxburgh</option>
<option value="wanaka">Otago - Wanaka</option>
<option value="0"> </option>
<option value="bluff">Southland - Bluff</option>
<option value="edendale">Southland - Edendale</option>
<option value="gore">Southland - Gore</option>
<option value="invercargill">Southland - Invercargill</option>
<option value="lumsden">Southland - Lumsden</option>
<option value="otautau">Southland - Otautau</option>
<option value="riverton">Southland - Riverton</option>
<option value="stewart_island">Southland - Stewart Island</option>
<option value="te_anau">Southland - Te Anau</option>
<option value="tokanui">Southland - Tokanui</option>
<option value="winton">Southland - Winton</option>
<option value="0"> </option>
<option value="chathamisl">Chatham Islands</option>
</select><br></li>
</ul>
</fieldset>
<input type="submit" name="submit" value="Submit">
</div>
</form>
</body>
</html>
<?php
}
// check if the form has been submitted. If it has, process the form and save it to the database
if (isset($_POST['submit'])){
if (is_numeric($_POST['user_id'])){
// get form data, making sure it is valid
$user_id = $_POST['user_id'];
$user_email = mysql_real_escape_string($_POST['user_email']);
$user_pass = mysql_real_escape_string($_POST['user_pass']);
$user_pass_confirm = mysql_real_escape_string($_POST['user_pass_confirm']);
$user_username = mysql_real_escape_string($_POST['user_username']);
$firstname = mysql_real_escape_string($_POST['firstname']);
$lastname = mysql_real_escape_string($_POST['lastname']);
$gender = mysql_real_escape_string($_POST['gender']);//optional field
$birthdate_day = mysql_real_escape_string($_POST['birthdate_day']);
$birthdate_month = mysql_real_escape_string($_POST['birthdate_month']);
$birthdate_year = mysql_real_escape_string($_POST['birthdate_year']);
$phoneNumber = mysql_real_escape_string($_POST['phoneNumber']);
$mobileNumber = mysql_real_escape_string($_POST['mobileNumber']);
$addressline1 = mysql_real_escape_string($_POST['addressline1']);
$addressline2 = mysql_real_escape_string($_POST['addressline2']); // optional field
$addressline3 = mysql_real_escape_string($_POST['addressline3']);
$stateprovince = mysql_real_escape_string($_POST['stateprovince']);
$postcode = mysql_real_escape_string($_POST['postcode']); // optional field
$closest_town = mysql_real_escape_string($_POST['closest_town']); //optional field
$date = time();
// check that firstname/lastname fields are both filled in
if ($firstname == '' || $lastname == '' || $user_email =='' || $user_pass == '' || $user_pass_confirm == '' || $user_username == '' || $firstname =='' || $lastname == '' || $gender == '' || $birthdate_day == '' || $birthdate_month == '' || $birthdate_year == '' || $phoneNumber == '' || $mobileNumber == '' || $addressline1 == '' || $addressline2 == '' || $addressline3 == '' || $stateprovince == '' || $postcode == '' || $closest_town == ''){
// generate error message
$error = 'ERROR: Please fill in all required fields!';
//error, display form
renderForm($user_id, $user_email, $user_pass, $user_pass_confirm, $user_username, $firstname, $lastname, $gender, $birthdate_day, $birthdate_month, $birthdate_year, $phoneNumber, $mobileNumber, $addressline1, $addressline2, $addressline3, $stateprovince, $postcode, $closest_town, $error);
}
else{
// save the data to the database
mysql_query("UPDATE user_list SET user_email='$user_email', user_pass=sha1('$user_pass.$salt'), user_pass_confirm=sha1('$user_pass_confirm.$salt'), user_username='$user_username', firstname='$firstname', lastname='$lastname', gender='$gender', birthdate_day='$birthdate_day, birthdate_month='$birthdate_month', birthdate_year='$birthdate_year', phoneNumber='$phoneNumber', mobileNumber='$mobileNumber', addressline1='$addressline1', addressline2='$addressline2', addressline3='$addressline3', stateprovince='$stateprovince', postcode='$postcode', closest_town='$closest_town' WHERE user_id='$user_id'")
or die(mysql_error());
// once saved, redirect back to the view page
header("Location: itradeusers.php");
}
}
else
{
// if the 'id' isn't valid, display an error
echo 'Error! ID not valid.';
}
}
else
// if the form hasn't been submitted, get the data from the db and display the form
{
// get the 'id' value from the URL (if it exists), making sure that it is valid (checing that it is numeric/larger than 0)
if (isset($_GET['user_id']) && is_numeric($_GET['user_id']) && $_GET['user_id'] > 0)
{
// query db
$user_id = $_GET['user_id'];
$result = mysql_query("SELECT * FROM user_list WHERE user_id=$user_id")
or die(mysql_error());
$row = mysql_fetch_array($result);
// check that the 'id' matches up with a row in the databse
if($row)
{
// get data from db
$user_id = $row['user_id'];
$user_email = $row['user_email'];
$user_pass = $row['user_pass'];
$user_pass_confirm = $row['user_pass_confirm'];
$user_username = $row['user_username'];
$firstname = $row['firstname'];
$lastname = $row['lastname'];
$gender = $row['gender'];
$birthdate_day = $row['birthdate_day'];
$birthdate_month = $row['birthdate_month'];
$birthdate_year = $row['birthdate_year'];
$phoneNumber = $row['phoneNumber'];
$mobileNumber = $row['mobileNumber'];
$addressline1 = $row['addressline1'];
$addressline2 = $row['addressline2'];
$addressline3 = $row['addressline3'];
$stateprovince = $row['stateprovince'];
$postcode = $row['postcode'];
$closest_town = $row['closest_town'];
// show form
renderForm($user_id, $user_email, $user_pass, $user_pass_confirm, $user_username, $firstname, $lastname, $gender, $birthdate_day, $birthdate_month, $birthdate_year, $phoneNumber, $mobileNumber, $addressline1, $addressline2, $addressline3, $stateprovince, $postcode, $closest_town, $error);
}else
// if no match, display result
{
echo "No results!";
}
}
else
// if the 'id' in the URL isn't valid, or if there is no 'id' value, display an error
{
echo 'Error! the \'id\' is not valid.';
}
}
?>
If someone could tell me what I have done wrong, or even explain to me a better way of doing something like this, please let me know... Thanks in advance!
First of all, it looks like you're storing passwords in plain text. That is a big no no!
For updating user info have a look at the UPDATE command for mysql: http://dev.mysql.com/doc/refman/5.0/en/update.html
For the delete/block issue, you could add another column that stores the user status (deleted, blocked, active, etc). You could then check the status of this column when the user logs in or visits a page.