Connecting to MySQL database

Connecting to MySQL database

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 1700 Views - Last Post: 29 July 2009 - 07:08 AM Rate Topic: -----

#1 bammo10  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 23-July 09

Connecting to MySQL database

Posted 28 July 2009 - 10:06 AM

Hey everybody :P

I have made this html form:
<html>
<title>Check your registration key</title>
<form action="Check.php">
<input type="text" name="Key"> Enter your registration key.
<input type="submit" value="Check key"
</form>
</html>



So the idea is to let people check their reg. key by searching a mySQL database
Do you guys have any idea what check.php has to look like?
Redirecting me to a tutorial is OK.

Thank you in advance,
Chris

Is This A Good Question/Topic? 0
  • +

Replies To: Connecting to MySQL database

#2 RPGonzo  Icon User is offline

  • // Note to self: hmphh .... I forgot
  • member icon

Reputation: 151
  • View blog
  • Posts: 954
  • Joined: 16-March 09

Re: Connecting to MySQL database

Posted 28 July 2009 - 10:16 AM

So your question is you need to know how to connect to the DB or how to SEARCH the DB with the key provided?

to search it its just a small query
$key = $_POST['Key'];
$query = "SELECT * FROM table WHERE key='$key'";
$result = mysql_query($query) or die (mysql_error());
$theCount = mysql_num_rows($result);

if ($theCount == 1) {
 echo "Your key is good";
} else {
 echo "Your key is not good";
}



if you need to connect you need to have some information ( username, password, etc) so you would use something like

$dbhost = 'localhost';
$dbuser = 'yourusername';
$dbpass = 'yourpassword';

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die (mysql_error());

$dbname = 'yourdbname';
mysql_select_db($dbname) or die (mysql_error());



EDIT: i had a typo in the first section of code -_-

This post has been edited by RPGonzo: 28 July 2009 - 10:42 AM

Was This Post Helpful? 1
  • +
  • -

#3 bammo10  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 23-July 09

Re: Connecting to MySQL database

Posted 28 July 2009 - 10:51 AM

View PostRPGonzo, on 28 Jul, 2009 - 09:16 AM, said:

So your question is you need to know how to connect to the DB or how to SEARCH the DB with the key provided?

to search it its just a small query
$key = $_POST['Key'];
$query = "SELECT * FROM table WHERE key='$key'";
$result = mysql_query($query) or die (mysql_error());
$theCount = mysql_num_rows($result);

if ($theCount == 1) {
 echo "Your key is good";
} else {
 echo "Your key is not good";
}



if you need to connect you need to have some information ( username, password, etc) so you would use something like

$dbhost = 'localhost';
$dbuser = 'yourusername';
$dbpass = 'yourpassword';

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die (mysql_error());

$dbname = 'yourdbname';
mysql_select_db($dbname) or die (mysql_error());



EDIT: i had a typo in the first section of code -_-

Hey I used your code, I thought it would be working perfect but,
When I use it, it shows a blank page,
here's my code.
If I'm wrong please correct it RPGonzo.
<?
$dbhost = 'localhost';
$dbuser = 'yourusername';
$dbpass = 'yourpassword';

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die (mysql_error());

$dbname = 'yourdbname';
mysql_select_db($dbname) or die (mysql_error());


$key = $_POST['Key'];
$query = "SELECT * FROM table WHERE key='$key'";
$result = mysql_query($query) or die (mysql_error());
$theCount = mysql_num_rows($result);

if ($theCount == 1) {
echo "Your key is good";
} else {
echo "Your key is not good";
}
?>


Was This Post Helpful? 0
  • +
  • -

#4 Wimpy  Icon User is offline

  • R.I.P. ( Really Intelligent Person, right? )
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,038
  • Joined: 02-May 09

Re: Connecting to MySQL database

Posted 28 July 2009 - 11:00 AM

Perhaps your server configuration doesn't support "php short tags" e g "<?" try using "<?php" instead:
<?php
$dbhost = 'localhost';
$dbuser = 'yourusername';
$dbpass = 'yourpassword';

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die (mysql_error());

$dbname = 'yourdbname';
mysql_select_db($dbname) or die (mysql_error());


$key = $_POST['Key'];
$query = "SELECT * FROM table WHERE key='$key'";
$result = mysql_query($query) or die (mysql_error());
$theCount = mysql_num_rows($result);

