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
https://forums.phpfreaks.com/topic/255701-php-form/
Share on other sites

You could suppress the error returned from mysql_connect() by preceding it with a @. To time it, you can store the value of microtime in a variable before the connection call, and then find and echo the difference between it and microtime() after the connection call.

Link to comment
https://forums.phpfreaks.com/topic/255701-php-form/#findComment-1310799
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
https://forums.phpfreaks.com/topic/255701-php-form/#findComment-1310802
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
https://forums.phpfreaks.com/topic/255701-php-form/#findComment-1310813
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.