和我一起学PHP--第一部分

index.php页面(数据的插入功能)
<!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>
	<script type="text/javascript" language="javascript">
		function Check(){
			var value= document.getElementsByName('name')[0].value;
			if(value==''){
				document.getElementById('tip').innerText='名号不能为空!';
				return;
				}
			}
	</script>
</head>
<body style="margin:0;padding:0;">
<div style="margin:0 auto; width:200px;height:500px;">
<form action="doinsert.php" method="post">

    给自己起一个名号:<br/>
    <input type="text" name="name" size="10" maxlength="10" /><div style="color: red;" id="tip" ></div>

<p>
    <input type="submit" name="submit" value="Submit!" onclick="Check();"/>
</p>
</form>
<table>
	<tr>
		<td>编号</td>
		<td>姓名</td>
	</tr>
	<?php
	error_reporting(0); 
	$conn=mysql_connect('localhost:3306','root','password');//此处是连接mysql数据库
	//echo $conn;
	mysql_select_db('database',$conn);
	$sql='SELECT  * FROM table';
	$query=mysql_query($sql);
	while ($row=mysql_fetch_array($query)){
		$id=$row[id];
		$name=$row[name];
		?>
	<tr>
		<td><?php echo $id;?></td>
		<td><?php echo $name;?></td>
	</tr>
			<?php
	}
?>
</table>
</div>
</body> 
</html>
doinsert.php,用户插入数据的实现操作
<?php
error_reporting(0); 
$name = $_POST['name'];
if ($name!=NULL)
{
$linkID=@mysql_connect("localhost:3306","root","password");
if(!$linkID){
echo "没连接上";
}
$ss=@mysql_select_db("database");
if(!$ss){
   echo "没找到数据库";
}


$query = "INSERT INTO student (name) values('$name')";


$result = mysql_query($query);


if($result) echo "<p>student successfuly insert!</p>";
else echo "<p>There was a problem inserting the student!</p>";


mysql_close();
}
include "index.php" //返回并继续插入


?>

以上两个文件,建立好了,和你mysql的数据库中的table表,字段是id(自增长),name.

你可能感兴趣的:(php新手)