Page 1 of 1

SQL Querying Problem

Posted: Mon Apr 04, 2005 12:59 pm
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

Posted: Mon Apr 04, 2005 1:12 pm
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

Posted: Mon Apr 04, 2005 1:32 pm
by Fusioned
Sorry I'm still not that great. Where exactly should I place it and how should I implement it properly?

Posted: Mon Apr 04, 2005 2:12 pm
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.

Posted: Mon Apr 04, 2005 2:19 pm
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.

Posted: Mon Apr 04, 2005 2:47 pm
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.

Posted: Mon Apr 04, 2005 9:20 pm
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

Posted: Mon Apr 04, 2005 9:47 pm
by infolock
post what is on lines 22 and 23 please

Posted: Mon Apr 04, 2005 9:51 pm
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;

Posted: Mon Apr 04, 2005 10:00 pm
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?

Posted: Mon Apr 04, 2005 10:01 pm
by feyd
Fusioned, please use [syntax=php]tags.[/syntax]

Posted: Mon Apr 04, 2005 11:46 pm
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.

Posted: Mon Apr 04, 2005 11:59 pm
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

Posted: Tue Apr 05, 2005 10:54 am
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.

Posted: Tue Apr 05, 2005 3:40 pm
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