2017-07-26 25 views
0

問題があります。私はあることを私の希望idを再配置できるようにしたいidallowCommentsfromUserIdcategorycategoryTitlepricelikesCountimagesCountviewsCountreviewsCountなどSQLテーブルを再配置する方法:IDを最後のIDに

のような内容で、私のデータベース内のアイテムと呼ばれるテーブルを持っています最新のid、残りを上に持ちます。ここで

は例です:

id allowComments fromUserId category categoryTitle  price 

1  1    1   3   Phone   20000 
2  1    1   5   Car   100000 
3  1    5   2   Console  20000 
4  1    2   1   Fashion  100 
5  0    1   3   Phone   12000 
6  1    2   3   Phone   21300 

だから私の質問は、私はid3が最後idなり、ANに上に移動するために、他のIDを並べ替える作るためにPHPを使用する方法であり、秩序あるファッション?

はただ、これはデータベース私はあなたが知る必要があるすべてをコメントしています以下のコードで

       <th>Id</th> 
           <th>category</th> 
           <th>price</th> 
           <th>cATEGORY ID </th> 
           <th>fromUserId</th> 
           <th>EDIT </th> 
           <th>REFRESH </th> 

          </tr> 

          <?php $k=1; while($get=mysql_fetch_array($insertionquery)) {?> 

<td><?php echo $get['id'];?></td> 
<td><?php echo $get['categoryTitle'];?></td> 
    <td><?php echo $get['price']; ?></td> 
    <td><?php echo $get['category']; ?></td> 
    <td><?php echo $get['fromUserId']; ?></td> 

<td><a href="editmenu.php?EDITC=<?php echo $get['id']?>"></a></td> 
<td><a href="refresh.php?REFRESH=<?php echo $get['id']?>"></a></td> 

+1

通常、移動IDはデータベースにとっては悪い考えです代わりにソート列を追加する方法について – rtfm

+0

最初にIDを最後のIDに設定します。 'UPDATE table_name SET id =(SELECT max(id)+1)FROM table_name WHERE id = 3;'。次に、auto_incrementカウンタを次のようにリセットします: 'ALTER TABLE table_name AUTO_INCREMENT = 1' – icecub

+0

少し混乱してPHPファイルの形式でコードを書いてください。 –

答えて

0

よし、からコンテンツを取得するために私のPHPファイルであると言うことができます。これを読んで、それがどのように機能するかを理解することが重要です!私はあなたにもこのリンクを残しています:PHP Prepared Statementsより使いやすい環境でPDOとPrepared Statementの詳細を知ることができます。この質問の一番下には、すべてのコメントがないコードがあります。

コードの先頭に正しいデータベース情報を設定する必要があることに注意してください。コメントで説明して

コード:

<?php 

/* Database info */ 
$dbhost = 'localhost'; 
$dbuser = ''; 
$dbpass = ''; 
$dbname = 'globali2_olx'; 

/* First we set up some information for our PDO object */ 
$dsn = 'mysql:host=' . $dbhost . ';dbname=' . $dbname; 
$options = array(
    PDO::ATTR_PERSISTENT => true, 
    PDO::ATTR_ERRMODE  => PDO::ERRMODE_EXCEPTION 
); 

/* Next we try to instantiate the PDO object and see if we can connect to the database */ 
try{ 
    $pdo = new PDO($dsn, $dbuser, $dbpass, $options); 
} 
/* Here we'll catch any database connection errors and output them so we know what's going on */ 
catch(PDOException $e){ 
    $error = $e->getMessage(); 
    return $error; 
    exit; 
} 

/* Now lets try to change row id 3 to the last row id */ 

/* First we setup our query */ 
$query = 'UPDATE items SET id=(SELECT max(id)+1) FROM items WHERE id = :id'; 

/* As you can see, we don't have the number 3 in there. Instead, we have a 
    placeholder ':id'. This is because we're going to use Prepared Statements. 
    They are not needed for this case, but I want to show how to do this 
    because you do need them if you want to insert data that's provided by a user. 
    This is to prevent SQL injection. SQL injection is very easy and allows 
    visitors to your website to completely delete your database if you don't 
    protect yourself against it. This will do just that. */ 

/* So lets prepare our query first */ 
$stmt = $pdo->prepare($query); 

/* Set ID */ 
$id = 3; 

/* Now we're going to bind the data to the placeholder */ 
$stmt->bindParam(':id', $id, PDO::PARAM_INT); 

/* Now all that's left to do is execute it so our database gets updated */ 
try { 
    $stmt->execute(); 
} 
/* Again, catch any errors in our query and output them */ 
catch(PDOException $e){ 
    $error = $e->getMessage(); 
    return $error; 
    exit; 
} 

/* Next we need to re-arrange the id's */ 

/* First we setup our query */ 
$query = 'ALTER TABLE items AUTO_INCREMENT = 1'; 

/* Again, Prepared Statements are not nessesary here. So I'm gonna show 
    you how to do it without them. */ 

/* All we have to do is query the database directly */ 
try { 
    $pdo->query($query); 
} 
/* Again, catch any errors in our query and output them */ 
catch(PDOException $e){ 
    $error = $e->getMessage(); 
    return $error; 
    exit; 
} 

?> 

クリーンコード:上記のスクリプトはそう私は研究し、何かが間違っているを発見でしたがうまくいきませんでしたテスト済み

<?php 

/* Database info */ 
$dbhost = 'localhost'; 
$dbuser = ''; 
$dbpass = ''; 
$dbname = 'globali2_olx'; 

/* Set DSN and Options */ 
$dsn = 'mysql:host=' . $dbhost . ';dbname=' . $dbname; 
$options = array(
    PDO::ATTR_PERSISTENT => true, 
    PDO::ATTR_ERRMODE  => PDO::ERRMODE_EXCEPTION 
); 

/* Instantiate the PDO object */ 
try{ 
    $pdo = new PDO($dsn, $dbuser, $dbpass, $options); 
} 
catch(PDOException $e){ 
    $error = $e->getMessage(); 
    return $error; 
    exit; 
} 

/* Set query */ 
$query = 'UPDATE items SET id=(SELECT max(id)+1) FROM items WHERE id = :id'; 

/* Prepare query */ 
$stmt = $pdo->prepare($query); 

/* Set ID */ 
$id = 3; 

/* Bind values */ 
$stmt->bindParam(':id', $id, PDO::PARAM_INT); 

/* Execute query */ 
try { 
    $stmt->execute(); 
} 
catch(PDOException $e){ 
    $error = $e->getMessage(); 
    return $error; 
    exit; 
} 

/* Set query */ 
$query = 'ALTER TABLE items AUTO_INCREMENT = 1'; 

/* Query database */ 
try { 
    $pdo->query($query); 
} 
catch(PDOException $e){ 
    $error = $e->getMessage(); 
    return $error; 
    exit; 
} 

?> 
+0

このようなエラーが発生します。致命的なエラー:C:\ xampp \ htdocs \ kobobay \ admin \ refresh.php($ stmt- > bindParam( ':id'、3); –

+0

@ Sir-mykeこれで修正されるはずです。 – icecub

+0

@ Sir-mykeこれで修正する必要があります。私は両方のコードを修正するのを忘れた。私の悪い。 – icecub

1

。以下のコードを使用して私はそれをテストし、それが

<?php 

/* Database info */ 
$dbhost = 'localhost'; 
$dbuser = 'root'; 
$dbpass = ''; 
$dbname = 'globali2_olx'; 

/* Set DSN and Options */ 
$dsn = 'mysql:host=' . $dbhost . ';dbname=' . $dbname; 
$options = array(
PDO::ATTR_PERSISTENT => true, 
PDO::ATTR_ERRMODE  => PDO::ERRMODE_EXCEPTION 
); 

/* Instantiate the PDO object */ 
try{ 
    $pdo = new PDO($dsn, $dbuser, $dbpass, $options); 
} 
catch(PDOException $e){ 
    $error = $e->getMessage(); 
    return $error; 
    exit; 
} 

/* Set ID */ 
$id = 25; 

/* Set query */ 

$query = "update location j1 inner join location j2 on j1.id <= j2.id 
left outer join location j3 on j2.id < j3.id 
set j1.id = j2.id + 1 
where j1.id = $id and j3.id is null"; 

/* Prepare query */ 
$stmt = $pdo->prepare($query); 

/* Bind values */ 
$stmt->bindParam(':id', $id, PDO::PARAM_INT); 

/* Execute query */ 
try { 
    $stmt->execute(); 
} 
catch(PDOException $e){ 
    $error = $e->getMessage(); 
    return $error; 
    exit; 
} 

/* Set query */ 
$query = 'ALTER TABLE location AUTO_INCREMENT = 1'; 

/* Query database */ 
try { 
    $pdo->query($query); 
} 
catch(PDOException $e){ 
    $error = $e->getMessage(); 
    return $error; 
    exit; 
} 

?> 

を働いたか、あなたはこの単純なコード

<?php 


include_once($_SERVER['DOCUMENT_ROOT']."/config/config.php"); 

// Create connection 
$con = mysqli_connect($host, $user, $pass, $database); 


$id = 128; 

$Sql_Query = "update product set id=3 where id=1"; 

$Sql_Query = "update product j1 inner join product j2 on j1.id <= j2.id 
left outer join product j3 on j2.id < j3.id 
set j1.id = j2.id + 1 
where j1.id = $id and j3.id is null"; 

if(mysqli_query($con,$Sql_Query)) 
{ 
echo 'Record Updated Successfully'; 
} 
else 
{ 
echo 'Something went wrong'; 
} 

mysqli_close($con); 
?> 
+0

ありがとうございます –

0

研究コードを使用すると、あなたのための最良の1を使用することができます。テーブルを変更し、それはすべての

UPDATE jobs j1 
INNER JOIN jobs j2 ON j1.worker_id <= j2.worker_id 
LEFT OUTER JOIN jobs j3 ON j2.worker_id < j3.worker_id 
SET j1.worker_id = j2.worker_id + 1 
WHERE j1.worker_id = 3 AND j3.worker_id IS NULL; 
  • J1はWHERE句で条件に基づいて、変更したい行で動作します。
  • j2は、worker_idがj1以上の行のセットです。
  • j3は、j2よりも大きなworker_idを持つ行のセットです(j2が表内で最大のworker_idを持つ場合、j3の行はないため、j3。*はNULLになります)。
  • したがって、j2は最高のworker_idを持つため、値+ 1を使用します。

デモ:

mysql> create table jobs (id serial primary key, worker_id int); 
mysql> insert into jobs (worker_id) values (1), (2), (3), (4), (5); 

mysql> select * from jobs; 
+----+-----------+ 
| id | worker_id | 
+----+-----------+ 
| 1 |   1 | 
| 2 |   2 | 
| 3 |   3 | 
| 4 |   4 | 
| 5 |   5 | 
+----+-----------+ 

mysql> update jobs j1 inner join jobs j2 on j1.worker_id <= j2.worker_id 
left outer join jobs j3 on j2.worker_id < j3.worker_id 
set j1.worker_id = j2.worker_id + 1 
where j1.worker_id = 3 and j3.worker_id is null; 

mysql> select * from jobs; 
+----+-----------+ 
| id | worker_id | 
+----+-----------+ 
| 1 |   1 | 
| 2 |   2 | 
| 3 |   6 | 
| 4 |   4 | 
| 5 |   5 | 
+----+-----------+ 

それは関係なく、我々が変更したい値の作品:

mysql> update jobs j1 inner join jobs j2 on j1.worker_id <= j2.worker_id 
left outer join jobs j3 on j2.worker_id < j3.worker_id 
set j1.worker_id = j2.worker_id + 1 
where j1.worker_id = 5 and j3.worker_id is null; 

mysql> select * from jobs; 
+----+-----------+ 
| id | worker_id | 
+----+-----------+ 
| 1 |   1 | 
| 2 |   2 | 
| 3 |   6 | 
| 4 |   4 | 
| 5 |   7 | 
+----+-----------+ 

そして、それは我々がすでに最高値を持つ行を変更している場合でも動作しますテーブル内:

mysql> update jobs j1 inner join jobs j2 on j1.worker_id <= j2.worker_id 
left outer join jobs j3 on j2.worker_id < j3.worker_id 
set j1.worker_id = j2.worker_id + 1 
where j1.worker_id = 7 and j3.worker_id is null; 

mysql> select * from jobs; 
+----+-----------+ 
| id | worker_id | 
+----+-----------+ 
| 1 |   1 | 
| 2 |   2 | 
| 3 |   6 | 
| 4 |   4 | 
| 5 |   8 | 
+----+-----------+ 
関連する問題