if ($theCount == 1) {
echo "Your key is good";
} else {
echo "Your key is not good";
}
?>
and also, you need to have MySQL and the specified database and such, but I figure that is quite obvious?
Was This Post Helpful? 0
  • +
  • -

#5 RPGonzo  Icon User is offline

  • // Note to self: hmphh .... I forgot
  • member icon

Reputation: 151
  • View blog
  • Posts: 954
  • Joined: 16-March 09

Re: Connecting to MySQL database

Posted 28 July 2009 - 11:05 AM

You also have to modify some parts of that script to make it work for YOU ...

$dbhost = 'localhost';
$dbuser = 'yourusername';
$dbpass = 'yourpassword';
$dbname = 'yourdbname';



all those variables need to be changed your your information ...

$query = "SELECT * FROM table WHERE key='$key'";



"table" needs to be changed to the table your working with as well ...

edit:

Quote

and also, you need to have MySQL and the specified database and such, but I figure that is quite obvious?


I just saw that lol

This post has been edited by RPGonzo: 28 July 2009 - 11:06 AM

Was This Post Helpful? 0
  • +
  • -

#6 bammo10  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 23-July 09

Re: Connecting to MySQL database

Posted 28 July 2009 - 11:13 AM

View PostWimpy, on 28 Jul, 2009 - 10:00 AM, said:

Perhaps your server configuration doesn't support "php short tags" e g "<?" try using "<?php" instead:
<?php
$dbhost = 'localhost';
$dbuser = 'yourusername';
$dbpass = 'yourpassword';

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die (mysql_error());

$dbname = 'yourdbname';
mysql_select_db($dbname) or die (mysql_error());


$key = $_POST['Key'];
$query = "SELECT * FROM table WHERE key='$key'";
$result = mysql_query($query) or die (mysql_error());
$theCount = mysql_num_rows($result);

if ($theCount == 1) {
echo "Your key is good";
} else {
echo "Your key is not good";
}
?>
and also, you need to have MySQL and the specified database and such, but I figure that is quite obvious?


Thanks that sort of helped...
now I get the following notice:
Notice: Undefined index: Key in C:\wamp\www\Check.php on line 10
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'table WHERE key=''' at line 1

Any solutions for this problem?
Chris

View PostRPGonzo, on 28 Jul, 2009 - 10:05 AM, said:

You also have to modify some parts of that script to make it work for YOU ...

$dbhost = 'localhost';
$dbuser = 'yourusername';
$dbpass = 'yourpassword';
$dbname = 'yourdbname';



all those variables need to be changed your your information ...

$query = "SELECT * FROM table WHERE key='$key'";



"table" needs to be changed to the table your working with as well ...

edit:

Quote

and also, you need to have MySQL and the specified database and such, but I figure that is quite obvious?


I just saw that lol


Yeah thanks but I'm smart enough to know that I have to put my own detailles in -_-
But now I get the notice from my last reply.
Any solutions?

Edit: I'm using wimpy's code.
Chris

This post has been edited by bammo10: 28 July 2009 - 11:20 AM

Was This Post Helpful? 0
  • +
  • -

#7 RPGonzo  Icon User is offline

  • // Note to self: hmphh .... I forgot
  • member icon

Reputation: 151
  • View blog
  • Posts: 954
  • Joined: 16-March 09

Re: Connecting to MySQL database

Posted 28 July 2009 - 11:30 AM

Your trying to use it wihtout a form being posted to that form ...

change

$key = $_POST['Key'];



to

if (isset($_POST['Key']) && $_POST['Key'] != "") {
 $key = $_POST['Key'];
} else {
 echo "The key to search for was not set!";
die;
}



EDIT:
I just noticed in your original form your going to have to make a change for $_POST to work ...

<form action="Check.php">



use instead ..
<form action="Check.php" method="post">



That way the server knows to look for a POST not a GET

This post has been edited by RPGonzo: 28 July 2009 - 11:36 AM

Was This Post Helpful? 0
  • +
  • -

#8 bammo10  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 23-July 09

Re: Connecting to MySQL database

Posted 28 July 2009 - 11:45 AM

View PostRPGonzo, on 28 Jul, 2009 - 10:30 AM, said:

