<?php
$dbserver="localhost";
$username="root";
$pass="root";
$link=mysql_connect("$dbserver","$username","$pass");
if(!$link){die('DB Connection Failed'.mysql_error()); }
echo('connected');
$Name=$_POST["namei"];
$ID=$_POST["pid"];
$Address=$_POST["address"];
$Phone=$_POST["phone"];
$query="INSERT INTO contact(Name,ID,Address,Phone) VALUES('".$Name."',".$ID.",'".$Address."',".$Phone.");";
echo($query);
?>
The code above is used by me to connect to a mysql db, i'm posting the contents to this page from an html page. As i checked there is no problem with POST. but on click of submit it gives me an error '500 Internal Server Error'.
I'm using Apache 2.2 Server, and 开发者_StackOverflow社区mysql 5.5.
Can any one tell what is my mistake?
Thank you
First please run
<?php
echo 'phpversion: ', phpversion(), "<br />\n";
if ( !extension_loaded('mysql') ) {
die('mysql module not available');
}
echo 'mysql_get_client_info: ', mysql_get_client_info(), "<br />\n";
die;
to check whether a) you can run any php script and b) the mysql_* functions are available.
Then try
<?php
echo "start<br />\n";
error_reporting(E_ALL);
ini_set('display_errors', true);
flush();
$dbserver="localhost";
$username="root";
$pass="root";
$link=mysql_connect($dbserver, $username, $pass);
if(!$link) {
die('DB Connection Failed '.mysql_error());
}
echo "connected<br />\n";
if ( !mysql_select_db('dbname here', $link) ) {
die('DB selection failed. '.mysql_error($link));
}
echo "db selected<br />\n";
$Name = mysql_real_escape_string($_POST['namei'], $link);
$ID = mysql_real_escape_string($_POST['pid'], $link);
$Address = mysql_real_escape_string($_POST['address'], $link);
$Phone = mysql_real_escape_string($_POST['phone'], $link);
$query = "
INSERT INTO
contact
(Name,ID,Address,Phone)
VALUES
('$Name', '$ID','$Address','$Phone')
";
echo '<pre>Debug: query=', htmlentities($query), "</pre>\n";
It prints something in any case (echo/flush) and sets error_reporting + display_errors so that error messages are sent to the client (you don't want that in production, don't forget to remove those lines).
I also added the necessary calls to mysql_select_db() and mysql_real_escape_string() (needed as soon as the script really sends the query to the mysql server).
K,
You should add
ini_set('display_errors', 1);
error_reporting(E_ALL);
To the beginning of your script. This will stop the 500 errors and give you the proper error message so you have a better idea of what is going wrong.
It might be the echo($query);
line, you only need echo $query;
without the parentheses.
精彩评论