[SOLVED] Can anyone see anything wrong with this code? pls

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
nutstretch
Forum Contributor
Posts: 104
Joined: Sun Jan 11, 2004 11:46 am
Location: Leicester

[SOLVED] Can anyone see anything wrong with this code? pls

Post by nutstretch »

I am trying to add a record to a database but for some reason it is not working.

Can anyone see what may be wrong with the code. The resulting page tells me it has not worked . Displays what is in the database but not the record I have just added'

Code:

Code: Select all

<?php
$linkID = @mysql_connect("localhost", "root", "");
mysql_select_db("Customer", $linkID);

$result = mysql_query("INSERT INTO tblcustomer( CustName, CustAdd1, CustAdd2, CustAdd3, CustCity, CustCounty, CustPostCode, CustPhone, CustEmail,  ShoeSize, CustGender) VALUES( '$CustName', '$CustAdd1', '$CustAdd2', '$CustAdd3', '$CustCity', '$CustCounty', '$CustPostCode', '$CustPhone', '$CustEmail', '$ShoeSize', '$CustGender')", $linkID);
if ($result == true)
{
	print "The New Record Has Been Added<p>";
}
else
{
	print "Something wrong here<p>";
}

$resultID = mysql_query("SELECT * FROM tblCustomer", $linkID);
Print "<table border=1 ><tr><th>ID</th>";
print "<th>Name</th><th>Addr1</TH><th>Addr2</th><th>Addr3</th><th>City</th><th>County</th><th>PostCode</th><th>Phone</th><th>Email</th><th>Date Entered</th><th>Shoe Size</th><th>Gender</th>";
while($row = mysql_fetch_row($resultID))
{
	print"<tr>";
foreach($row as $field)
	{
		print"<td>$field</td>";
	}
	Print "</tr>";
}

print "</table>";

mysql_close($linkID);

?>
Any help greatly appreciated
User avatar
Dr Evil
Forum Contributor
Posts: 184
Joined: Wed Jan 14, 2004 9:56 am
Location: Switzerland

Post by Dr Evil »

Could you give us your DB structure and the form ?
I suspect wrong type fields.

Dr Evil
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

also, give us an error to go with... a good thing to use is or die statement...

so, for the first $result use something ilke this :

Code: Select all

$result = mysql_query("INSERT INTO tblcustomer (CustName, CustAdd1, CustAdd2, CustAdd3, CustCity, CustCounty, CustPostCode, CustPhone, CustEmail,  ShoeSize, CustGender) VALUES( '".$CustName."', '".$CustAdd1."', '".$CustAdd2."', '".$CustAdd3."', '".$CustCity."', '".$CustCounty."', '".$CustPostCode."', '".$CustPhone."', '".$CustEmail."', '".$ShoeSize."', '".$CustGender."')", $linkID) or die(mysql_error());
and

Code: Select all

$resultID = mysql_query("SELECT * FROM tblCustomer", $linkID) or die(mysql_error());
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Just to point out the script also seems to require register_globals to be On if the values are coming from a form/url etc..etc..
If register_globals are Off then you'll need to use '{$_POST['CustName'}' instead of '$CustName' for example.
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

good eye mark i missed that part. guess i was assuming he had defined the variables before hand lol...
nutstretch
Forum Contributor
Posts: 104
Joined: Sun Jan 11, 2004 11:46 am
Location: Leicester

Post by nutstretch »

the fields are all being posted from a form previous to this one. This is working on a simialr form.

the database is mysql4. the fieldnames are exactly as shown and the posted fields have been given the same names so that i do not get confused. I have double checked each of these several times (if that sentance makes sense)

This is what i am getting on the form

Adding Data: Heading
Something wrong here : my if statement result<p>
<b>This is in a table form</b></p>
ID Name Addr1 Addr2 Addr3 City County PostCode Phone Email Date Entered Shoe Size Gender
1 Angie Stokes 7 penny lane Barwell Leicester Leics LE9 8HJ 01455 451971 angie@fourthinking.co.uk 2004-01-15 8 Female
nutstretch
Forum Contributor
Posts: 104
Joined: Sun Jan 11, 2004 11:46 am
Location: Leicester

Post by nutstretch »

when i put the or die bit in it tells me

Cannot add or update a child row: a foreign key constraint fails

Does this mean i have something set wrong in my database?
microthick
Forum Regular
Posts: 543
Joined: Wed Sep 24, 2003 2:15 pm
Location: Vancouver, BC

Post by microthick »

This means that in this table you have a foreign key relating this table to another, maybe the ID or CustName.

This conflict probably means that in that other table (the parent table), there is no existing ID of the ID you are inserting. This violates the relationship.

Is there an ID column that's supposed to be auto incrementing, but isn't?
nutstretch
Forum Contributor
Posts: 104
Joined: Sun Jan 11, 2004 11:46 am
Location: Leicester

Post by nutstretch »

Thanks i have solved it now my foriegn key was pointing in the wrong direction!!!!

Many thanks peeps i will put the die part on to all form now on it saves a lot of thinking eh!!
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

nutstretch wrote:Many thanks peeps i will put the die part on to all form now on it saves a lot of thinking eh!!
And this can save a little bit more (a script could have many queries):

Code: Select all

<?php
or die(mysql_error() . '<br />location: ' __LINE__ . ' | ' . __FILE__)
?>
If you define the db query string as a var you can feed that into the report as well.
Post Reply