这是最后一个页面,名为登陆页面哦 基本代码
代码语言:javascript复制echo "</table>";
echo "<br/>";
echo "<form action='denglucg.php' method='post'>";
echo "<h2>创建新事项</h2>";
echo "<label><span>题目</span><input type='text' name='title'></label>";
echo "<label><span>最后期限</span><input type='text' name='complete-by' value='YYYY-MM-DD'></label>";
echo "<label><span>描写</span><textarea name='description'></textarea></label>";
echo "<input type='submit' value='创建新事项'/>";
echo "</form>";
先打开session技术的东西,能够获取存在服务器中的数据可以获取
代码语言:javascript复制@session_start();
然后看之前有没有登陆过并且没用退出过的情况,有就显示登陆后的页面,并且
代码语言:javascript复制$current_userid=$_SESSION['userid'];
$query = "SELECT id, title,mx,ksdate,wcdate from items WHERE user_id = $current_userid";
意思是把获取的存到变量中,因为每次都获取浏览器的数据消耗性能 查询items里面的所有数据,条件是注册过的用户与登陆的用户一致就显示出哪一个用户的信息
代码语言:javascript复制$db=new mysqli('localhost','root','','bb');
$db->set_charset('utf8');
//看看链接成不成功,
if ($db->connect_errno) {
echo "这次链接有问题。,重新登录把";
exit();
}
不用我说了把,这个
更新操作
代码语言:javascript复制if(isset($_POST['completed-item'])) {
$item_id = $_POST['completed-item'];
$completed_query = "UPDATE items set wcdate= '".date("Y-m-d")."' WHERE id = $item_id";
$completed_result = $db->query($completed_query);
if(!$completed_result) {
echo "没有更新";
exit();
}
}
插入操作
代码语言:javascript复制if(isset($_POST['title'])) {
$title = $_POST['title'];
$description = $_POST['description'];
$complete_by = $_POST['complete-by'];
$user_id = $_SESSION['userid'];
$stmt = $db->prepare("INSERT INTO items VALUES (?,?,?,?,?,?)");
$id = NULL;
$completed_on = NULL;
$stmt->bind_param("issssi", $id, $title, $description, $complete_by, $completed_on, $user_id);
$stmt->execute();
if(!$stmt->affected_rows) {
echo "没有插入";
}
}
删除数据
代码语言:javascript复制if(isset($_POST['item-to-delete'])) {
$item_to_delete = $_POST['item-to-delete'];
$delete_query = "DELETE FROM items WHERE id = $item_to_delete";
$delete_result = $db->query($delete_query);
if(!$delete_result) {
echo "没有删除";
exit();
}
}
老规矩:field_seek(1);跳过第一个字段。,为什么,因为用户的id是从1开始的啊 $result->fetch_field()获取字段 fetch_assoc()关联数组
代码语言:javascript复制if($result->num_rows) {
echo "<table>";
echo "<caption>我的To-Do List</caption>";
echo "<thead>";
echo "<tr>";
echo "<th>号</th>";
$result->field_seek(1);
while($field = $result->fetch_field()) {
echo "<th>".$field->name."</th>";
}
echo "<th>删除</th>";
echo "</tr>";
echo "</thead>";
for($i = 1; ($row = $result->fetch_assoc()); $i ) {
echo "<tr>";
echo "<td>$i</td>";
echo "<td>".$row['title']."</td>";
echo "<td>".$row['mx']."</td>";
echo "<td>";
if(!$row['ksdate']) {
echo "无最后期限";
} else {
echo $row['ksdate'];
}
echo "</td>";
if(!$row['wcdate']) {
echo "<td>";
echo "<form action='denglucg.php' method='post'>";
echo "<input type='hidden' value='".$row['id']."' name='completed-item' />";
echo "<input type='submit' value='标记为完成了'/>";
echo "</form>";
echo "</td>";
} else {
echo "<td>".$row['wcdate']."</td>";
}
echo "<td>";
echo "<form action='denglucg.php' method='post'>";
echo "<input type='hidden' name='item-to-delete' value='".$row['id']."'/>";
echo "<input type='submit' value='删除'/>";
echo "</form>";
echo "</td>";
echo "</td>";
echo "</tr>";
}
}