Troubles with PHP code - trying to post to mySQL

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

etech
Forum Newbie
Posts: 11
Joined: Fri Jan 22, 2010 8:26 am

Troubles with PHP code - trying to post to mySQL

Post 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);
?>
aravona
Forum Contributor
Posts: 347
Joined: Sat Jun 13, 2009 3:59 pm
Location: England

Re: Troubles with PHP code - trying to post to mySQL

Post by aravona »

Code: Select all

$SQL = "INSERT INTO 'users'";
this should be

Code: Select all

$SQL = "INSERT INTO users";
I dont think you need the quotes.
etech
Forum Newbie
Posts: 11
Joined: Fri Jan 22, 2010 8:26 am

Re: Troubles with PHP code - trying to post to mySQL

Post 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.
aravona
Forum Contributor
Posts: 347
Joined: Sat Jun 13, 2009 3:59 pm
Location: England

Re: Troubles with PHP code - trying to post to mySQL

Post 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.
etech
Forum Newbie
Posts: 11
Joined: Fri Jan 22, 2010 8:26 am

Re: Troubles with PHP code - trying to post to mySQL

Post 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.
User avatar
SimpleManWeb
Forum Commoner
Posts: 57
Joined: Wed Dec 30, 2009 4:15 pm
Location: New Hampshire, USA

Re: Troubles with PHP code - trying to post to mySQL

Post 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
etech
Forum Newbie
Posts: 11
Joined: Fri Jan 22, 2010 8:26 am

Re: Troubles with PHP code - trying to post to mySQL

Post 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.
etech
Forum Newbie
Posts: 11
Joined: Fri Jan 22, 2010 8:26 am

Re: Troubles with PHP code - trying to post to mySQL

Post 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!
User avatar
SimpleManWeb
Forum Commoner
Posts: 57
Joined: Wed Dec 30, 2009 4:15 pm
Location: New Hampshire, USA

Re: Troubles with PHP code - trying to post to mySQL

Post 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
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Troubles with PHP code - trying to post to mySQL

Post 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');
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
tabutcher
Forum Newbie
Posts: 20
Joined: Fri Aug 21, 2009 7:10 am

Re: Troubles with PHP code - trying to post to mySQL

Post 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.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Troubles with PHP code - trying to post to mySQL

Post 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.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
etech
Forum Newbie
Posts: 11
Joined: Fri Jan 22, 2010 8:26 am

Re: Troubles with PHP code - trying to post to mySQL

Post 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');
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Troubles with PHP code - trying to post to mySQL

Post by AbraCadaver »

Yep, check the line before that.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
etech
Forum Newbie
Posts: 11
Joined: Fri Jan 22, 2010 8:26 am

Re: Troubles with PHP code - trying to post to mySQL

Post 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();
?>
Post Reply