php表单数据输入代码 php编写表单( 二 )


--
!--数据库用PDO调用方法
form method="post" action="write2db_pdo.php"
--
labelFirst Name/label
input type="text" name="first_name" /
br /
labelLast Name/label
input type="text" name="last_name" /
br /
labeldepartment/label
input type="text" name="department" /
br /
labelEmail/label
input type="text" name="email" /
br /
input type="submit" value="https://www.04ip.com/post/Add students"
/form
/body
/html
------------------------------
?php
//拷贝命名为write2db.php,数据库用mysqli 面向过程调用方法
//print_r($_POST);
// create a variable
$first_name=$_POST['first_name'];
$last_name=$_POST['last_name'];
$department=$_POST['department'];
$email=$_POST['email'];
//调试用
echo "Your input: ";
echo $first_name;
echo 'br /';
echo $last_name;
echo 'br /';
echo $department;
echo 'br /';
echo $email;
echo 'br /';
$servername = "localhost";
//Your database username and password
//$username = "username";
//$password = "password";
$username = "tester";
$password = "testerPassword";
//your database name
$dbname = "test";
$tablename ="student";
// Create connection
$connect = mysqli_connect($servername, $username, $password, $dbname);
if (!$connect) {
die("Connection failed: " . mysqli_connect_error());
}
//Execute the query
$sql="INSERT INTO $tablename (first_name,last_name,department,email)
VALUES('$first_name','$last_name','$department','$email')";
if (mysqli_query($connect, $sql)) {
echo "Hooray! New record is inserted to database successfully. Please check database.";
} else {
echo "Error: " . $sql . "br /" . mysqli_error($connect);
}
mysqli_close($connect);
?
?php
//拷贝命名为write2db_sqlio.php,数据库用mysqli 面向对象调用方法
//print_r($_POST);
// create a variable
$first_name=$_POST['first_name'];
$last_name=$_POST['last_name'];
$department=$_POST['department'];
$email=$_POST['email'];
//调试用
echo "Your input: ";
echo $first_name;
echo 'br /';
echo $last_name;
echo 'br /';
echo $department;
echo 'br /';
echo $email;
echo 'br /';
$servername = "localhost";
//Your database username and password
//$username = "username";
//$password = "password";
$username = "tester";
$password = "testerPassword";
//database name
$dbname = "test";
$tablename ="student";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn-connect_error) {
die("Connection failed: " . $conn-connect_error);
}
$sql="INSERT INTO $tablename (first_name,last_name,department,email)
VALUES('$first_name','$last_name','$department','$email')";
if ($conn-query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "br" . $conn-error;
}
$conn-close();
?
?php
//拷贝为文件write2db_pdo.php,数据库用PDO调用方法
//print_r($_POST);
a variable
$first_name=$_POST['first_name'];
$last_name=$_POST['last_name'];
$department=$_POST['department'];
$email=$_POST['email'];
//调试用
echo "Your input: ";
echo $first_name;
echo 'br /';
echo $last_name;
echo 'br /';
echo $department;
echo 'br /';
echo $email;
echo 'br /';
$servername = "localhost";
//Your database username and password
//$username = "username";
//$password = "password";
$username = "tester";
$password = "testerPassword";

推荐阅读