Jump to content

PHP form


blakesmoore

Recommended Posts

Hi,

 

I'm trying to create a form which will test database connection but I'm getting an error. I'm new to php so any help would be appreciated.

 

form.php

<html>
<form action="dbconnection.php" method="post"> 
<table width="400" border="0" cellspacing="2" cellpadding="0"> 
<tr> 
<td width="29%" class="bodytext">Host Name</td> 
<td width="71%"><input name="server" type="text" id="name" size="32"></td> 
</tr> 
<tr> 
<td class="bodytext">Username</td> 
<td><input name="username" type="text" id="email" size="32"></td> 
</tr> 
<tr> 
<td class="bodytext">Password</td> 
<td><input name="password" type="text" id="email" size="32"></td> 
</tr> 
<tr> 
<td class="bodytext"> </td> 
<td align="left" valign="top"><input type="submit" name="Submit" value="Send"></td> 
</tr> 
</table> 
</form>

 

dbconnection.php

<?php
$server = $_POST['server'];
$username = $_POST['username'];
$password = $_POST['password'];
$link = mysql_connect ('$server', '$username', '$password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>

 

When submitting the form you get the below error

 

Warning: mysql_connect() [function.mysql-connect]: Unknown MySQL server host '$server' (1) in /website/public_html/dbconnection.php on line 6

Could not connect: Unknown MySQL server host '$server' (1)

 

If you connect successfully if should echo 'Connected Successfully'

If you cannot connect if should say 'Could not connect'

 

any advice would be greatly appreciated.

 

Thanks

Link to comment
Share on other sites

Thanks again, have added the @ before the mysql_connect

 

The connection error message is shorter, but would it be possible to literally just make it say 'Could not connect?'

 

I don't really understand the timing bit, would it be possible to explain in simpler terms? Sorry about this but i'm only just learning :(

 

Thank you for your help!

Link to comment
Share on other sites

Which part of the code do you think would produce an error message from MySQL? Just remove that part . . .

 

I linked to the microtime() function manual entry above, so that will have everything you need to know about microtime() itself. Just assign it to a variable before connecting, then find the difference between the ending microtime(), and the stored starting microtime() by subtracting, then echo the result.

 

$start = microtime(TRUE);

$link = mysql_connect('blah', 'blah', 'blah');

$end = microtime(TRUE);
$elapsed = $end - $start;

echo "Elapsed time: $elapsed seconds.

 

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.