数据库testdb ,表user 列email和nikename
html代码:
<form method="post" action="search1.php" name="search"> <input name="search" type="text" value="" size="15"> <input type="submit" value="Search"> </form>以下为数据库增加:...</p> <div> <form action="insert.php" method="post"> nikename: <input type="text" name="nikename" /> email: <input type="text" name="email" /> <input type="submit" /> </form> </div>
search1.php代码:
<?php $con = mysql_connect("localhost","root","root"); if (!$con) { die(‘Could not connect: ‘ . mysql_error()); } mysql_select_db("testdb", $con); $searchs = $_POST[‘search‘]; //传递search框过来的值 $searchs= trim($searchs); if (!$searchs) { echo ‘搜索框不能为空.‘; exit; } $result = mysql_query("SELECT * FROM user where nikename like ‘%$searchs%‘"); if (mysql_num_rows($result) < 1) echo ‘没有数据‘; while($row = mysql_fetch_array($result)) { echo $row[‘nikename‘] . " " . $row[‘email‘]; echo "<br />"; } mysql_close($con); ?>
insert.php 代码如下:
<?php $con = mysql_connect("localhost","root","root"); if (!$con) { die(‘Could not connect: ‘ . mysql_error()); } mysql_select_db("testdb", $con); $nikename=$_POST[‘nikename‘]; // create short variable names $email=$_POST[‘email‘]; $nikename= trim($nikename); if (!$nikename || !$email) //判断是否为空 { echo ‘不能为空‘; exit; } $sql="INSERT INTO user (nikename, email ) VALUES (‘$nikename‘,‘$email‘)"; if (!mysql_query($sql,$con)) { die(‘Error: ‘ . mysql_error()); } echo "1 record added"; mysql_close($con) ?>
时间: 2024-10-20 17:36:52