SQL addage

Associate
Joined
19 Oct 2005
Posts
528
G,day

Im adding some data to a mysql table which has a auto increment field. When i add the data using the php the table increments by one, but no other data is inserted from the PHP page. The php page drags data from a form on a html page. username,forename, password and email are the name of the fields in the html page.
thankyou muchly for any help

Code:
<?php
include("db_connect.php");
// Connect to the database.
//If the form was submitted, process it.
//Pick up the values from the previous form
$username =$_REQUEST['CustName'];
$forename =$_REQUEST['CustAddress'];
$password =$_REQUEST['CustTelNo'];
$email =$_REQUEST['CustE-mail'];

$query = "insert into Customer values
  ('$CustNumber','$CustName', '$CustAddress', '$CustTelNo', '$CustE-mail')";
  if (@mysql_query ($query))
    {
	echo 'The Customer has been added.';
	}
  else
    {
	echo 'The Customer could not be added because - ' . mysql_error();
	}
?>
 
('$CustNumber','$CustName', '$CustAddress', '$CustTelNo', '$CustE-mail')

Where's the auto-increment field?

Leave it blank, ie:

('','$CustN....

:)
 
Specify the values you're adding. That way you don't have to pass a blank value for an auto-increment field, and don't break your script if you ever add a field without updating the script.

Code:
INSERT INTO sometable
(some_field, some_other_field)
VALUES("foo", "bar")

It also has the added bonus of not forcing you to put the fields in the right order, which is always nice—saves having to go and look at the schema every time you write an INSERT statement.
 
hmmmmmmmmmmmmmm

If i specify the value will it still take the value from the html form? I added the blank space for the auto-increment but the database is still blank.

Code:
<?php
include("db_connect.php");
// Connect to the database.
//If the form was submitted, process it.
//Pick up the values from the previous form
$username =$_REQUEST['CustName'];
$forename =$_REQUEST['CustAddress'];
$password =$_REQUEST['CustTelNo'];
$email =$_REQUEST['CustE-mail'];

$query = "insert into Customer values
  ('','$CustName', '$CustAddress', '$CustTelNo', '$CustE-mail')";
  if (@mysql_query ($query))
    {
	echo 'The Customer has been added.';
	}
  else
    {
	echo 'The Customer could not be added because - ' . mysql_error();
	}
?>
 
Last edited:
hmmmmmmmmmmmmmm

So its still giving me blank database fields except the aut-increment

Code:
<?php
include("db_connect.php");
// Connect to the database.
//If the form was submitted, process it.
//Pick up the values from the previous form
$forename =$_REQUEST['CustName'];
$surname =$_REQUEST['CustAddress'];
$email =$_REQUEST['CustE-mail'];
$maiden =$_REQUEST['CustTelNo'];


$query = "insert into Customer values
  ('','$CustName', '$CustAddress', '$CustTelNo', '$CustE-mail')";
  if (@mysql_query ($query))
    {
	echo 'The Customer has been added.';
	}
  else
    {
	echo 'The Customer could not be added because - ' . mysql_error();
	}
?>

Its like the code isnt picking up the fields from the previous form? I have added the html form below


Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
        "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
	<title>SCOTIA AIRWAYS</title>
	<link rel="stylesheet" type="text/css" href="layout.css" />
	<SCRIPT language=javascript>

function Validate() 
{
Message = ""
Message = Message + CheckName()
Message = Message + Checksurname()
Message = Message + CheckEmail()
Message = Message + Checkpasswords()
//Message = Message + Checkpassword2()
Message = Message + Checkmaiden()

if (Message == "") 
	{
	return true
	}
else 
	{
	alert(Message)
	return false
	}
}

function CheckName() {
var UserName = document.form1.forename.value

if (UserName == "") {
Message = "Something's wrong with your forename" + "\n"
}
else {
Message = ""
}
return Message

}

function Checksurname() {
var surname = document.form1.surname.value

if (surname == "") {
Message = "Something's wrong with your surname" + "\n"
}
else {
Message = ""
}
return Message

}

function CheckEmail() {
email = document.form1.email.value
AtPos = email.indexOf("@")
StopPos = email.lastIndexOf(".")
Message = ""
if (email == "") {
Message = "Not a valid Email address" + "\n"
}
if (AtPos == -1 || StopPos == -1) {
Message = "Not a valid email address"  + "\n"
}
if (StopPos < AtPos) {
Message = "Not a valid email address"  + "\n"
}
if (StopPos - AtPos == 1) {
Message = "Not a valid email address"  + "\n"
} 
return Message
}

function Checkpasswords() {
password = document.form1.password.value
password2 = document.form1.password2.value

txt = ""

if (password == "") {
Message = "Not a valid password" + "\n"
}
else

if (password == password2) {
Message = ""
}
else {
Message = "passwords dont match" + "\n"
}

return Message

}









//function Checkpassword2() {
//var Checkpassword2 = document.form1.password2.value

//if (Checkpassword2 == "") {
//Checkpassword2 = "please enter password" + "\n"
//}
//else {
//Message = ""
//}
//return Message
//
//}







function Checkmaiden() {
var maiden = document.form1.maiden.value

if (maiden == "") {
Message = "please enter a maiden name" + "\n"
}
else {
Message = ""
}
return Message
}


