邮件内容的空白输出

时间:2014-02-18 21:34:55

标签: php post echo

当我回复帖子内容或将其插入我的数据库$时,我总是得到空白结果 这是我的代码

PHP

<?php

$host = "mysql.1freehosting.com";
$user = "u249494946_user";
$pass = "aqwaqwaqw";
$db = "u249494946_first";

mysql_connect($host,$user,$pass);
mysql_select_db($db);

/*if(isset($_post["ajout"])) {
echo("error submitting");
}
else {
*/
var_dump(isset($_post['submit']));
$nom = $_post["nom"];
$prenom = $_post["prenom"];
echo $_post[nom];
echo $nom;
$result1=mysql_query("INSERT INTO eleve (id,nom,prenom) VALUES (NULL,'$nom','$prenom')");var_dump($result1);
/*$result2=mysql_query("INSERT INTO eleve(id, nom, prenom) VALUES (null,'y','y')");
if(!$result1){
die('errreur result1 :' . mysql_error());
}
*/
var_dump($_POST);

?>

HTML

<html>
<head>

<title>disaster</title>
</head>
<body>
<div align="center">
<form  id="form" method="post" action="ajout.php">

nom : <input type="text" id="nom" />
prenom : <input type="text" id="prenom" />
<input type="submit" value="ajouter"  id="submit" />

</form>
</div>
</body>
</html>

这是我得到的结果

bool(false)
bool(true)
array(3) {
  ["nom"]=>
  string(7) "example"
  ["prenom"]=>
  string(7) "example"
  ["submit"]=>
  string(7) "ajouter"
}

2 个答案:

答案 0 :(得分:3)

$_POSTsuperglobal,必须为大写。

将您的所有$_post更改为$_POST

另外,正如Alon已经说明的那样,您需要命名输入元素:

<input type="text" id="nom" name="nom" />

归功于Alon。

我还建议您使用mysqli_*prepared statements切换到PDO函数,因为mysql_*函数已弃用,将在以后的PHP版本中删除。


修改

您的某些输入元素未命名:

更改为:

nom : <input type="text" id="nom" name="name" />
prenom : <input type="text" id="prenom" name="prenom" />
<input type="submit" value="ajouter" name="submit" id="submit" />

答案 1 :(得分:2)

应该是echo $_POST["nom"];而不是echo $_post[nom];

编辑: 您需要将名称属性添加到表单输入,如<input type="text" id="nom" name="nom" />