Database connection to MySQL using PHP

Hi,
I'm having trouble connecting to my MySQL database using PHP.

The following is my simple code to add a user to a table called users in the DB. The table "users" has the two basic columns; "Username" and "Password".
The first page has a basic form, user enters Username and Password and the page clicks through to the next page call add.php

The code that I have for that page is:

<body>
<?
$connection = mysql_pconnect("$host","$username","$database!")
      or die ("Couldn't connect to server.");

$db = mysql_select_db("$database", $connection)
      or die("Couldn't select database.");
?>
            
<?php
$sql = mysql_query("INSERT INTO users (`Username`,`Password`) VALUES('$Username' , '$Password' )") or die (mysql_error());
if(!$sql){
      echo 'ERROR';
} else {
echo 'User added';

User has been added to the DB
}
?>
</body>
blaadomAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gamebitsCommented:
In your conection string ("$host","$username","$database!")
 isn't suppose to be  $password instead of $database?
0
glcumminsCommented:
When using mysql_pconnect(), the paramters should be Hostname, Username, Password.

It looks like you are passing '$database!' as the third parameter, rather than the database password.
0
Aamir SaeedCommented:
you have to enter password here not the database
$connection = mysql_pconnect("$host","$username","$database!")
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

Aamir SaeedCommented:
03.22.2007 at 01:13PM PDT
0
blaadomAuthor Commented:
Sorry my mistake, I had that it the code put when i copied it in here I must of changed $password to $database by accident. I have that in my code, but nothing gets submitted into the db
0
Aamir SaeedCommented:
make sure you are connected with DB
0
glcumminsCommented:
Can you make this modification to your code so we can troubleshoot the SQL statement:

