Home Backend Development PHP Tutorial PHP如何把学生信息资料添加到数据库

PHP如何把学生信息资料添加到数据库

Jun 20, 2016 pm 12:46 PM

  首先打开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;

}

}

 

学生信息管理系统-新生录入

 

   

     

     

     

   

   

     

     

     

   

   

     

     

     

   

   

     

     

     

   

   

     

     

     

   

       

     

     

     

   

   

     

     

     

   

       

     

     

     

   

   

     

     

     

   

     

     

     

     

   

   

     

     

     

   

   

     

     

     

   

   

     

     

     

   

 

     

      输入学号:

     

     

     

       

     

     

*不能为空

输入姓名:

     

       

     

     

     

*不能为空

     

     

所在年级:

     

     

       

    for($i=2007; $i

  //循环显示2007-2012间的数字做为列表的选项值

{

echo "";

}

?>

       

     

     

 

     

所在班级:

     

     

       

          for($i=1; $i

 //循环显示1-6间的数字做为列表的选项

{

echo "";

}

?>

       

     

     

 

     

选择专业:

     

     

       

         

         

         

         

         

         

         

       

     

     

 

     

选择宿舍:

     

     

         

         

          房间

         

 

     

选择床号:

     

     

       

     

     

 

     

性  别:

     

     

       

     

      男

     

     

      女

     

 

     

出生日期

     

     

       

        for($i=1982; $i

 //循环显示1982-2008间的数字做为列表的选项

{

echo "";

}

?>

       

      年

     

      for($i=1; $i

//循环显示1-12间的数字做为列表的选项

{

echo "";

}

?>

     

      月

     

      for($i=1; $i

 //循环显示1-31间的数字做为列表的选项

{

echo "";

}

?>

     

      日

     

 

     

家庭地址:

     

     

       

     

     

 

     

联系手机:

     

     

       

     

     

自己的手机号码

     

家庭电话

     

       

     

     

家庭联系电话

 

     

       

 

 

  查看学生信息

 

 

第二步: 创建表单处理文件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。

打开11.PHP,按f12对网页进行调试。

如图;

首先来测试其中的JavaScript是否正确,在不输入任何数据的情况下,点击“录入系统”按钮,会弹出如图的警告框。

点击“录入系统”按钮,若添加数据到数据库中成功,则显示如图14-22所示的信息,否则显示“添加新数据时出错”以及错误信息:

点击下方的“查看学生信息”,我们可以进入11.PHP,会看到在数据表student的最后添加了一条新记录,图:

end

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

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,

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

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.

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

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? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

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 permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

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...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

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.

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

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�...

See all articles