</SCRIPT>
	</head>
<body>
<div id="wrap">
	<div id="header">
    <h1><img src="bannerforscotiatesttestcopy.jpg" width="600" height="200"></h1>
  </div>
	<div id="nav">
		<ul>
			<li><a href="HOME.htm">Home</a></li>
			<li><a href="REGISTER.htm">Register</a></li>
			<li><a href="LOGIN.htm">Login</a></li>
			<li><a href="VIEWAVAILIBLEFLIGHTS.htm">View Availible Flights</a></li>
			<li><a href="CONTACT.htm">Contact</a></li>
			<li><a href="calculate.htm">Calculate</a></li>
		</ul>
	</div>
	
  <div id="main"> 
    <h2>Welcome...</h2>
    <p>Please Register Below</p>
    <form name="form1" onsubmit="return Validate()" method="post" action="registerphp.php">
      <p><font color="#FF0000"> 
        <input name="forename" type="text" id="forename" />
        Customer Name</font></p>
      <p><font color="#FF0000"> 
        <textarea name="surname" id="surname"></textarea>
        Customer Address</font></p>
      <p><font color="#FF0000"><br />
        <input name="email" type="text" id="email" />
        e-mail address <br />
        <input name="password" type="password" id="password" />
        password<br />
        <input type="password" name="password2" id="password2" />
        confirmation of the password </font></p>
      <p><font color="#FF0000"><br />
        <input type="text" name="maiden" id="maiden" />
        Customer Telephone Number (for security purposes) </font></p>
      <p><font color="#FF0000"> 
        <input type="submit" name="Submit" value="Submit" />
        <input type="reset" name="Submit2" value="Reset" />
        </font></p>
    </form>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
  </div>
	<div id="sidebar">
		<h3>Availible Flights</h3>
		
    <p>Book Your Flight With Scotia Today</p>
		</div>
	<div id="footer">
		
    <p>Copyright: Andrew Comrie 2006</p>
	</div>
</div>
</body>
</html>

any help appreciated! I am getting driven mad.
 
array(7) { ["forename"]=> string(4) "dfsd" ["surname"]=> string(7) "sdfsfss" ["email"]=> string(18) "[email protected]" ["password"]=> string(3) "123" ["password2"]=> string(3) "123" ["maiden"]=> string(6) "sdfsdf" ["Submit"]=> string(6) "Submit" }

Does that mean i should get rid of the textfields not going into the database?
 
You don't have the names of your input right, surely?

You should be accessing $_POST['forename'], $_POST['surname'], $_POST['email'], etc.—not the "CustEmail" etc. stuff that you're currently accessing.

Also, escape your input.
 
hmmmmmmmmmmmm

ok i have done that, now when i submit the php page just shows a blank page.

Code:
<?php
include("db_connect.php");
// Connect to the database.
//If the form was submitted, process it.
//Pick up the values from the previous form
var_dump($_POST);
$CustName =$_POST['forename'];
$CustAddress =$_POST['surname'];
$CustE-mail =$_POST['email'];
$CustTelNo =$_POST['maiden'];


$query = "INSERT INTO Customer values ('','$CustName', '$CustAddress', '$CustTelNo', '$CustE-mail')";
  if (@mysql_query ($query))
    {
	echo 'The Customer has been added.';
	}
  else
    {
	echo 'The Customer could not be added because - ' . mysql_error();
	}
?>
 
Last edited:
your query is wrong, surely?

Code:
INSERT INTO Customer ('db_field','db_field','db_field','db_field','db_field') values('$variable','$variable','$variable','$variable','$variable')

you're not specifying the fields you want to put your data into, the query above is how it should be...i think anyway. maybe i missed the point
 
yeah, but what im saying is you have to tell the database which fields you want to use in your query...the first set of parentheses are for denoting the fields you're using, and the second is for the values you're going to put in. to make it easier, tell us what the field names are that you're using and i'll write you a query that you should just be able to drop in there.

also:

Code:
@mysql_query($query)

no spaces
 
cool, well for a start, you don't need to include your auto-incremenet field in the query. also, take the hyphen out of the email variable and field name.

Code:
mysql_query("INSERT INTO customer ('CustName','CustAddress','CustTelNo','CustEmail') values('$Custname','$CustAddress','$CustTelNo','$CustEmail')")

try that...but don't forget to take out those hyphens first.
 
hmmmmmmmmmm

Code:
<?php
include("db_connect.php");
// Connect to the database.
//If the form was submitted, process it.
//Pick up the values from the previous form
var_dump($_POST);
$CustName =$_POST['forename'];
$CustAddress =$_POST['surname'];
$CustTelNo =$_POST['maiden'];
$CustEmail =$_POST['email'];



mysql_query("INSERT INTO customer ('CustName','CustAddress','CustTelNo','CustEmail')   values('$CustName','$CustAddress','$CustTelNo','$CustEmail')")
  if (@mysql_query($query))
    {
	echo 'The Customer has been added.';
	}
  else
    {
	echo 'The Customer could not be added because - ' . mysql_error();
	}
?>

right ok should it look like that? I have done that but it still comes up with a blank page.
 
What pray tell is this:

'$C ustEmail'

You also left off the ; at the end of the query line............. Helps if you spend 10 seconds reading it :)
 
Back
Top Bottom