Page 1 of 2
Troubles with PHP code - trying to post to mySQL
Posted: Fri Jan 22, 2010 8:33 am
by etech
So I am trying to post form data to a mySQL database, and am having troubles with the form that's supposed to do it.
Here's the issue:
I have register.php pointing my form to submit-registration.php, which will insert data into a database (blueplanet) table (users). The form works correctly in pointing to submit-registration.php, but the submit-registration.pph does not insert into the table, although it looks as though it is connecting to the database.
Anyway, here is the code I have for submit-registration.php. Can someone look at it and tell me what could possibly be the problem? Thanks!
Code: Select all
<?php
if ($_SERVER['REQUEST_METHOD'] == "POST") {
mysql_connect(localhost,root,probiz123);
@mysql_select_db(blueplanet) or die( "Unable to select database");
// the following 4 lines are needed if your server has register_globals set to Off
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$address1 = $_POST['address1'];
$address2 = $_POST['address2'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$username = $_POST['username'];
$password = $_POST['pwd'];
$hear = $_POST['hear'];
$empbus = $_POST['empbus'];
$favtravel = $_POST['favtravel'];
$phone = $_POST['phone'];
$SQL = "INSERT INTO 'users'";
$SQL = $SQL . "(fname, lname, address1, address2, city, state, zip, username, pwd, hear, empbus, favtravel, phone) VALUES";
$SQL = $SQL . "('$fname', '$lname','$address1','$address2','$city','$state','$zip','$username','$password','$hear','$empbus','$favtravel','$phone')";
$result = mysql_db_query($db,'$SQL',$cid);
if (!$result) {
echo("ERROR: " . mysql_error() . "\n$SQL\n"); }
echo ("Your registration has been submitted. You can now login to the Blue Planet Website.\n");
}
mysql_close($cid);
?>
Re: Troubles with PHP code - trying to post to mySQL
Posted: Fri Jan 22, 2010 8:48 am
by aravona
this should be
I dont think you need the quotes.
Re: Troubles with PHP code - trying to post to mySQL
Posted: Fri Jan 22, 2010 9:10 am
by etech
No luck yet...now getting this error -
ERROR: INSERT INTO users(fname, lname, address1, address2, city, state, zip, username, pwd, hear, empbus, favtravel, phone) VALUES('Test', 'Test','Test','','Test','Kentucky','41018','etech','probiz123','Employee Referral','Tester Test','Pigeon Forge, TN','5555555555') Your registration has been submitted. You can now login to the Blue Planet Website.
Re: Troubles with PHP code - trying to post to mySQL
Posted: Fri Jan 22, 2010 9:16 am
by aravona
is it still not inserting into the DB ?
Have you tried your SQL code directly? when something doesnt work in my php I test it'll work in my MySQL console (cos I use wamp)
$result = mysql_db_query($db,'$SQL',$cid);
Also dont think you need quotes there either. I dont know if it'll effect anything but I've never used quotes around my variables in this context.
Re: Troubles with PHP code - trying to post to mySQL
Posted: Fri Jan 22, 2010 12:09 pm
by etech
Not inserting into the database, but I do know other websites are working with mySQL on my server, so the connection is not the problem, it seems to be the form.
Removed the quotes and still no luck.
Re: Troubles with PHP code - trying to post to mySQL
Posted: Fri Jan 22, 2010 12:36 pm
by SimpleManWeb
I'm willing to bet that you aren't actually connected to the database
Code: Select all
mysql_connect(localhost,root,probiz123);
@mysql_select_db(blueplanet) or die( "Unable to select database");
The @ symbol in the above code tells it to not throw any errors, which means that if you didn't connect, you wouldn't know it. I bet if you remove that @ you will get an error. You need to hand in strings for your host, username and password. Try this code
Code: Select all
mysql_connect('localhost','root','probiz123');
mysql_select_db('blueplanet') or die( "Unable to select database");
I didn't test this but it should get you a lot closer to your goal
Re: Troubles with PHP code - trying to post to mySQL
Posted: Fri Jan 22, 2010 1:37 pm
by etech
Changed the code a bit to this....
Code: Select all
<?
mysql_connect('localhost','root','probiz123');
mysql_select_db('blueplanet') or die("Unable to select database");
// the following 4 lines are needed if your server has register_globals set to Off
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$address1 = $_POST['address1'];
$address2 = $_POST['address2'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$username = $_POST['username'];
$password = $_POST['pwd'];
$hear = $_POST['hear'];
$empbus = $_POST['empbus'];
$favtravel = $_POST['favtravel'];
$phone = $_POST['phone'];
$id = $_POST[mysql_insert_id()]
$query = "INSERT INTO users VALUES ('$username','$password','$address1','$address2','$city','$state','$zip','$email','$hear','$empbus','$favtravel','$phone','$id')";
mysql_query($query);
if (!$result) {
echo("ERROR: " . mysql_error() . "\n$query\n"); }
echo ("Your registration has been submitted. You can now login to the Blue Planet Website.\n");
mysql_close();
?>
Now I am not getting any error and it's not posting to my database. Any clues on what I should be doing next?
Thanks.
Re: Troubles with PHP code - trying to post to mySQL
Posted: Fri Jan 22, 2010 3:30 pm
by etech
Anymore help on this would be super! I am trying to finish coding this for a client, but am having a rough time doing so. Thanks ahead of time!
Re: Troubles with PHP code - trying to post to mySQL
Posted: Fri Jan 22, 2010 8:41 pm
by SimpleManWeb
I'm exhausted right now so I may be missing something obvious. The only thing I see is your insert statement looks like it would throw an error. Try this.
Code: Select all
# $query = "INSERT INTO users (`username`,`password`,`address1`,`address2`,`city`,`state`,`zip`,`email`,`hear`,`empbus`,`favtravel`,`phone`,`id`) VALUES ('$username','$password','$address1','$address2','$city','$state','$zip','$email','$hear','$empbus','$favtravel','$phone','$id')";
Hope it helps
Re: Troubles with PHP code - trying to post to mySQL
Posted: Sat Jan 23, 2010 11:51 am
by AbraCadaver
etech wrote:Anymore help on this would be super! I am trying to finish coding this for a client, but am having a rough time doing so. Thanks ahead of time!
All kinds of errors in your code, from parse to database errors. Add to top:
Code: Select all
error_reporting(E_ALL);
ini_set('display_errors', '1');
Re: Troubles with PHP code - trying to post to mySQL
Posted: Sat Jan 23, 2010 4:57 pm
by tabutcher
hi,
don't you need to assign the function to a variable for example:
Code: Select all
resourceId = mysql_connect(server, username, password);
and when you are selecting a database the parameters to pass are the database name and the resource returned from the above connection function.
Code: Select all
mysql_select_db(databaseName, resourceId);
hope this leads you in the right direction.
Re: Troubles with PHP code - trying to post to mySQL
Posted: Sat Jan 23, 2010 5:31 pm
by AbraCadaver
tabutcher wrote:hi,
don't you need to assign the function to a variable for example:
Code: Select all
resourceId = mysql_connect(server, username, password);
and when you are selecting a database the parameters to pass are the database name and the resource returned from the above connection function.
Code: Select all
mysql_select_db(databaseName, resourceId);
hope this leads you in the right direction.
There are many more errors than that. The current parse error kills the script before any of that.
Re: Troubles with PHP code - trying to post to mySQL
Posted: Mon Jan 25, 2010 8:07 am
by etech
So here's the current code:
Code: Select all
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$address1 = $_POST['address1'];
$address2 = $_POST['address2'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$username = $_POST['username'];
$password = $_POST['pwd'];
$hear = $_POST['hear'];
$empbus = $_POST['empbus'];
$favtravel = $_POST['favtravel'];
$phone = $_POST['phone'];
$id = $_POST[mysql_insert_id()]
$resourceId = mysql_connect('localhost', 'root', 'probiz123');
mysql_select_db('blueplanet', $resourceId);
$query = "INSERT INTO users (`username`,`password`,`address1`,`address2`,`city`,`state`,`zip`,`email`,`hear`,`empbus`,`favtravel`,`phone`,`id`) VALUES ('$username','$password','$address1','$address2','$city','$state','$zip','$email','$hear','$empbus','$favtravel','$phone','$id')";
mysql_query($query);
if (!$result) {
echo("ERROR: " . mysql_error() . "\n$query\n"); }
echo ("Your registration has been submitted. You can now login to the Blue Planet Website.\n");
mysql_close();
?>
And here's the error thrown:
Parse error: syntax error, unexpected T_VARIABLE in D:\WEBROOT\test.blueplanetadventuretravel.com\submit-registration.php on line 35
Line 35 is this line in the code:
Code: Select all
$resourceId = mysql_connect('localhost', 'root', 'probiz123');
Re: Troubles with PHP code - trying to post to mySQL
Posted: Mon Jan 25, 2010 9:03 am
by AbraCadaver
Yep, check the line before that.
Re: Troubles with PHP code - trying to post to mySQL
Posted: Mon Jan 25, 2010 9:40 am
by etech
Okay, so I have a lot of the bugs worked out, but I still have one lingering bug.
Warning: mysql_insert_id() expects parameter 1 to be resource, string given in D:\WEBROOT\test.blueplanetadventuretravel.com\submit-registration.php on line 35 Notice: Undefined index: in D:\WEBROOT\test.blueplanetadventuretravel.com\submit-registration.php on line 35
Let me give you a little idea of what I am looking for. I want my form to also check for registrations based on username and email address and tell the person that a username and/or email address already exists in the database for what they have selected. The 'id' variable may not even need to exist, as the database auto-increments an id when a person submits their registration. Should this form submit an ID? Could you possibly tell me what code I need to get what I am looking for and what I have asked, and what I need to do to get this error fixed? Thanks for all the help and ahead of time for the help too!
Again, here is the code:
Code: Select all
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
$resourceId = mysql_connect('localhost', 'root', 'probiz123');
mysql_select_db('blueplanet', $resourceId);
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$address1 = $_POST['address1'];
$address2 = $_POST['address2'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$username = $_POST['username'];
$password = $_POST['pwd'];
$hear = $_POST['hear'];
$empbus = $_POST['empbus'];
$favtravel = $_POST['favtravel'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$id = $_POST[mysql_insert_id()];
$query = "INSERT INTO users (`username`,`password`,`address1`,`address2`,`city`,`state`,`zip`,`email`,`hear`,`empbus`,`favtravel`,`phone`,`id`) VALUES ('$username','$password','$address1','$address2','$city','$state','$zip','$email','$hear','$empbus','$favtravel','$phone','$id')";
mysql_query($query);
mysql_close();
?>