PHP如何把学生信息资料添加到数据库
首先打开Dreamweaver CS5,设计两个表单,如:1.PHP,2PHP,最后把表单信息添加到数据库。
新生录入的表单页面;新建了名为1。PHP的表单,其中包括文本输入框(学号、姓名、栋、房间、床号、家庭地址、联系手机和家庭电话)、列表/菜单组件(年级、班级、专业、年、月、日)、单选按钮(性别)以及两个按钮(一个是提交submit按钮,另一个是重置reset按钮)。本页还对学号与姓名两个输入框进行了javascript判断,判断两者不能为空!
2.php:获取表单form1中的各个输入项内容,并写入数据表student中,若成功则返回“记录已经成功添加,1秒后返回继续录入新生信息……”,否则提示“添加新数据出错!
打开Dreamweaver CS5;新建文件;
输入如下代码,其中加粗的代码为JavaScript验证语句与PHP语句:
function valid_form(theForm)
{
if((theForm.stu_id.value=="")||(theForm.stu_name.value=""))
{
alert("出错原因:\n学生学号为空!\n学生姓名为空!");
theForm.stu_id.focus();
return false;
}
}
第二步: 创建表单处理文件ch14-5-1.php
创建新文件,在Dreamweaver CS3编辑区,输入如下代码:
//获取表单信息
$id=htmlspecialchars($_POST["stu_id"]);
$name=htmlspecialchars($_POST["name"]);
$class=htmlspecialchars($_POST["class"]);
$classid=htmlspecialchars($_POST["classid"]);
$major=htmlspecialchars($_POST["major"]);
$room=htmlspecialchars($_POST["building"])."栋".htmlspecialchars($_POST["room"])."房间";
$bedid=htmlspecialchars($_POST["bed"]);
$sex=htmlspecialchars($_POST["sex"]);
$birthday=trim(htmlspecialchars($_POST["year"]))."-".trim(htmlspecialchars($_POST["month"]))."-".trim(htmlspecialchars($_POST["day"]));
$address=htmlspecialchars($_POST["address"]);
$tel=htmlspecialchars($_POST["tel"]);
$tel2=htmlspecialchars($_POST["tel2"]);
//获取为空的数据时做相应的处理
if($room=="")
$room="未安排";
if($bedid=="")
$bedid="未安排";
if($address=="")
$address="暂缺";
if($tel=="")
$tel="暂缺";
if($tel2=="")
$tel2="暂缺";
//引用公用文件,连接服务器,选择数据库
require "config.inc.php";
//发送SQL请求,插入新数据
$sql="insert into $table_name(stu_id,name,classname,classid,major,room,bedid,sex,birthday,address,telephone,hometel) values('$id','$name','$class','$classid','$major','$room','$bedid','$sex','$birthday','$address','$tel','$tel2')";
//发送SQL请求
mysql_query($sql) or die("添加新数据时出错".mysql_error());
//若成功执行插入语句,则执行下列操作:1秒后刷新跳转
echo "
";echo "";
echo "";
echo "
记录已经成功添加,1秒后返回继续录入新生信息……";echo "";
?>
第三步:保存文件并调试运行
点击“文件”-“保存”,将两个文件分别保存为11.PHP,12.PHP。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...
