I'm trying to populate an html textbox value with the $Firstname, but I keep getting a HTTP 500 Internal Server Error. The insert php part works perfectly fine.
this is my PHP code. The box I'm trying to set is down the bottom.
<?php
$Firstname = $_POST['Fname'];
$Surname = $_POST['Lname'];
$UserName = $_POST['UName'];
$Password = $_POST['Password'];
$Valid = mysql_connect('localhost' , 'root' , 'pword');
if(!$Valid)
{
echo 'Could not connect' . mysql_error();
}
else
{
mysql_select_db('accounts', $Valid) or die('could not select database');
$query = "INSERT INTO `users` (`UserName`, `Password`, `FirstName`, `Surname`) VALUES ('".$UserName."' , '".$Password."' , '".$Firstname."' , '".$Surname."')";
mysql_query($query, $Valid) or die('Error Adding the users details.');
echo "The following user " . $Firstname . " has been added to the database";
}
<html>
<body>
<input type="text" name="UName" size="30" value=<?php echo htmlentities($Firstname); ?>/><br/>
</body>
</html>
?>
Remove the ?>
from the end and put it before the <html>
I dont think your code is having any problem. But you try this one
<input type="text" name="UName" size="30" value="<?php echo $Firstname; ?>" />
You probably have an error in your code somewhere. I can't seem to find it in your example code.
By the way, a good practice for echoing a single value in case like this, can be:
<input type="text" name="UName" size="30" value="<?=$Firstname?>" />
in addition to the previous answers, i would advise you to should make sure that you set error_reporting to E_ALL and display_errors to ON in your dev environment. this will help you to get more verbose error messages than a simple HTTP 500.
cheers :)
The first thing i noticed is this,
You forgot to enclosed your php injection in "
<input type="text" name="UName" size="30" value="<?php echo htmlentities($Firstname); ?>"/><br/>
instead of:
...
echo "The following user " . $Firstname . " has been added to the database";
}
<html>
<body>
....
?>
try:
...
echo "The following user " . $Firstname . " has been added to the database";
}
?> <!-- add ?> -->
<html>
<body>
....
(delete ?>)
Error 500 is from Apache server, it doesn't concern PHP. I think you need to make changes in your htaccess, if you have any!
Try restarting your server, and see the result, or better even check apache error log in /var/log/
one site that has been invaluable for me is: http://phpcodechecker.com/
you can paste in a line or an entire file and click the analyze button. it will tell you what the PHP parser doesn't like including some warnings. Very handy for finding missing ')' and similar errors that don't pop out.