2009-12-06 76 views
0

首先讓我解釋一下,我有這個腳本應該讓用戶輸入多個用逗號分隔的標籤,例如shoe, shirt, hat, glasses,並將每個標籤存儲在數據庫中。使用PHP/MySQL在MySQL數據庫問題中存儲多個標籤?

但由於某些原因標籤沒有正確存儲在數據庫中。例如,當用戶輸入兩個或多個標籤時,它們全部存儲在同一行中,而不是顯示在自己的行中,並且標籤甚至沒有被輸入到數據庫中,只輸入空行。

有人能給我幾個我需要在腳本中改變的例子,以便解決這個問題嗎?

下面是mysql表格。

CREATE TABLE questions_tags (
id INT UNSIGNED NOT NULL AUTO_INCREMENT, 
tag_id INT UNSIGNED NOT NULL, 
users_questions_id INT UNSIGNED NOT NULL, 
PRIMARY KEY (id) 
); 

CREATE TABLE tags (
id INT UNSIGNED NOT NULL AUTO_INCREMENT, 
tag VARCHAR(255) NOT NULL, 
PRIMARY KEY (id), 
UNIQUE('tag') 
); 

這是腳本。

<?php 
require_once ('./mysqli_connect.php'); 

if (isset($_POST['submitted'])) { 
     $mysqli = new mysqli("localhost", "root", "", "sitename"); 
     $dbc = mysqli_query($mysqli,"SELECT questions_tags.*, tags.* FROM questions_tags, tags"); 
    if (!$dbc) { 
     print mysqli_error($mysqli); 
    } 
$page = '3'; 

$tag = mysqli_real_escape_string($mysqli, $_POST['tag']); 

$mysqli = new mysqli("localhost", "root", "", "sitename"); 
$dbc = mysqli_query($mysqli,"SELECT questions_tags.*, tags.* FROM questions_tags INNER JOIN tags ON tags.id = questions_tags.tag_id WHERE questions_tags.users_questions_id=3"); 
if(mysqli_num_rows($dbc) >= 0){ 

if (isset($_POST['tag'])){ 
    $tags = explode(",", $_POST['tag']); 

    for ($x = 0; $x < count($tags); $x++){ 

$mysqli = new mysqli("localhost", "root", "", "sitename"); 
$clean_url = mysqli_real_escape_string($mysqli, $page); 

$query1 = "INSERT INTO tags (tag) VALUES ('$tags[x]')"; 

if (!mysqli_query($mysqli, $query1)) { 
    print mysqli_error($mysqli); 
    return; 
} 

$mysqli = new mysqli("localhost", "root", "", "sitename"); 
$dbc = mysqli_query($mysqli,"SELECT id FROM tags WHERE tag='$tags[x]'"); 
    } 
} 

if (!$dbc) { 
    print mysqli_error($mysqli); 
} else { 
    while($row = mysqli_fetch_array($dbc)){ 
     $id = $row["id"]; 
    } 
} 

$query2 = "INSERT INTO questions_tags (tag_id, users_questions_id) VALUES ('$id', '$page')"; 

if (!mysqli_query($mysqli, $query2)) { 
    print mysqli_error($mysqli); 
    return; 
} 

echo "$tag has been entered"; 

    if (!$dbc) { 
      print mysqli_error($mysqli); 
    } 
} 
mysqli_close($mysqli); 
} 
?> 

回答

0

替換此:

$query1 = "INSERT INTO tags (tag) VALUES ('$tags[x]')"; 

與此:

$query1 = "INSERT INTO tags (tag) VALUES ('" . $tags[$x] . "')"; 

和BTW,你需要確保對sql injectionsXSS

更新:你還需要替換爲:

$dbc = mysqli_query($mysqli,"SELECT id FROM tags WHERE tag='$tags[x]'"); 

與此

$dbc = mysqli_query($mysqli,"SELECT id FROM tags WHERE tag='" . $tags[$x] . "'"); 

x是一個變量...必須使用$x

UPDATE2:該questions_tags代碼必須for循環

內這裏的固定碼

<?php 
require_once ('./mysqli_connect.php'); 

if (isset($_POST['submitted'])) { 
    $mysqli = new mysqli("localhost", "root", "", "sitename"); 
    $dbc = mysqli_query($mysqli,"SELECT questions_tags.*, tags.* FROM questions_tags, tags"); 
    if (!$dbc) { 
     print mysqli_error($mysqli); 
    } 
    $page = '3'; 

    $tag = mysqli_real_escape_string($mysqli, $_POST['tag']); 

    $mysqli = new mysqli("localhost", "root", "", "sitename"); 
    $dbc = mysqli_query($mysqli,"SELECT questions_tags.*, tags.* FROM questions_tags INNER JOIN tags ON tags.id = questions_tags.tag_id WHERE questions_tags.users_questions_id=3"); 
    if(mysqli_num_rows($dbc) >= 0){ 

     if (isset($_POST['tag'])) { 
      $tags = explode(",", $_POST['tag']); 

      for ($x = 0; $x < count($tags); $x++){ 

       $mysqli = new mysqli("localhost", "root", "", "sitename"); 
       $clean_url = mysqli_real_escape_string($mysqli, $page); 

       $query1 = "INSERT INTO tags (tag) VALUES ('" . $tags[$x] . "')"; 

       if (!mysqli_query($mysqli, $query1)) { 
        print mysqli_error($mysqli); 
        return; 
       } 

       $mysqli = new mysqli("localhost", "root", "", "sitename"); 
       $dbc = mysqli_query($mysqli,"SELECT id FROM tags WHERE tag='" . $tags[$x] . "'"); 


       if (!$dbc) { 
        print mysqli_error($mysqli); 
       } else { 
        while($row = mysqli_fetch_array($dbc)){ 
         $id = $row["id"]; 
        } 
       } 

       $query2 = "INSERT INTO questions_tags (tag_id, users_questions_id) VALUES ('$id', '$page')"; 

       if (!mysqli_query($mysqli, $query2)) { 
        print mysqli_error($mysqli); 
        return; 
       } 

       echo "$tag has been entered"; 
      } 
     } 

     // is this needed? 
     // if (!$dbc) { 
     // print mysqli_error($mysqli); 
     // } 
    } 
    mysqli_close($mysqli); 
} 
?> 
+0

現在,它沒有更新我的questions_tags表正確的存儲上一行的所有標籤信息? – MyHeadHurts 2009-12-06 11:36:48

+0

更新了答案..試試吧 – Aziz 2009-12-06 11:44:24

+0

沒有工作,有沒有其他解決方案? – MyHeadHurts 2009-12-06 11:46:18

1

查看explode()以分離輸入的標籤列表。使用foreach/for循環遍歷列表,並分別插入每個標籤。

最良好的祝願,
費邊

相關問題