SQL Querying Problem

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

Post Reply
User avatar
Fusioned
Forum Commoner
Posts: 32
Joined: Tue Jan 18, 2005 10:43 pm
Location: Philadelphia, PA

SQL Querying Problem

Post by Fusioned »

Ok, I'm trying to make a simple signup form so "members" can enter in their info and it will be stored in a database. Here is the page where you enter the info:

Code: Select all

<?php

///START FORM
print ("<FORM ACTION=\"sql-process.php\" METHOD=POST>\n");

print ("Enter your SQL Host, Username, and Password <BR>\n");

print ("<INPUT TYPE=TEXT NAME=\"hostname\" VALUE=\"localhost\"><BR>\n");

print ("<INPUT TYPE=TEXT NAME=\"sql_username\" VALUE=\"sql_username here\"><BR>\n");

print ("<INPUT TYPE=PASSWORD NAME=\"sql_password\"><BR><BR>\n");

print ("Enter your Information below to Sign Up<BR>\n");
///ENTER INFORMATION


///IF NEEDED, DATE COTE BELOW
///$todaysdate = date("yyyy-m-d");
////print ("<INPUT TYPE=HIDDEN NAME=\"Date\" VALUE=\"$todaysdate\">\n");

///USER ID 4 CHARS - FIX THIS FOR AUTO INCREMENTS
print ("<INPUT TYPE=HIDDEN NAME=\"UserID\" VALUE=\"0002\">\n");

print ("Choose a Username (20 Chars. Max)<BR>\n");

print ("<INPUT TYPE=TEXT NAME=\"UserName\"><BR><BR><\n");

print ("Choose a Password (20 Chars. Max)<BR>\n");

print ("<INPUT TYPE=TEXT NAME=\"Password\"><BR><BR><\n");

print ("First Name<BR>\n");

print ("<INPUT TYPE=TEXT NAME=\"FirstName\"><BR><BR><\n");

print ("Last Name<BR>\n");

print ("<INPUT TYPE=TEXT NAME=\"LastName\"><BR><BR><\n");

print ("E-mail address<BR>\n");

print ("<INPUT TYPE=TEXT NAME=\"Email\"><BR><BR><\n");

print ("City<BR>\n");

print ("<INPUT TYPE=TEXT NAME=\"City\"><BR><BR><\n");
///STATE - MAKE A DROP DOWN
print ("Enter your State (2 letters)<BR>\n");

print ("<INPUT TYPE=TEXT NAME=\"State\"><BR><BR><\n");

print ("Phone Number<BR>\n");

print ("<INPUT TYPE=TEXT NAME=\"Phone\"><BR><BR><\n");
///OP SYS - MAKE DROP DOWN
print ("Operating System<BR>\n");

print ("<INPUT TYPE=TEXT NAME=\"OpSys\"><BR><BR><\n");

///SUBMIT
print ("<INPUT TYPE=SUBMIT NAME=\"Submit\" VALUE=\"Connect\">\n");
///END FORM
print ("</FORM><BR>\n");


?>

And here is the SQL processing page/receipt page:

Code: Select all

<?php

$host = "$hostname";
$user = "$sql_username";
$pass = "$sql_password";
$DBName = "members";
$TableName = "Members";

$link = mysql_connect ($host, $user, $pass);
if (!$link) {
   die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully<BR><BR>';
///BE CAREFUL WITH QUOTES
$todaysdate = date("yyyy-m-d");

$query = "INSERT INTO $TableName (UserID, UserName, CreateDate, Password, FirstName, LastName,
Email, City, State, Phone, OpSys) VALUES '('$UserID', '$UserName', '$todaysdate', '$Password', '$FirstName', '$LastName',
'$Email', '$City', '$State', '$Phone', '$OpSys')'"

///mysql_query($query);


///$thequery = "mysql_query($query)";

if (mysql_db_query ('$DBName', '$query', '$link')) {

echo ("Success! You've joined!<BR>\n");
} else {

echo ("Error! Go back and...fix something!<BR>\n");
}

echo ("Your Query Was:<BR>$query\n");

///KILLS SQL CONNECTION
mysql_close($link);

?>
It's getting errors when it trys to connect and execute the query. I don't get it. I've tried 3 different ways and it doesn't like it. Any ideas? Thanks!

:D

PHENOM | If you are going to post code of any kind, please review Posting Code in the Forums first
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Sounds like you have register globals OFF, while your script requires it to be on.
To append the variables in your processing script, your variables should be appended via $_POST..

for example

Code: Select all

&lt;input type=&quote;text&quote; name=&quote;hostname&quote; value=&quote;localhost&quote;&gt;

Code: Select all

echo $_POST&#1111;'hostname'];
will output localhost on processing.php if the form submitted is pointed to processing.php
User avatar
Fusioned
Forum Commoner
Posts: 32
Joined: Tue Jan 18, 2005 10:43 pm
Location: Philadelphia, PA

Post by Fusioned »

