我正在尝试使用以下html和php代码将某些数据输入数据库...但显然无论我在运行代码时输入文本框,在数据库中输入为“1”...我该怎么做才能解决这个问题?
<html>
<head>
<title>Project Details</title>
</head>
<body>
<?php include 'project.html';
$pn=isset($_POST['name']) ? $_POST['ProjectName'] : '';
$tn=isset($_POST['tname']) ? $_POST['TaskName'] : '';
$dsc=isset($_POST['DESC']) ? $_POST['Proj_desc'] : '';
$serverName = "Swagatha-PC";
if($_SERVER['REQUEST_METHOD']=="POST")
{
$connectionInfo = array( "Database"=>"Testing");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn )
{
echo "Connection established.<br />";
}
else
{
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
$query="Insert Into dbo.Project (ProjectName,TaskName,Proj_desc) values ('$pn' ,'$tn' , '$dsc')";
$stmt=sqlsrv_query($conn,$query);
if($stmt==false)
{
echo "Error in adding Info!! Reload Page an try again!!<br/>";
die( print_r( sqlsrv_errors(), true));
}
else
{
echo " Record Added!!";
}
sqlsrv_close($conn);
}
?>
</body>
</html>
html代码是
<html>
<head>
<title>Project Details</title>
</head>
<body>
<form method = "post" action = "http://localhost/project.php">
<table>
<tr>
<td>Project Name:</td>
<td><input type = "text" name = "name"></td>
</tr>
<tr>
<td>Task Name:</td>
<td><input type = "text" name = "tname"></td>
</tr>
<tr>
<td>Project description:</td>
<td><textarea name = "DESC" rows = "5" cols = "40"></textarea></td>
</tr>
<tr>
<td><input type = "submit" name = "submit" value = "Submit" onclick="http://localhost/project.php"></td>
</tr>
</table>
</form>
</body>
</html>
我输入的数据在数据库中输入如下: Output error
如何修复此错误????
答案 0 :(得分:1)
请更改
$pn=isset($_POST['ProjectName']);
$tn=isset($_POST['TaskName']);
$dsc=isset($_POST['Proj_desc']);
到
$pn=isset($_POST['name']) ? $_POST['name'] : '';
$tn=isset($_POST['tname']) ? $_POST['tname'] : '';
$dsc=isset($_POST['DESC']) ? $_POST['DESC'] : '';