Page 1 of 1

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

Posted: Thu Jan 15, 2004 3:24 pm
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

Posted: Thu Jan 15, 2004 3:36 pm
by Dr Evil
Could you give us your DB structure and the form ?
I suspect wrong type fields.

Dr Evil

Posted: Thu Jan 15, 2004 4:44 pm
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());

Posted: Thu Jan 15, 2004 4:48 pm
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.

Posted: Thu Jan 15, 2004 4:49 pm
by infolock
good eye mark i missed that part. guess i was assuming he had defined the variables before hand lol...

Posted: Fri Jan 16, 2004 1:30 am
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

Posted: Fri Jan 16, 2004 1:35 am
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?

Posted: Fri Jan 16, 2004 1:59 am
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?

Posted: Fri Jan 16, 2004 2:38 am
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!!

Posted: Fri Jan 16, 2004 3:26 am
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.