Sorry I'm still not that great. Where exactly should I place it and how should I implement it properly?
sn202
Forum Commoner
Posts: 36
Joined: Thu Dec 16, 2004 7:30 pm

Post by sn202 »

Basically in page "sql-process.php" at the top

Instead of:

Code: Select all

$host = "$hostname";
You should use:

Code: Select all

$host = $_POST['hostname'];
Same for all the rest of the required variables.

Simon.
User avatar
Fusioned
Forum Commoner
Posts: 32
Joined: Tue Jan 18, 2005 10:43 pm
Location: Philadelphia, PA

Post by Fusioned »

basically, it didn't do anything. it's having problems at line 26 when trying to connect. I just cant find a way around it...damn.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

to troubleshoot it, try connecting to your db separate from the query like this:

Code: Select all

mysql_select_db("members")
  or die(mysql_error());

// now try your query

mysql_query("insert into blah values ('bling')")
  or die(mysql_error());
give that a go and it should at least narrow down your problem.
User avatar
Fusioned
Forum Commoner
Posts: 32
Joined: Tue Jan 18, 2005 10:43 pm
Location: Philadelphia, PA

Post by Fusioned »

It just keeps buggin. Im gettin this:


Parse error: parse error, unexpected T_STRING in /home/virtual/site484/fst/var/www/html/interbase/sql-process.php on line 24

Line 24 is:

Code: Select all

mysql_query($query, $link);
This is so frustratinggg. I tried your method burrito but no luck
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

post what is on lines 22 and 23 please
User avatar
Fusioned
Forum Commoner
Posts: 32
Joined: Tue Jan 18, 2005 10:43 pm
Location: Philadelphia, PA

Post by Fusioned »

line 22 is the query itself

Code: Select all

$query = &quote;INSERT INTO $TableName (UserID, UserName, CreateDate, Password, FirstName, LastName,
Email, City, State, Phone, OpSys) VALUES '('$UserID', '$UserName', '$todaysdate', '$Password', '$FirstName', '$LastName',
'$Email', '$City', '$State', '$Phone', '$OpSys')'&quote;
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

you are missing a ; ;)

should be this

Code: Select all

$query = &quote;INSERT INTO &quote;.$TableName.&quote; (UserID, UserName, CreateDate, Password, FirstName, LastName,Email, City, State, Phone, OpSys) VALUES ('&quote;.$UserID.&quote;', '&quote;.$UserName.&quote;', '&quote;.$todaysdate.&quote;', '&quote;.$Password.&quote;', '&quote;.$FirstName.&quote;', '&quote;.$LastName.&quote;','&quote;.$Email.&quote;', '&quote;.$City.&quote;', '&quote;.$State.&quote;', '&quote;.$Phone.&quote;', '&quote;.$OpSys.&quote;')'&quote;;
edit: just to touch base on 2 different things about this query...

1) I exit variables. You don't *have* to do it this way, I just choose to. If you use an editor that color-coats your code, then it helps as it pretty much makes the variables stand out (useful for me when debugging)...

2) is your UserID field an autoinc field? If not, why isn't it? if it is an autoinc, then why are you trying to post data into it?
Last edited by infolock on Mon Apr 04, 2005 10:08 pm, edited 2 times in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Fusioned, please use [syntax=php]tags.[/syntax]
User avatar
Fusioned
Forum Commoner
Posts: 32
Joined: Tue Jan 18, 2005 10:43 pm
Location: Philadelphia, PA

Post by Fusioned »

Sorry feyd. And wow. It is amazing what missing a semicolon will do. It's "fixed" now but it's having an access issue.

Access denied for user: 'myusernamehere@localhost' to database 'members'

This is a bit odd. Im using the same login I use for myPHPAdmin and if i change the password, i get a completely different error. This one apparently shows I've logged in, yet doesnt wanna access it.
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

make sure you have added that database as accessable by the username you are using.. see the mysql documentation on MySQL User Account Management
User avatar
Fusioned
Forum Commoner
Posts: 32
Joined: Tue Jan 18, 2005 10:43 pm
Location: Philadelphia, PA

Post by Fusioned »

I emailed my hosting company asking them if I have the ability to login SQL-wise as root and to create a new user (by the way, thanks, I checked out the manual here: http://dev.mysql.com/doc/mysql/en/adding-users.html)

So, I suppose not having a user with proper access is the problem. However, it doesnt make much sense considering Ive installed tons of things using SQL like phpBB, vBulliten, Invision Board, Movable Type, etc.

Any other ideas? The script is complete, but access is now a problem.

Thanks a ton for everyone's help by the way.
User avatar
Fusioned
Forum Commoner
Posts: 32
Joined: Tue Jan 18, 2005 10:43 pm
Location: Philadelphia, PA

Post by Fusioned »

I am....insanely happy to report...the script is now working just fine. I also redid my database to have the auto_increment function. A big problem was my hosting, but they let me know the proper names for my database and the proper login stuff...so now it works! Thanks a ton everyone!...if you'd like to try it out, go here: http://www.junkthatrocks.com/interbase/ ... onnect.php
Post Reply