<?php
$query = "INSERT INTO users (`Username`,`Password`) VALUES('$Username' , '$Password' )";
print $query . "<br />";
$sql = mysql_query($query) or die (mysql_error());
if(!$sql){
      echo 'ERROR';
} else {
echo 'User added';

We will then be able to see the exact statement that is being executed.
0
gamebitsCommented:
Do you get any error when you try the sript?
0
glcumminsCommented:
Another minor issue, which may or may not apply here: In your first code block, you are using a short-open tag (<?), and in the second block you are using a normal-open tag (<?php). If your server has short-open tags disabled, the first block is never being executed, so you never connect to the MySQL server. You might try using long-open tags in both places, just to make sure.
0
gamebitsCommented:
Try to set your variables first

Add this above your insert statement.

$username = $_POST['username'];
$password = $_POST['password'];
0
blaadomAuthor Commented:
Ok I've tried all of the above, still nothing going in yet. I don't get any error messages either.
0
blaadomAuthor Commented:
This is what I put in for the table in the DB:

CREATE TABLE `users` (                  
          `Username` varchar(15) NOT NULL,      
          `Password` varchar(15) NOT NULL,      
          PRIMARY KEY  (`Username`)            
        ) ENGINE=InnoDB DEFAULT CHARSET=latin1

Would you like me to post the php code I have again?
0
glcumminsCommented:
What was the output of the 'print $query' statement?
0
glcumminsCommented:
Yes, if you could post the entire script as it now stands, that would be helpful.
0
gamebitsCommented:
Put at the top of your script just after the <?

error_reporting(E_ALL);

I'd like to know if we have a parse error.
0
blaadomAuthor Commented:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<?php
$connection = mysql_pconnect("$GenServer","$root","$password")
      or die ("Couldn't connect to server.");

$db = mysql_select_db("$generator", $connection)
      or die("Couldn't select database.");
?>
<body>
                        
User added

<?php
$username = $_POST['username'];
$password = $_POST['password'];

$sql = mysql_query("INSERT INTO users (`Username`,`Password`) VALUES('$username' , '$password' )";
print $query . "<br />";
$sql = mysql_query($query) or die (mysql_error());
if(!$sql){
      echo 'ERROR';
} else {
echo 'User added';
?>
</body>
</html>

The values "username" and "password" are on the previous page being submitted.
There is no output from the query statement, and even when I change the database or password for example the query dosen't seem to execute. All that shows is User added
0
glcumminsCommented:
$sql = mysql_query("INSERT INTO users (`Username`,`Password`) VALUES('$username' , '$password' )";
print $query . "<br />";
$sql = mysql_query($query) or die (mysql_error());

Should be:

$query = mysql_query("INSERT INTO users (`Username`,`Password`) VALUES('$username' , '$password' )";
print $query . "<br />";
$sql = mysql_query($query) or die (mysql_error());
0
glcumminsCommented:
This tells me that error reporting is turned off, because you should have received a message when you tried to print a non-existant variable ($query). gamebits' suggestion is sound: you should turn on error reporting when troubleshooting a PHP script.
0
glcumminsCommented:
<?php
$connection = mysql_pconnect("$GenServer","$root","$password")
      or die ("Couldn't connect to server.");

$db = mysql_select_db("$generator", $connection)
      or die("Couldn't select database.");
?>

Where are these variables being set: $GenServer, $root, $password, $generator, $connection ?

If you are trying to pass strings, rather than variables, remove the '$' from these.
0
blaadomAuthor Commented:
I didn't have the variables set. I set them above but it still didn't work.
I'm using dreamweaver 8 to edit the files. Is there any error reporting on this do you know?
0
glcumminsCommented:
Error reporting is controlled by the server, not by the editor. You will need to edit your php.ini file, and look for the following entries:

error_reporting
   set this to 'error_reporting = E_ALL'

display_errors = On

Restart your webserver, and these changes will take effect.

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
blaadomAuthor Commented:
Unfortunately I don't have permission to get in to restart the server  :(
The query works in MySQL manually, it's just figuring what could be the reason why it's not submitting / connecting to the DB. I thought I'd just post both pages of code, to see if you could see anything that I'm missing. I've changed the code in add.php to see if that would make any difference.
Page one - the form

 <form id="form1" name="form1" method="post" action="add.php">
                    <table width="100%" height="58%" border="0" cellpadding="0" cellspacing="0">
                  <tr>
                    <td width="20%" height="36">&nbsp;</td>
                    <td width="50%" colspan="2">&nbsp;</td>
                    <td width="30%">&nbsp;</td>
                  </tr>
                  <tr>
                    <td width="20%" height="37">&nbsp;</td>
                    <td width="50%" colspan="2">
                      <label>
                      <div align="right"><strong>Username:
                        </strong>
                        <input name="username" type="text" id="username" maxlength="10" />
                      </div>
                      </label>
                      </td>
                    <td width="30%">&nbsp;</td>
                  </tr>
                  <tr>
                    <td width="20%">&nbsp;</td>
                    <td width="50%" colspan="2">
                      <label>
                      <div align="right"><strong>Password:
                        </strong>
                        <input name="password" type="password" id="password" maxlength="10" />
                      </div>
                      </label>
                      </td>
                    <td width="30%">&nbsp;</td>
                  </tr>
                  <tr>
                    <td width="20%">&nbsp;</td>
                    <td width="50%" colspan="2"><div align="right"></div></td>
                    <td width="30%">&nbsp;</td>
                  </tr>
                  <tr>
                    <td width="20%" height="37">&nbsp;</td>
                    <td width="25%">&nbsp;</td>
                    <td width="25%">
                      <div align="center">
                          <input name="Login" type="submit" id="Login" value="Login" />
                        </div>
                      </td>
                    <td width="30%">&nbsp;</td>
                  </tr>
                  <tr>
                    <td height="73" colspan="4"><p>&nbsp;</p>
                      <p>&nbsp;</p></td>
                    </tr>
                </table>
                  </form>

Page 2 - add.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<?php

mysql_connect("GenServer", "root", "G3nerator!") or die(mysql_error());
mysql_select_db("generator") or die(mysql_error());

$username = $_POST['username'];
$password = $_POST['password'];

mysql_query("INSERT INTO users (Username, Password) VALUES('username', 'password' ) ")
or die(mysql_error());  

echo "Data Inserted!";

?>
Added
</body>
</html>


0
glcumminsCommented:
mysql_query("INSERT INTO users (Username, Password) VALUES('username', 'password' ) ")

Should be:

mysql_query("INSERT INTO users (Username, Password) VALUES('$username', '$password' ) ")

0
glcumminsCommented:
Since you cannot edit the php.ini file or restart the server, you will need to use gamebits' suggestion to turn on error reporting in add.php. At the top of the script, right after '<?php', add:

error_reporting(E_ALL);
ini_set('display_errors', 'On');
0
blaadomAuthor Commented:
Still the same, is there any other way I could check the connection to the DB?
Or make a new table and check login details against whats in the DB. I reckon the query would work, as it's more or less going to be the same.
0
blaadomAuthor Commented:
Thanks for the help guys. I had to wait to get Error reporting turned on and now it's fine.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.