Jump to content

php mysql connection


xenolith05

Recommended Posts

Hi, I'm just starting out with php and mysql.I've successfully created my database, tables, php connection file, and forms to update records so all in all good start.My connection.php looks like this:

<?php$db_host = "localhost";$db_username = "dummy";$db_pass = "dummy1234";$db_use = "dummy";$con = mysql_connect("$db_host","$db_username","$db_pass");if (!$con)   {   die('Could not connect to mysql: ' . mysql_error());   } echo "Connected to mysql";?><br /><?phpmysql_select_db("$db_use", $con);echo "Connected to database: "."$db_use";?>

I'm trying to create a form that submits variables e.g. $db_host = $_POST['host'];$db_username = $_POST['username']; $db_pass = $_POST['pass']; $db_use = $_POST['db']; so that the user can open different databases on different hosts.But when the php form is parsed I receive the undefined index error due to the variables not being set initially (I think!).Can anyone offer any advice?

Edited by xenolith05
Link to comment
Share on other sites

Check your data like this: var_dump($_POST); Just put that at the top of your script and exit. Don't do anything else. If there really is no data there, something's wrong with your HTML document. Show us your form.

Link to comment
Share on other sites

The variables will be empty until the user submits the form.When the form is submitted the connection works ok, it's just the initial loading of the php page that shows the errors as there is no $_POST data.I'm on a different pc at the moment so do not have the form to hand. Thanks

Link to comment
Share on other sites

it's just the initial loading of the php page that shows the errors as there is no $_POST data.
Do you need to be evaluating the post data when the doc first loads? Do you, for example, need to test isset($_POST) and skip a bunch of code if that returns false? EDIT. Forgot to add, it no longer sounds like the form is the issue. Edited by Deirdre's Dad
Link to comment
Share on other sites

Sorry i need to be clearer. The form and connection work ok but php complains about the empty variables when the doc loads. This is not a problem if error reporting is switched off.I'll go away and have another look at isset (still learning!). I thought isset would test as true even if the value is empty but not set as a NULL so I'm not sure how I would use it.I don't need to evaluate when first loaded as the variables should be empty.The errors obviously only show when I set my php to show errors but if turned off everything works as it should so maybe not a problem. Thanks 'Deidre's Dad'!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...