/// SQL Query
CREATE DATABASE IF NOT EXISTS `move_row`
USE `move_row`;
CREATE TABLE IF NOT EXISTS `tbl_cate` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`ordering` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
INSERT INTO `tbl_cate` (`id`, `name`, `ordering`) VALUES
(1, 'a', 1),
(2, 'b', 2),
(3, 'c', 3),
(4, 'd', 4),
(5, 'e', 5),
(6, 'f', 6);
/// My code
<?php
mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("move_row");
?>
<table width="100%">
<tr>
<th>N</th>
<th>Name</th>
<th>Ordering</th>
</tr>
<?php
$query = mysql_query("SELECT * FROM tbl_cate ORDER BY ordering ASC");
while($rows = mysql_fetch_array($query)){
?>
<tr>
<td><?php echo $rows['id'] ;?></td>
<td><?php echo $rows['name'] ;?></td>
<td>
<a href="index.php?action=move_up&move_up_id<?php echo $rows['id'];?>">Up</a>
<a href="index.php?action=move_down&move_down_id<?php echo $rows['id'];?>">Down</a>
</td>
</tr>
<?php } ?>
</table>
<?php
if(isset($_GET['action']) && ($_GET['action'] =="move_up")){
//// please help
}else if(isset($_GET['action']) && ($_GET['action'] =="move_down")){
//// please help
}
?>
Asked
Active
Viewed 87 times
1
-
2What does it mean to move rows up and down in a database? – Tim Biegeleisen Oct 07 '16 at 08:30
-
1rows don't like elevator rides – Drew Oct 07 '16 at 08:31
-
1@Drew I hear they prefer the escalator – RiggsFolly Oct 07 '16 at 08:34
-
Your script is at risk of [SQL Injection Attack](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) Have a look at what happened to [Little Bobby Tables](http://bobby-tables.com/) Even [if you are escaping inputs, its not safe!](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) Use [prepared parameterized statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) – RiggsFolly Oct 07 '16 at 08:34
-
You mean sort order the table rows? You don't need to do on database, you can manage by changing sorting with jQuery or Javascript, example: https://datatables.net/examples/basic_init/multi_col_sort.html – Igor O Oct 07 '16 at 08:35
-
How did JS enter into this. Ok, let's talk about Go Programming too :p – Drew Oct 07 '16 at 08:36
-
Table rows are not stored in an order that you can effect. The only thing you can do is create another column lets call it `rank` into which you put a number. You can then sort on that number, and change those numbers to effect the order that you see the rows when you sort on that column – RiggsFolly Oct 07 '16 at 08:38
-
Ok, thanks for comment. But I still understand, could you show me, how to move rows when click to up and click to down table rows with php mysql? If possible do have example for me to do like this. – Soy Dara Oct 07 '16 at 10:32