[RESOLVED] Form data and variables
Hi,
For some reason the data isn't being accepted by the variables in my php.
HTML Code:
<form action="<?php $_SERVER['PHP_SELF'];?>" method="post" enctype="application/x-www-form-urlencoded" name="Register" target="_self">
<label class="left-label" for="cid">Customer ID:</label><label class="center-label" for="fname">First Name:</label><label class="right-label" for="lname">Last Name:</label><br /><input name="cid" id="cid" type="text" /> <input name="fname" id="fname" type="text" /> <input name="lname" id="lname" type="text" /> <br /><label class= "row2left-label" for="snum">Street Number:</label><label class= "row2center-label" for="sname">Street Name:</label><label class= "row2right-label" for="suburb">Suburb:</label><br /><input name="sum" id="snum" type="text" /> <input name="sname" id="sname" type="text" /> <input name="suburb" id="suburb" type="text" /> <br /><label class= "row3left-label" for="pcode">Post Code:</label><label class= "row3center-label" for="country">Country:</label><label class= "row3right-label" for="phone">Phone:</label><br /> <input name="pcode" id="pcode" type="text" /> <input name="country" id="counry" type="text" /> <input name="phone" id="phone" type="text" /><br /> <label class= "row4left-label" for="email">Email:</label><label class= "row4center-label" for="user">Username:</label><label class= "row4right-label" for="upassword">Password:</label><br /><input name="email" id="email" type="text" /> <input name="user" id="user" type="text" /> <input name="upassword" id="upassword" type="text" /><br />
<input name="submit" class="form-button" type="button" value="Submit" /> <input name="reset" type="button" value="Reset" />
</form>
<?php
$cid ="0";
$fname = "0";
$lname = "0";
$snum = "0";
$sname = "0";
$suburb = "0";
$pcode = "0";
$country = "0";
$phone = "0";
$email = "0";
$user = "0";
$password = "0";
?>
I can manually change the variable data and when I run the script that data will put in the database. However, if I set variable = "" or variable = 0 nothing gets put in the database. I have narrowed the problem down to the variables or the form but don't know which is the cause. I have reason to suspect the form though because neither of the buttons are working.
Any help will be appreciated!
Thanks,
Nightwalker
Re: Form data and variables
well, your "submit" button is a "button" rather than a submit button, which is why it wouldn't be doing anything unless you attached some javascript to it. use a "submit" button:
HTML Code:
<input type="submit" valie="Submit" />
a "reset" button won't do anything if it is a "button" either; you need to change the type to "reset"! a form "button" is just that -- a button. it won't do anything unless you tell it to with javascript, or something. in this case, there is absolutely no reason to have javascript, though.
anyway -- if that doesn't help, the code you posted has absolutely nothing to do with any databases, and actually doesn't even contain any sort of logic whatsoever. it's just a form, and a bunch of variable declarations. so, you'll need to post something more if you need anymore help.
also, it's incredibly difficult to read what you call your "form" when you format it the way you are. actually, it's absolutely impossible to read. try putting some line breaks in there somewhere if you expect anyone to decipher it! :)
Re: Form data and variables
Quote:
Originally Posted by
kows
well, your "submit" button is a "button" rather than a submit button, which is why it wouldn't be doing anything unless you attached some javascript to it. use a "submit" button:
HTML Code:
<input type="submit" valie="Submit" />
a "reset" button won't do anything if it is a "button" either; you need to change the type to "reset"! a form "button" is just that -- a button. it won't do
anything unless you tell it to with javascript, or something. in this case, there is absolutely no reason to have javascript, though.
Yeah, I noticed that after I posted!
The code I was trying to use was:
Quote:
Originally Posted by
Nightwalker83
Here is the modified script from post #43:
PHP Code:
<?php
// Database connection variables
$dbDatabase = "BazaarCeramics";
//connect to server or exit
$conn = @mysql_connect("localhost", "root", "");
if (!$conn) {
die("Connection failed: " .mysql_error());
}
//create database
$query = "CREATE DATABASE IF NOT EXISTS BazaarCeramics";
if (mysql_query($query, $conn)) {
echo ("Database create query successful");
}
//select database
if (mysql_select_db($dbDatabase, $conn)) {
echo ("Database selection successful");
}else {
die ("Could not locate BazaarCeramics database" .mysql_error());
}
//create tables
$query = "CREATE TABLE IF NOT EXISTS customers (
cid int(2) NOT NULL auto_increment,
FName varchar(30) default NULL,
LName varchar(30) default NULL,
Email varchar(50) default NULL,
Streetname varchar(20) default NULL,
Housenum char(3) default NULL,
Suburb varchar(20) default NULL,
Postcode varchar(6) default NULL,
Country varchar(20) default NULL,
Phone varchar(10) default NULL,
Username varchar(10) default NULL,
Password varchar(8) default NULL,
PRIMARY KEY (cid)
) TYPE=MyISAM";
if (mysql_query($query, $conn)) {
echo ("table users query successful");
}else {
//connect to database or exit
if (!(mysql_select_db($dbDatabase, $conn))){
echo '&message=db+selection+failed&';
exit;
}
}
//convert the POST variables from flash to local variables
$cid = mysql_real_escape_string($_POST['cid']);
$fname = mysql_real_escape_string($_POST['fname']);
$lname = mysql_real_escape_string($_POST['lname']);
$snum = mysql_real_escape_string($_POST['snum']);
$sname = mysql_real_escape_string($_POST['sname']);
$suburb = mysql_real_escape_string($_POST['suburb']);
$pcode = mysql_real_escape_string($_POST['pcode']);
$country = mysql_real_escape_string($_POST['country']);
$phone = mysql_real_escape_string($_POST['phone']);
$email = mysql_real_escape_string($_POST['email']);
$user = mysql_real_escape_string($_POST['user']);
$password = mysql_real_escape_string($_POST['upassword']);
// Make sure the data has been sent to the script from flash
if($cid==""){
echo '&message=you+must+enter+customer+record&';
exit;
}
//send mysql a query to select records from the products table where the id's match. If the query fails exit
if (!($result = mysql_query("SELECT * FROM customers where cid= '$cid'"))){
echo '&message=query+failed&';
exit;
}
//Retrieve the number of rows (records)that have been returned from above query
$num_results = mysql_num_rows($result);
if($num_results <= 0) {//customer does not exist so insert
$query = "INSERT INTO customers (cid, FName, LName, Housenum, Streetname, Suburb, Postcode, Country, Phone, Email, Username, Password)
VALUES ('$cid','$fname', '$lname', '$snum', '$sname','$suburb','$pcode','$country','$phone','$email','$user','$password')";
if (mysql_query($query , $conn))
echo "&message=the+customer+'$FName'+'$LName'+has+been+successfully+added&";
else
echo '&message=the+insert+was+not+successful&';
}else { //customer exists so update existing customer
$update = "update customers set FName='$fname'; LName='$lname'; where customerid='$cid'";
if(mysql_query($update, $conn))
echo "&message=the+details+have+been+updated&";
else
echo "&message=update+not+successful&";
}
?>
and this was how I made it work:
HTML Code:
<form action="<?php $_SERVER['PHP_SELF'];?>" method="post" enctype="application/x-www-form-urlencoded" name="Register" target="_self">
<label class="left-label" for="cid">Customer ID:</label><label class="center-label" for="fname">First Name:</label><label class="right-label" for="lname">Last Name:</label><br /><input name="cid" id="cid" type="text" /> <input name="fname" id="fname" type="text" /> <input name="lname" id="lname" type="text" /> <br /><label class= "row2left-label" for="snum">Street Number:</label><label class= "row2center-label" for="sname">Street Name:</label><label class= "row2right-label" for="suburb">Suburb:</label><br /><input name="snum" id="snum" type="text" /> <input name="sname" id="sname" type="text" /> <input name="suburb" id="suburb" type="text" /> <br /><label class= "row3left-label" for="pcode">Post Code:</label><label class= "row3center-label" for="country">Country:</label><label class= "row3right-label" for="phone">Phone:</label><br /> <input name="pcode" id="pcode" type="text" /> <input name="country" id="counry" type="text" /> <input name="phone" id="phone" type="text" /><br /> <label class= "row4left-label" for="email">Email:</label><label class= "row4center-label" for="user">Username:</label><label class= "row4right-label" for="upassword">Password:</label><br /><input name="email" id="email" type="text" /> <input name="user" id="user" type="text" /> <input name="upassword" id="upassword" type="text" /><br />
<input name="submit" class="form-button" type="submit" value="Submit" /> <input name="reset" type="reset" value="Reset" />
</form>
PHP Code:
<?php
$cid="0";
$fname="0";
$lname="0";
$snun="0";
$sname="0";
$suburb="0";
$pcode="0";
$country="0";
$phone="0";
$email="0";
$user ="0";
$password="0";
// Database connection variables
$dbDatabase = "BazaarCeramics";
//connect to server or exit
$conn = @mysql_connect("localhost", "user", "");
if (!$conn) {
die("Connection failed: " .mysql_error());
}
//create database
$query = "CREATE DATABASE IF NOT EXISTS BazaarCeramics";
if (mysql_query($query, $conn)) {
echo ("Database create query successful");
}
//select database
if (mysql_select_db($dbDatabase, $conn)) {
echo ("Database selection successful");
}else {
die ("Could not locate BazaarCeramics database" .mysql_error());
}
//create tables
$query = "CREATE TABLE IF NOT EXISTS customers (
cid int(2) NOT NULL auto_increment,
FName varchar(30) default NULL,
LName varchar(30) default NULL,
Email varchar(50) default NULL,
Streetname varchar(20) default NULL,
Housenum char(3) default NULL,
Suburb varchar(20) default NULL,
Postcode varchar(6) default NULL,
Country varchar(20) default NULL,
Phone varchar(10) default NULL,
Username varchar(10) default NULL,
Password varchar(8) default NULL,
PRIMARY KEY (cid)
) TYPE=MyISAM";
if (mysql_query($query, $conn)) {
//echo ("table users query successful");
}else {
//connect to database or exit
if (!(mysql_select_db($dbDatabase, $conn))){
//echo '&message=db+selection+failed&';
exit;
}
}
//validate or send data.
//convert the POST variables from the html to local variables
if(isset($_POST['cid'], $_POST['fname'], $_POST['lname'], $_POST['snum'], $_POST['sname'], $_POST['suburb'], $_POST['pcode'], $_POST['country'], $_POST['phone'], $_POST['email'], $_POST['user'],$_POST['upassword'])){
$cid = mysql_real_escape_string($_POST['cid']);
$fname = mysql_real_escape_string($_POST['fname']);
$lname = mysql_real_escape_string($_POST['lname']);
$snum = mysql_real_escape_string($_POST['snum']);
$sname = mysql_real_escape_string($_POST['sname']);
$suburb = mysql_real_escape_string($_POST['suburb']);
$pcode = mysql_real_escape_string($_POST['pcode']);
$country = mysql_real_escape_string($_POST['country']);
$phone = mysql_real_escape_string($_POST['phone']);
$email = mysql_real_escape_string($_POST['email']);
$user = mysql_real_escape_string($_POST['user']);
$password = mysql_real_escape_string($_POST['upassword']);
}
if($_SERVER['REQUEST_METHOD'] == "POST"){
// Make sure the data has been sent to the script from flash
if($cid==""){
echo '&message=you+must+enter+customer+record&';
exit;
}
//send mysql a query to select records from the products table where the id's match. If the query fails exit
if (!($result = mysql_query("SELECT * FROM customers where cid= '$cid'"))){
echo '&message=query+failed&';
exit;
}
//Retrieve the number of rows (records)that have been returned from above query
$num_results = mysql_num_rows($result);
if($num_results <= 0) {//customer does not exist so insert
$query = "INSERT INTO customers (cid, FName, LName, Housenum, Streetname, Suburb, Postcode, Country, Phone, Email, Username, Password)
VALUES ('$cid','$fname', '$lname', '$snum', '$sname','$suburb','$pcode','$country','$phone','$email','$user','$password')";
if (mysql_query($query , $conn)){
$update = "update customers set FName='$fname'; LName='$lname'; where customerid='$cid'";
echo "&message=the+customer+'$fname'+'$lname'+has+been+successfully+added&";
}else{
echo '&message=the+insert+was+not+successful&';
}
if(!mysql_query($update, $conn)){
echo "&message=the+details+have+been+updated&";
}else{
echo "&message=update+not+successful&";
}
}
}
?>