PHP基础

一、基本概念。

<?php
	echo 'Hello world!';
?>

PHP以<?php开始以?>结束,所有代码都封装在中间。
还可以是脚本风格:

<script language="php">
	echo 'Hello world!';
</script>

短标记风格:(需要将php.ini文件中的short_open_tag改成on)

<?
	echo 'Hello world!';
?>

ASP风格:(需要将php.ini文件中的asp_tag改成on)

<%
	echo 'Hello world!';
%>

每局代码以结尾。
将PHP写的代码放在Apache的根目录中,打开http://localhost.com查看。
在这里插入图片描述
PHP代码也可以镶嵌在HTML中。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>php</title>
</head>

<body>
<?php
	echo 'Hello world!';
?>
</body>
</html>

将上段代码保存为.php格式,解析器会自动把他翻译成HTML文本。

PHP的注释风格有三种。
c/c++风格://这是一个注释或者/*这是一个注释*/
shell风格:#这是一个注释

基础知识的编程语言基本大同小异的,不再细记了。

二、PHP与数据库交互

1.链接数据库

  <?php
    $hostname='localhost';			//主机名
    	$user='root';					//用户名
    	$password='password';		//密码
    	
    	//连接数据库
    	$conn=mysqli_connect($hostname,$user,$password);
    	if(!$conn){
    		die("数据库连接失败!");
    	}
    	
    	//选择数据库
    	mysqli_select_db($conn,'un1')
    		or die("数据库选择失败!");
    		
    	//告诉数据库我们使用的是utf8字码集
    	mysqli_query($conn,"set names utf8");
 ?>

2.展示数据表信息
数据库表结构
在这里插入图片描述

<?php
	$hostname='localhost';			//主机名
	$user='root';					//用户名
	$password='1982433960..';		//密码
	
	//连接数据库
	$conn=mysqli_connect($hostname,$user,$password);
	if(!$conn){
		die("数据库连接失败!");
	}
	
	//选择数据库
	mysqli_select_db($conn,'un1')
		or die("数据库选择失败!");
		
	//告诉数据库我们使用的是utf8字码集
	mysqli_query($conn,"set names utf8");
	
	//MySQL查询语句
	$sql="select * from countryinformation order by countryid asc";
	//执行MySQL查询语句
	$result=mysqli_query($conn,$sql);
	if(!$result){
		die("查询失败!");
	}
	
	echo '<h2 align="center">模拟联合国成员信息</h2>';
	//制表HTML+PHP
	//EOF标记结束需要独立一行且前后不能空格(此处用TR代替EOF)
	echo '<table border="2" align="center" width="90%">';
	echo <<<TR
	<tr>
		<td>序号</td>
		<td>国家</td>
		<td>加入时间</td>
		<td>所在大洲</td>
		<td>国家领导人</td>
		<td>国土面积</td>
		<td>首都</td>
	</td>
TR;
	
	//循环将表列出
	while($row=mysqli_fetch_array($result)){
		echo<<<TR
		<tr>
			<td>{$row['countryid']}</td>
			<td>{$row['membercountry']}</td>
			<td>{$row['jointime']}</td>
			<td>{$row['countrycontinent']}</td>
			<td>{$row['countryleader']}</td>
			<td>{$row['countryarea']}</td>
			<td>{$row['captial']}</td>
		</tr>
TR;
	}
	echo '</table>';
	
	//释放查询资源
	mysqli_free_result($result);
	//关闭数据库连接
	mysqli_close($conn);
?>

展示效果
在这里插入图片描述
3.修改删除表信息

<?php
	$hostname='localhost';			//主机名
	$user='root';					//用户名
	$password='1982433960..';		//密码
	
	//连接数据库
	$conn=mysqli_connect($hostname,$user,$password);
	if(!$conn){
		die("数据库连接失败!");
	}
	
	//选择数据库
	mysqli_select_db($conn,'un1')
		or die("数据库选择失败!");
		
	//告诉数据库我们使用的是utf8字码集
	mysqli_query($conn,"set names utf8");
	

	//更新数据
	if(isset($_POST['alter'])){
		$countryid=$_POST['countryid'];
		$membercountry=$_POST['membercountry'];
		$jointime=$_POST['jointime'];
		$countrycontinent=$_POST['countrycontinent'];
		$countryleader=$_POST['countryleader'];
		$countryarea=$_POST['countryarea'];
		$captial=$_POST['captial'];
		
		//更新语句
		$sql=<<<SQL
			update countryinformation
			set membercountry="{$membercountry}",jointime="{$jointime}",
			countrycontinent="{$countrycontinent}",countryleader="{$countryleader}",countryarea="{$countryarea}",
			captial="{$captial}"
			where countryid="{$countryid}"
SQL;
		$change=mysqli_query($conn,$sql);
		if($change){
			echo '<script>alert("成功更新一条记录!");</script>';}
			else echo '<script>alert("更新数据失败!");</script>';
	}
	
	
	
	
	//删除数据
	if(isset($_POST['delete'])){
		$countryid=$_POST['countryid'];
		
		//MySQL删除语句
		$sql=<<<SQL
		delete from countryinformation
		where countryid={$countryid};
SQL;
		$drop=mysqli_query($conn,$sql);
		if($drop){
			echo '<script>alert("成功删除一条记录!");</script>';}
			else echo '<script>alert("删除数据失败!");</script>';
	}
	
	//MySQL查询语句
	$sql="select * from countryinformation order by countryid asc";
	//执行MySQL查询语句
	$result=mysqli_query($conn,$sql);
	if(!$result){
		die("查询失败!");
	}
	
	echo '<h2 align="center">修改删除联合国成员信息</h2>';
	//制表HTML+PHP
	//EOF标记结束需要独立一行且前后不能空格(此处用TR代替EOF)
	echo '<table border="2" align="center" width="100%">';
	echo <<<TR
	<tr>
		<td>序号</td>
		<td>国家</td>
		<td>加入时间</td>
		<td>所在大洲</td>
		<td>国家领导人</td>
		<td>国土面积</td>
		<td>首都</td>
		<td>修改</td>
		<td>删除</td>
	</td>