Your trying to use it wihtout a form being posted to that form ...

change

$key = $_POST['Key'];



to

if (isset($_POST['Key']) && $_POST['Key'] != "") {
 $key = $_POST['Key'];
} else {
 echo "The key to search for was not set!";
die;
}



EDIT:
I just noticed in your original form your going to have to make a change for $_POST to work ...

<form action="Check.php">



use instead ..
<form action="Check.php" method="post">



That way the server knows to look for a POST not a GET

Doesn't work
If I enter a key in the textbox, it still says I didn't.
I can't find any typo's or such in my current code but i hope you can.
BTW. in my addresbar = ....Check.php?Key=321342
That means he posts it to check right?

Check.php
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
if (isset($_POST['Key']) && $_POST['Key'] != "") {
$key = $_POST['Key'];
} else {
echo "The key to search for was not set!";
die;
}
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die (mysql_error());

$dbname = 'keys';
mysql_select_db($dbname) or die (mysql_error());
$query = "SELECT * FROM table WHERE key='$key'";
$result = mysql_query($query) or die (mysql_error());
$theCount = mysql_num_rows($result);

if ($theCount == 1) {
echo "Your key is good";
} else {
echo "Your key is not good";
}

?>



Index.html

<html>
<title>Check your registration key</title>
<form action="Check.php" method="post">
<input type="text" name="Key"> Enter your registration key.
<input type="submit" value="Check key"
</form>
</html>




Chris

This post has been edited by bammo10: 28 July 2009 - 11:47 AM

Was This Post Helpful? 0
  • +
  • -

#9 RPGonzo  Icon User is offline

  • // Note to self: hmphh .... I forgot
  • member icon

Reputation: 151
  • View blog
  • Posts: 954
  • Joined: 16-March 09

Re: Connecting to MySQL database

Posted 28 July 2009 - 11:54 AM

I just copied and pasted your code exactly as you have it and it works ...

If you see the value in the URL that means its trying to use GET ...

which you CAN use if you like just swap the $_POST for $_GET .. but just personal preference i would rather let the server handle the data not the URL ...

EDIT: i just found this in your FORM ...

<input type="submit" value="Check key"



you didnt close the input ...

<input type="submit" value="Check key"/>



If your using IE i think it will cause the form to not operate right ... FF auto corrects it

This post has been edited by RPGonzo: 28 July 2009 - 11:57 AM

Was This Post Helpful? 0
  • +
  • -

#10 Wimpy  Icon User is offline

  • R.I.P. ( Really Intelligent Person, right? )
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,038
  • Joined: 02-May 09

Re: Connecting to MySQL database

Posted 28 July 2009 - 01:23 PM

To be honest I don't know, but if I remember correctly web forms defaults to post as method? :) No reason to be lazy though! ^^

View PostRPGonzo, on 28 Jul, 2009 - 08:30 PM, said:

I just noticed in your original form your going to have to make a change for $_POST to work ...

<form action="Check.php">



use instead ..
<form action="Check.php" method="post">



That way the server knows to look for a POST not a GET

Was This Post Helpful? 0
  • +
  • -

#11 RPGonzo  Icon User is offline

  • // Note to self: hmphh .... I forgot
  • member icon

Reputation: 151
  • View blog
  • Posts: 954
  • Joined: 16-March 09

Re: Connecting to MySQL database

Posted 28 July 2009 - 01:27 PM

View PostWimpy, on 28 Jul, 2009 - 12:23 PM, said:

To be honest I don't know, but if I remember correctly web forms defaults to post as method? :) No reason to be lazy though! ^^

View PostRPGonzo, on 28 Jul, 2009 - 08:30 PM, said:

I just noticed in your original form your going to have to make a change for $_POST to work ...

<form action="Check.php">



use instead ..
<form action="Check.php" method="post">



That way the server knows to look for a POST not a GET


9 out of 10 times i have had NO issue but i did hit one server that was running PHP in safe mode and it wouldn't work right without it .. dont know if it was me or that but i just make it a habit now ...
Was This Post Helpful? 0
  • +
  • -

#12 Wimpy  Icon User is offline

  • R.I.P. ( Really Intelligent Person, right? )
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,038
  • Joined: 02-May 09

Re: Connecting to MySQL database

