DrJan-Itor
DrJan-Itor

Reputation: 33

Uploading data into two mysql tables

i have a problem where i have two tables in a database, one being categories (cat_id and cat name) and the other being jokes (joke_id and joke). I want a user to be able to chose a category and describe their joke, then this being entered into the database with the category of the joke being referenced so the joke can be portrayed on a specific category page. So far i am able to upload the joke to the database but yet i have not been able to upload the joke to a specific category.

here is the code to the joke table

CREATE TABLE `category` (
  `category_id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(51) NOT NULL,
  PRIMARY KEY (`category_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=9 ;

the code for the joke table

CREATE TABLE `jokes` (
  `joke_id` int(11) NOT NULL AUTO_INCREMENT,
  `joke` varchar(1024) NOT NULL,
  PRIMARY KEY (`joke_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;

and this peiece of code inserts the joke description to the joke table

if ($valid) {
            $pdo = Database::connect();
            $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $sql = "INSERT INTO jokes (joke) values(?)";
            $q = $pdo->prepare($sql);
            $q->execute(array($joke));
            Database::disconnect();
            header("Location: index.php");
        }

i am not sure how to upload data into the other table, with the joke being assigned a category. If anybody could help me that would be fantastic, thank you.

Here is the full file on githut: https://github.com/sambarrowclough/jokes.git

Upvotes: 1

Views: 108

Answers (3)

Brewal
Brewal

Reputation: 8189

You can't add a joke in the category table. You have to reference the category with an indexed field in your table :

+=============+  +=============+
| category    |  | jokes       |
+=============+  +=============+
| category_id |  | joke_id     |
+-------------+  +-------------+
| name        |  | joke        |
+-------------+  +-------------+
                 | category_id |
                 +-------------+

Then when you retrieve a joke, you know to which category it belongs.
This is really basic in database disigning. You should learn a bit more on database relationships.

Don't forget to create an index on the field (for huge performance gains) :

CREATE INDEX "idx_category" ON "jokes" (category_id);

Upvotes: 0

ppawel
ppawel

Reputation: 1056

I suspect you're looking for a way to somehow connect the joke with a given category. Well, that's what R stands for in RDBMS, so I would advice to do some research on "relationships" between MySql tables. I think that useful will be also concepts such as foreign key and perhaps joining tables afterwards.

But if you're looking just for quick solution, you can define your jokes table as following:

CREATE TABLE `jokes` (
  `joke_id` int(11) NOT NULL AUTO_INCREMENT,
  `joke` varchar(1024) NOT NULL,
  `category_id` int(11) NOT NULL,
  PRIMARY KEY (`joke_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;

The above doesn't add foreign key restriction but requires the category to be set (as being NOT NULL). Nevertheless, it allows you to add the new entity to database this way:

$sql = "INSERT INTO jokes (joke, category_id) values(?,?)";
$q = $pdo->prepare($sql);
$q->execute(array($joke, $category_id));

where $category_id is expected to stand for id of category the joke belongs to.

Upvotes: 1

Ronin
Ronin

Reputation: 1703

In order to link the joke to the category, you need to add correspond field in jokes table.

CREATE TABLE `jokes` (
    `joke_id` int(11) NOT NULL AUTO_INCREMENT,
    `joke` varchar(1024) NOT NULL,
    `category_id` int(11) NOT NULL,
    PRIMARY KEY (`joke_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5;

And then:

$category_id = $_POST['category_id'];

if ($valid) {
    $pdo = Database::connect();
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $sql = "INSERT INTO jokes (joke, category_id) values(?,?)";
    $q = $pdo->prepare($sql);
    $q->execute(array($joke, $category_id));
    Database::disconnect();
    header("Location: index.php");
}

category_id will show which category is applied to the joke, so you will be able retrieve correspond category info from categories table.

Upvotes: 0

Related Questions