TR;
	
	//循环将表列出
	while($row=mysqli_fetch_array($result)){
		
		$s1="";$s2="";$s3="";$s4="";$s5="";$s6="";$s7="";
	   if($row['countrycontinent']=="Africa"){
	   	  $s1='selected="selected"';
	   }
	   if($row['countrycontinent']=="Asia"){
	   	  $s2='selected="selected"';
	   }
	   if($row['countrycontinent']=="Europe"){
	   	  $s3='selected="selected"';
	   }
	   if($row['countrycontinent']=="Africa"){
	   	  $s4='selected="selected"';
	   }
	   if($row['countrycontinent']=="North America"){
	   	  $s5='selected="selected"';
	   }
	   if($row['countrycontinent']=="South America"){
	   	  $s6='selected="selected"';
	   }
	   if($row['countrycontinent']=="Antarctica"){
	   	  $s7='selected="selected"';
	   }
		
		
		echo<<<TR
		<form action="{$_SERVER["PHP_SELF"]}" method="post" name="countryinformation">
		<tr>
			<td><input type="hidden" name="countryid" value="{$row['countryid']}">{$row['countryid']}</td>
			<td><input type="text" name="membercountry" maxlength="13" value="{$row['membercountry']}"></td>
			<td><input type="date" name="jointime" value="{$row['jointime']}"></td>
			<td><select name="countrycontinent" value=""> 
				<option {$s1} value="Africa">Africa</option>
				<option {$s2}  value="Asia">Asia</option>
				<option {$s3}  value="Europe">Europe</option>
				<option {$s4}  value="North America">North America</option>
				<option {$s5}  value="Oceania">Oceania</option>
				<option {$s6}  value="South America">South America</option>
				<option {$s7}  value="Antarctica">Antarctica</option>
			</select></td>
			<td><input type="text" name="countryleader" maxlength="20" value="{$row['countryleader']}"></td>
			<td><input type="number" name="countryarea" maxlength="8" value="{$row['countryarea']}"></td>
			<td><input type="text" name="captial" maxlength="7" value="{$row['captial']}"></td>
			<td><input type="submit" name="alter" value="修改"></td>
			<td><input type="submit" name="delete" value="删除"></td>
		</tr>
		</form>
TR;
	}
	echo '</table>';

	//释放查询资源
	mysqli_free_result($result);
	//关闭数据库连接
	mysqli_close($conn);
?>

展示效果
在这里插入图片描述
4.往表中添加信息

<form action="<?=$_SERVER["PHP_SELF"]?>" method="post">
		<table border="2" align="center" width="90%">
			<tr>
				<td>国家</td>
				<td>加入时间</td>
				<td>所在大洲</td>
				<td>国家领导人</td>
				<td>国土面积</td>
				<td>首都</td>
			</tr>
			<tr>
				<td><input type="text" name="membercountry" maxlength="13"></td>
				<td><input type="date" name="jointime"></td>
				<td><select name="countrycontinent">
					<option value="Africa">Africa
					<option value="Asia">Asia
					<option value="Europe">Europe
					<option value="North America">North America
					<option value="Oceania">Oceania
					<option value="South America">South America
					<option value="Antarctica">Antarctica
					</select></td>
				<td><input type="text" name="countryleader" maxlength="20"></td>
				<td><input type="number" name="countryarea" maxlength="8"></td>
				<td><input type="text" name="captial" maxlength="7"></td>
			</tr>
		</table>
		<div padding:8cm/><input type="submit" name="submit" value="提交" />
		<input type="reset" name="reset" value="重置"/>
	</form>
<?php
		if(isset($_POST['submit'])){
			$hostname='localhost';			//主机名
			$user='root';					//用户名
			$password='1982433960..';		//密码
	
			//连接数据库
			$conn=mysqli_connect($hostname,$user,$password);
			if(!$conn){
				die("数据库连接失败!");
			}
			
			//选择数据库
			mysqli_select_db($conn,'un1')
				or die("数据库选择失败!");
			
			//告诉数据库我们使用的是utf8字码集
			mysqli_query($conn,"set names utf8");
			
			$membercountry=$_POST['membercountry'];
			$jointime=$_POST['jointime'];
			$countrycontinent=$_POST['countrycontinent'];
			$countryleader=$_POST['countryleader'];
			$countryarea=$_POST['countryarea'];
			$captial=$_POST['captial'];
			
			//MySQL插入语句
			$sql=<<<SQL
				insert into countryinformation
				values
				(null,"{$membercountry}","{$jointime}","{$countrycontinent}","{$countryleader}",
				"{$countryarea}","{$captial}");
SQL;

			//执行SQL插入语句
			$result=mysqli_query($conn, $sql);
			
			if($result){
			echo '<script>alert("成功插入一条记录!");</script>';}
			else echo '<script>alert("插入数据失败!");</script>';
			
			//关闭数据库连接
			mysqli_close($conn);
		}
	?>

展示效果
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值