View Full Version : Need help with my first php script
krabople
Mar 25th 2007, 6:12 am
Hi, I have just attempted my first php script. The script is designed to take information passed from a form on the page before it and then append it into a table in a database. However, when I run it I don't get any error messages or anything, just a blank screen. When I check the database it hasn't updated. Could someone please look through my code and tell me where I'm going wrong? Any advice would be greatly appreciated!
aredhelrim
Mar 25th 2007, 6:22 am
Line 32
$sql = "INSERT INTO Users SET
Username='$username',
Password='$password',
Address1='$address1',
Address2='$address2',
Town='$town',
Postcode='$postcode',
Homephone='$homephone',
Mobile='$mobile',
DOB='$dob'";
Usage Insert : INSERT INTO tableName (field, another_field, more_field) VALUES ('$data', '$another_data', '$more_data')
You must use "SET" for UPDATE queries.
// aredhelrim
klown
Mar 25th 2007, 7:13 am
I recommend just adding a bit more to your error detect so it tells the error on the page. You could always disable the error later. Change your code as follows:
//error checking code
$dbcnx = mysql_connect('localhost', 'user', 'password');
if (!$dbcnx ) {
die('Connection error: ' . mysql_error());
}
/*Old Code
$dbcnx = @mysql_connect('localhost', 'user', 'password');
if (!$dbcnx) {
exit('<p>Unable to connect to the database server at this time.</p>');
}
if (!mysql_select_db('mydb')) {
exit('<p>Unable to locate the krabople database at this time.</p>');
}*/
Then again the problem might not be in your connection. So change the code down here..
//Error Detection Code
$result = mysql_query($sql);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
/*
Old Code
if(@mysql_query($sql)) {
echo '<p>Your registration was successful!</p>';
} else {
echo '<p>There was an error while processing your registration: ' . mysql_error() . '</p>';
}
}*/
This should allow you to figure it out yourself.. If you still have a problem post a bit more about it along with the error code.
krabople
Mar 25th 2007, 9:19 am
Hi, thanks for the advice. However, I have tried changing the code as suggested and nothing happens. The annoying thing is that there is no error message whatsoever, just a blank screen. I know that php is definitely working on my computer as I have tried deleting everything and just displaying variables using the echo command. Any idea why the screen might be completely blank? If I miss a bracket or something it tells me there is an error but it is not doing that so I presume the problem is not with the syntax
klown
Mar 25th 2007, 7:14 pm
if you changed the code as I suggested you should be getting an error message. Unless you had a PHP error I didn't notice.. You are accessing the php code from within a PHP server right?
Anyhow I'm sure your doing that so I would advise you to turn on your local error reporting by modifying your php.ini file. You can find documentation on that here at php.net (http://cn.php.net/errorfunc)
Robert Plank
Mar 25th 2007, 9:37 pm
You must use "SET" for UPDATE queries.
Says who? I avoid VALUES like the plague... SET is more readable and will work for update and insert.
krabople
Mar 26th 2007, 2:23 am
Hi, thanks a lot for your help - the answer did lie in the php.ini file. I just needed to unremark the mysql.dll line - I thought I had done it but I'd done the wrong one. Code works fine now.
vBulletin® v3.6.8, Copyright ©2000-2008, Jelsoft Enterprises Ltd.