我尝试使用POST方法将Value从一个页面发送到另一个页面,其中数据库的名称是“firstdb”,表格名为“TAB1”。 这是我的代码:
名为“addStud.php”的第一页。代码是:
<html>
<body>
<form action= "addStud_D.php" method="POST">
<center>
<fieldset>
Last Name : <input type = 'text' name ='LName'>
</br> </br>
First Name : <input type = 'text' name = 'FName'>
</br></br>
Date of birth : <input type = 'text' name = 'dat'>
</br></br>
<input type='submit' value='OK'>
</fieldset>
</center>
</form>
</body >
</html>
接收名为“addStud_D.php”的数据的第二页。代码是:
<html>
<body>
<?php
$V1 = $_POST['LName'];
$V2 = $_POST['FName'];
$V3 = $_POST['dat'];
$db=mysql_connect('127.0.0.1','root','','firstdb')
or die('error connecting to MySQL server.');
mysql_select_db('firstdb');
mysql_query("INSERT INTO `firstdb`.`TAB1` (`ID`, `Last_Name`, `First_Name`,`date`)
VALUES (NULL,'$V1','$V2', '$V3')");
?>
</body>
</html>
静止。这个方法不起作用,这是错误:
Notice: Undefined index: LName in D:\Computer Scince\Web\EasyPHP-5.3.3.1\www\APP04\addStud_D.php on line 6
Notice: Undefined index: FName in D:\Computer Scince\Web\EasyPHP-5.3.3.1\www\APP04\addStud_D.php on line 7
Notice: Undefined index: dat in D:\Computer Scince\Web\EasyPHP-5.3.3.1\www\APP04\addStud_D.php on line 8
答案 0 :(得分:0)
更改方法的拼写=&#34; POST&#34; to method =&#34; POST&#34;,你的代码的其余部分也存在一些问题,因为你应该阅读有关准备好的语句,这些语句在处理用户输入时是有效的。
参考:tutorial
答案 1 :(得分:0)
在第一页中,您应将'methode'更改为'method'。
第二页:
<html>
<body>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "firstdb";
$conn = new mysqli($servername, $username, $password, $dbname);
$V1 = isset($_POST['LName']) ? mysqli_real_escape_string($conn, $_POST['LName']) : '';
$V2 = isset($_POST['FName']) ? mysqli_real_escape_string($conn, $_POST['FName']) : '';
$V3 = isset($_POST['dat']) ? mysqli_real_escape_string($conn, $_POST['dat']) : '';
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql
= "INSERT INTO firstdb (id, last_name, first_name, date)
VALUES ('', '" . $V1 . "', '" . $V2 . "', '" . $V3 . "')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
答案 2 :(得分:-1)
修复addStud_D.php
档案
<html>
<body>
<?php
$V1 = (isset($_POST['LName'])?$_POST['LName']:'';
$V2 = (isset($_POST['FName'])?$_POST['FName']:'';
$V3 = (isset($_POST['dat'])?$_POST['dat']:'';
$db=mysqli_connect('127.0.0.1','root','','firstdb')
or die('error connecting to MySQL server.');
mysql_select_db('firstdb');
mysql_query("INSERT INTO `firstdb`.`TAB1` (`ID`, `Last_Name`, `First_Name`,`date`)
VALUES (NULL,'$V1','$V2', '$V3')");
?>
</body>
</html>
您可以验证表格
<html>
<body>
<?php
$V1 = (isset($_POST['LName'])?$_POST['LName']:'';
$V2 = (isset($_POST['FName'])?$_POST['FName']:'';
$V3 = (isset($_POST['dat'])?$_POST['dat']:'';
if(!empty($V1) && !empty($V2) && !empty($V3)){
$db=mysqli_connect('127.0.0.1','root','','firstdb') or die('error connecting to MySQL server.');
mysqli_query("INSERT INTO `firstdb`.`TAB1` (`ID`, `Last_Name`, `First_Name`,`date`) VALUES (NULL,'$V1','$V2', '$V3')");
}else{
echo "Error";
}
?>
</body>
</html>