<?php $result = mysql_query($sql); while($row = mysql_fetch_array($result)) { ?> 要写的内容代码,比如说Html <?php } ?>
可以在html中添加数据提交到数据库中,前提要和数据库中名字信息一致
表单提交:
<form action="showDaYin.php" method="post">
.......
<input type="submit" name="submit" value="添加">
</form>
这是一部分提交表单中的数据的代码,其中包含有链接到数据库的
<html> <head> <title>添加信息</title> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/> </head> <body> <?php if(@$_POST['submit']){ $id = $_POST['id']; $name = $_POST['name']; $address = $_POST['address']; $age = $_POST['age']; $sex = $_POST['sex']; $hobby = $_POST['hobby']; $fav = ""; foreach($hobby as $v){ $fav.=$v.","; } if($sex){ $sex = "男"; }else{ $sex = "女"; } echo "学号:".$id."<br/>"; echo "姓名:".$name."<br/>"; echo "地址:".$address."<br/>"; echo "年龄:".$age."<br/>"; echo "性别:".$sex."<br/>"; echo "爱好:".$fav."<br/>"; $conn = mysql_connect("localhost","root","") or die("数据库连接失败!!"); //找到mysql服务器 mysql_select_db("test"); //选择链接到的数据库 mysql_query("set names 'utf8'"); //设置编码 $sql = "INSERT INTO `test`.`students`(`id`, `names`, `address`, `age`, `sex`, `hobby`)VALUES (null,'$name','$address',$age,'$sex','$fav')"; mysql_query($sql) or die(mysql_error()); //执行数据库操作 mysql_close($conn); } ?> </body> </html>
只能读出返回运行一次的值:
$result = mysql_query($sql); $row = mysql_fetch_array($result)
要显示库中所有数据需要加上循环
<?php $result = mysql_query($sql); while($row = mysql_fetch_array($result)){ ?> //内容代码 <?php } ?>
ieset 得到的是布尔类型的值(1 or 0)