tags:

views:

56

answers:

2

First let me explain that I have this script that should let users enter multiple tags that are separated by a comma for example, shoe, shirt, hat, glasses and store each tag in the database.

But for some reason the tags are not being stored in the database correctly. For instance when a user enters two or more tags they are stored all in the same row instead of displaying them in there own rows plus the tags are not even being entered into the database only a blank row is entered?

Can someone please give me a couple of examples of what I need to change in my script in-order to fix this problem?

Here is the mysql tables below.

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')
);

Here is the script.

<?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);
}
?>
A: 

replace this:

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

with this:

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

and BTW, you need to secure against sql injections and XSS

update: you also need to replace this:

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

with this

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

x is a variable ... must use $x

Update2: The questions_tags code must be inside the for-loop

Here's the fixed code

<?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);
}
?>
Aziz
Now its not updating my questions_tags table correctly its storing all the tags info on one row?
MyHeadHurts
updated the answer .. try it
Aziz
Didn't work, any other solutions?
MyHeadHurts
what exactly is inserted to the database ?
Aziz
It only associates one tag to the users question, I want it to associate all the tags to the users questions on there own row.
MyHeadHurts
ok, i see the problem .. the code that inserts tags to `questions_tags` is out of the for-loop. It should be inside it so that you add an entry for every tag. I updated my answer.
Aziz
+1  A: 

Take a look at explode() to seperate the list of tags entered. Iterate over the list using a foreach/for loop and insert each tag seperately.

Best wishes,
Fabian

halfdan