xenolith05 Posted February 7, 2013 Share Posted February 7, 2013 (edited) 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 February 7, 2013 by xenolith05 Link to comment Share on other sites More sharing options...
jeffman Posted February 7, 2013 Share Posted February 7, 2013 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 More sharing options...
xenolith05 Posted February 7, 2013 Author Share Posted February 7, 2013 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 More sharing options...
jeffman Posted February 7, 2013 Share Posted February 7, 2013 (edited) 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 February 7, 2013 by Deirdre's Dad Link to comment Share on other sites More sharing options...
xenolith05 Posted February 8, 2013 Author Share Posted February 8, 2013 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 More sharing options...
jeffman Posted February 8, 2013 Share Posted February 8, 2013 Oops. You're right. Test for a certain array element, like isset($_POST['host']); FWIW, it's bad practice to ignore errors. Get in the habit of testing for stuff and working around it. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now