首先,我为发布如此庞大的代码块而道歉。它可能与问题无关,但以防万一......代码维护了一个简单的ToDo列表,我希望将其合并到现有的PHP网站中,该网站为每个用户存储大量信息。换句话说,我想将此添加到mySQL DB中用户的信息行。
我是PHP的新手,但通过提出想法并弄清楚如何使它们发挥作用已经走过了漫长的道路。你能否指点我添加这样的功能,通过添加&存储信息。删除信息行,分配给用户的字段列表?
另一种表达方式:我希望为用户提供一种维护自己的待办事项列表的方法。
<?php
$conn = mysql_connect('server, 'db', 'password') or die(mysql_error());
$db = mysql_select_db('db',$conn) or die(mysql_error());
// if an arrow link was clicked...
if ($_GET['dir'] && $_GET['id']) {
// make GET vars easier to handle
$dir = $_GET['dir'];
// cast as int and couple with switch for sql injection prevention for $id
$id = (int) $_GET['id'];
// decide what row we're swapping based on $dir
switch ($dir) {
// if we're going up, swap is 1 less than id
case 'up':
// make sure that there's a row above to swap
$swap = ($id > 1)? $id-- : 1;
break;
// if we're going down, swap is 1 more than id
case 'down':
// find out what the highest row is
$sql = "SELECT count(*) FROM info";
$result = mysql_query($sql, $conn) or die(mysql_error());
$r = mysql_fetch_row($result);
$max = $r[0];
// make sure that there's a row below to swap with
$swap = ($id < $max)? $id++ : $max;
break;
// default value (sql injection prevention for $dir)
default:
$swap = $id;
} // end switch $dir
// swap the rows. Basic idea is to make $id=$swap and $swap=$id
$sql = "UPDATE info SET usort = CASE usort WHEN $id THEN $swap WHEN $swap THEN $id END WHERE usort IN ($id, $swap)";
$result = mysql_query($sql, $conn) or die(mysql_error());
} // end if GET
// set a result order with a default (sql infection prevention for $sortby)
$sortby = ($_GET['sortby'] == 'name')? $_GET['sortby'] : 'usort';
// pull the info from the table
$sql = "SELECT usort, name FROM info ORDER BY $sortby";
$result = mysql_query($sql, $conn) or die(mysql_error());
// display table
echo "<table border = '1'>";
echo "<tr>";
// make column names links, passing sortby
echo "<td><a href='{$_SERVER['PHP_SELF']}?sortby=usort'>usort</a></td>";
echo "<td><a href='{$_SERVER['PHP_SELF']}?sortby=name'>name</a></td>";
echo "</tr>";
// delete from table
if ($_GET['del'] == 'true') {
// cast id as int for security
$id = (int) $_GET['id'];
// delete row from table
$sql = "DELETE FROM info WHERE usort = '$id'";
$result = mysql_query($sql, $conn) or die(mysql_error());
// select the info, ordering by usort
$sql = "SELECT usort, name FROM info ORDER BY usort";
$result = mysql_query($sql, $conn) or die(mysql_error());
// initialize a counter for rewriting usort
$usort = 1;
// while there is info to be fetched...
while ($r = mysql_fetch_assoc($result)) {
$name = $r['name'];
// update the usort number to the one in the next number
$sql = "UPDATE info SET usort = '$usort' WHERE name = '$name'";
$update = mysql_query($sql, $conn) or die(mysql_error());
// inc to next avail number
$usort++;
} // end while
} // end if del
// display data 1 row at a time
while ($r = mysql_fetch_assoc($result)) {
echo "<tr>";
// make the links to change custom order, passing direction and the custom sort id
echo "<td align = 'center'><a href='{$_SERVER['PHP_SELF']}?dir=up&id={$r['usort']}'>/\</a> ";
echo "<a href='{$_SERVER['PHP_SELF']}?dir=down&id={$r['usort']}'>\/</a></td>";
echo "<td>{$r['name']}</td>";
echo "<td><a href='{$_SERVER['PHP_SELF']}?del=true&id={$r['usort']}'>delete</a></td>";
echo "</tr>";
} // end while $r
echo "</table>";
// end display table
?>
答案 0 :(得分:1)
用户待办事项清单似乎是我的另一张表。您无需更改用户信息中的任何值。只需添加,删除或更改另一个表中的任务顺序。像下图这样的东西