Jump to content

How to make a form-action run a function?


Chanpa

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.