Posted 28 July 2009 - 01:31 PM

Then there's even a good reason not to be lazy! ^^

View PostRPGonzo, on 28 Jul, 2009 - 10:27 PM, said:

9 out of 10 times i have had NO issue but i did hit one server that was running PHP in safe mode and it wouldn't work right without it .. dont know if it was me or that but i just make it a habit now ...

Was This Post Helpful? 0
  • +
  • -

#13 bammo10  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 23-July 09

Re: Connecting to MySQL database

Posted 29 July 2009 - 01:13 AM

View PostRPGonzo, on 28 Jul, 2009 - 10:54 AM, said:

I just copied and pasted your code exactly as you have it and it works ...

If you see the value in the URL that means its trying to use GET ...

which you CAN use if you like just swap the $_POST for $_GET .. but just personal preference i would rather let the server handle the data not the URL ...

EDIT: i just found this in your FORM ...

<input type="submit" value="Check key"



you didnt close the input ...

<input type="submit" value="Check key"/>



If your using IE i think it will cause the form to not operate right ... FF auto corrects it


Hey RPGonzo, can you please tell me on which server you pasted the files on? (I am using my own IP)
Chris

EDIT: Closing the input helped!
But it still doesn't work, now I get this error.
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'table WHERE key='lolz'' at line 1

Notice that $key isn't blank anymore.

BTW do i have to put in my own table name here?
$query = "SELECT * FROM table WHERE keys='$key'";

This post has been edited by bammo10: 29 July 2009 - 01:20 AM

Was This Post Helpful? 0
  • +
  • -

#14 RPGonzo  Icon User is offline

  • // Note to self: hmphh .... I forgot
  • member icon

Reputation: 151
  • View blog
  • Posts: 954
  • Joined: 16-March 09

Re: Connecting to MySQL database

Posted 29 July 2009 - 06:12 AM

Yes your going to want to replace "table" with the name of your table your inserting data to ...

That error is happening cause if you have the column named "key" or "keys" MySQL has those names are keywords so you cant use them as column names ... as well as you cannot use "table" as a table name

I tested this using a table names regkeys and a column named regkey and it works fine ... this is the exact code i tested ...

<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
if (isset($_POST['Key']) && $_POST['Key'] != "") {
$key = $_POST['Key'];
} else {
echo "The key to search for was not set!";
die;
}
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die (mysql_error());

$dbname = 'test'; // my database was named test i would not use "keys" as a name here either
mysql_select_db($dbname) or die (mysql_error());
$query = "SELECT * FROM regkeys WHERE regkey='$key'";
$result = mysql_query($query) or die (mysql_error());
$theCount = mysql_num_rows($result);

if ($theCount == 1) {
echo "Your key is good";
} else {
echo "Your key is not good";
}
?>


Was This Post Helpful? 0
  • +
  • -

#15 bammo10  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 23-July 09

Re: Connecting to MySQL database

Posted 29 July 2009 - 06:37 AM

View PostRPGonzo, on 29 Jul, 2009 - 05:12 AM, said:

Yes your going to want to replace "table" with the name of your table your inserting data to ...

That error is happening cause if you have the column named "key" or "keys" MySQL has those names are keywords so you cant use them as column names ... as well as you cannot use "table" as a table name

I tested this using a table names regkeys and a column named regkey and it works fine ... this is the exact code i tested ...

<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
if (isset($_POST['Key']) && $_POST['Key'] != "") {
$key = $_POST['Key'];
} else {
echo "The key to search for was not set!";
die;
}
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die (mysql_error());

$dbname = 'test'; // my database was named test i would not use "keys" as a name here either
mysql_select_db($dbname) or die (mysql_error());
$query = "SELECT * FROM regkeys WHERE regkey='$key'";
$result = mysql_query($query) or die (mysql_error());
$theCount = mysql_num_rows($result);

if ($theCount == 1) {
echo "Your key is good";
} else {
echo "Your key is not good";
}
?>


OMG RPGonzo you are the best!!
You just solved it by pasting your code!!
I am really thankfull, but is there a way to check if the cell next to it is empty?
If it is, change it to 1? (that means that it is used).
If not, it must say it is already used once before.
MANY MANY THANKS TO RPGONZO FOR SOLVING MY PROBLEM!!!!:P
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2