Chanpa Posted October 7, 2011 Share Posted October 7, 2011 Hi, I'm creating a PHP application to handle my SQL server and I've run into a bit of a problem; I have two files atm: mainClass.php and testSite.php My mainClass.php looks like this: class mainClass { private $host = 'localhost'; public function createDb($user,$pass,$dbName) { $con = mysql_connect($host, $user, $pass); if (!$con){ die('Could not connect: '.mysql_error()); } $sql = "CREATE DATABASE `$dbName`;"; if (!mysql_query($sql)){ die('Error 1: '.mysql_error()); } mysql_close(); } } and testSite.php looks like this: <!DOCTYPE HTML> <html lang="en"> <head> <meta charset="UTF-8"> </head> <body> <h1>testSite for my PHP app</h1> <?php function __autoload($className){ require_once "./classes/{$className}.php"; } $test = new mainClass(); ?> <form name='createDb' method='post' action=''> User: <input type='text' name='user'><br> Password: <input type='password' name='pass'><br> dbName: <input type='text' name='dbName'><br> <input type='submit' value='Create DB'> </form> </body> </html> What I'm asking is if it is possible to make the form-action from testSite.php run the createDb function from mainClass.php I have pretty much no idea how to do it but I tried like this: <form name='createDb' method='post' action="<?php $test->createDb($_POST['user'],$_POST['pass'],$_POST['dbName']); ?>"> User: <input type='text' name='user'><br> Password: <input type='password' name='pass'><br> dbName: <input type='text' name='dbName'><br> <input type='submit' value='Log in'> </form> But that just made the whole form disappear so now I'm completely lost, any help greatly appreciated. PS: I'm doing this to get better at PHP so please don't come with advice like "use a framework" or "there already are applications that handles this", I know there is. Quote Link to comment https://forums.phpfreaks.com/topic/248605-how-to-make-a-form-action-run-a-function/ Share on other sites More sharing options...
Chanpa Posted October 7, 2011 Author Share Posted October 7, 2011 I solved it temporarily like this: if (isset($_GET['createDb'])) { $user = mysql_real_escape_string($_POST['user']); $pass = mysql_real_escape_string($_POST['pass']); $dbName = mysql_real_escape_string($_POST['dbName']); $test->createDb($user,$pass,$dbName); } else { echo " <form name='formCreateDb' method='post' action='?createDb'> dbName: <input type='text' name='dbName'><br> User: <input type='text' name='user'><br> Password: <input type='password' name='pass'><br> <input type='submit' value='Create DB'><br> </form>"; } But I think it's kind of ugly and I'm stil wondering if there is another way to do it. Quote Link to comment https://forums.phpfreaks.com/topic/248605-how-to-make-a-form-action-run-a-function/#findComment-1276681 Share on other sites More sharing options...
Chanpa Posted October 7, 2011 Author Share Posted October 7, 2011 Actually the above code didn't work, I got Could not connect: Access denied for user 'www-data'@'localhost' (using password: NO) because of the mysql_real_escape_string. Anyone know why? (It works if I remove mysql_real_escape_string) Quote Link to comment https://forums.phpfreaks.com/topic/248605-how-to-make-a-form-action-run-a-function/#findComment-1276684 Share on other sites More sharing options...
Buddski Posted October 7, 2011 Share Posted October 7, 2011 Your original code would never work. PHP isnt that type of language. Your "temp" fix is how it has to be done (among variants). The mysql_real_escape_string function requires that a connection to a mysql server is already present. You will have to move it into your class function createDb. Also, inside your createDb function $host in the mysql_connect() will be undefined, you will have to reference it using $this $con = mysql_connect($this->host, $user, $pass); As a side note, your connection username and password really should be static, not a user input (just my opinion) Quote Link to comment https://forums.phpfreaks.com/topic/248605-how-to-make-a-form-action-run-a-function/#findComment-1276688 Share on other sites More sharing options...
Chanpa Posted October 7, 2011 Author Share Posted October 7, 2011 Your original code would never work. PHP isnt that type of language. Your "temp" fix is how it has to be done (among variants). The mysql_real_escape_string function requires that a connection to a mysql server is already present. You will have to move it into your class function createDb. Also, inside your createDb function $host in the mysql_connect() will be undefined, you will have to reference it using $this $con = mysql_connect($this->host, $user, $pass); As a side note, your connection username and password really should be static, not a user input (just my opinion) Thanks for the reply. I made the changes you said. And the reason I don't have static login is because I plan on experimenting with multiple users with different access-levels. Thanks for the help, this topic can be closed/solved. Quote Link to comment https://forums.phpfreaks.com/topic/248605-how-to-make-a-form-action-run-a-function/#findComment-1276690 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.