表单值不能输入mysql,但可以连接mysql (PHP)


the form value can't input to mysql, but can connect mysql (PHP)

我想diy一个php测试,当你有完整的成绩,你可以输入你的信息到我的数据库,但当我已经填写了表格,并把sumbit。有这个错误~

注意:未定义的索引:firstname在C:'xampp'htdocs'record.php第19行

注意:未定义的索引:姓氏在C:'xampp'htdocs'record.php第19行

注意:未定义的索引:年龄在C:'xampp'htdocs'record.php第19行mysql_connect.inc.php

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php
//資料庫設定
//資料庫位置
$db_server = "localhost";
//資料庫名稱
$db_name = "quiz";
//資料庫管理者帳號
$db_user = "root";
//資料庫管理者密碼
$db_passwd = "123456";
//對資料庫連線
if(!@mysql_connect($db_server, $db_user, $db_passwd))
        die("can't connect mysql");
//資料庫連線採UTF8
mysql_query("SET NAMES utf8");
//選擇資料庫
if(!@mysql_select_db($db_name))
        die("can't connect db");
?>  

record.php

<?php  ?>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php
session_start();
include("mysql_connect.inc.php");
$con = mysql_connect("localhost","root","123456");

mysql_query("INSERT INTO Persons (FirstName, LastName, Age) 
VALUES ('Peter', 'Griffin', '35')");
mysql_query("INSERT INTO Persons (FirstName, LastName, Age) 
VALUES ('Glenn', 'Quagmire', '33')");
$sql="INSERT INTO Persons (FirstName, LastName, Age)
VALUES
('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";
 mysql_close($con)
?>

process.php

<html>
<head>
<style type="text/css">
#wrapper {
    width:950px;
     height:auto;
     padding: 13px;
     margin-right:auto;
     margin-left:auto;
     background-color:#fff;
}
</style>
</head>
<?php 
    $fid = $_GET['id'];
?>
<body bgcolor="#e1e1e1">
<div id="wrapper">
<center><font face="Andalus" size="5">Your Score</font></center>
<br />
<br />
<?php
    $answer1= $_POST['answerOne'];
    $answer2= $_POST['answerTwo'];
    $answer3= $_POST['answerThree'];
    $score = 0;
    if ($answer1 == "A"){$score++;}
    if ($answer2 == "B"){$score++;}
    if ($answer3 == "C"){$score++;}
    echo "<center><font face='Berlin Sans FB' size='8'>Your Score is <br> $score/3</font></center>";
?>
<br>
<br>
<Center>
<?php   
  if ($score == 3)
    { 
      echo " 
      <form action='record.php' method='post' >
       ID: <input type='text' id='firstname'/><br>
       Phone: <input type='text' id='lastname'/><br>
       E-mail: <input type='text' id='age'/><br><br>
       <input type='submit' value='Submit Data' />
      </form>  ";
    }
?>
</Center>

</div><!--- end of wrapper div --->
</body>
</html>

您的输入字段也需要有一个"name"属性

您忘记引用数组键。

$_POST['firstname'] ...
  • 首先,('$_POST[firstname]','$_POST[lastname]','$_POST[age]')应该是("$_POST[firstname]","$_POST[lastname]","$_POST[age]"),"而不是"可以解析内部变量。否则,实际上就是插入$....

  • 其次,正如Vidario所说数组索引需要引号。意味着$_POST[firstname]应该变成$_POST['firstname']$_POST["firstname"]。因为我们已经在变量周围设置了",所以现在让我们使用"作为索引。

那么现在你有:

`("$_POST['firstname']","$_POST['lastname']","$_POST['age']")`
  • 第三,如Danny所说,在HTML中需要给输入一个名称,以便在PHP中使用。只有自我是不够的。所以<input type="..." id="blah" name="blah" />

替换为

$sql="INSERT INTO Persons (FirstName, LastName, Age)
VALUES
('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";
与这个:

$sql="INSERT INTO Persons (FirstName, LastName, Age)
VALUES
('$_POST['firstname']','$_POST['lastname']','$_POST['age']')";

和添加名称属性到您的表单,像这样:

<?php   
if ($score == 3)
{ 
  echo " 
  <form action=''record.php'' method=''post'' >
   ID: <input type=''text'' name=''firstname'' id=''firstname''><br>
   Phone: <input type=''text'' name=''lastname'' id=''lastname''><br>
   E-mail: <input type=''text'' name=''age'' id=''age''><br><br>
   <input type=''submit'' value=''Submit Data''>
  </form>  ";
}
?>