简体   繁体   中英

Storing multiple tags in a MySQL database problem using PHP/MySQL?

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM