tags:

views:

65

answers:

1

I was wondering how can I update my two database tables using PHP and MySQL? I want to add a second table called tags_2 that will check if a tag already exists and if it does add it to the current count in the table and if the tag does not exist add it to the table.

I hope I explained it right?

I basically need help in adding the correct code in the right places this has stumped me all day for some reason? I got the first table to work but not the second?

Here is my MySQL tables below.

CREATE TABLE tags (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
page TEXT NOT NULL,
tag VARCHAR(255) NOT NULL,
count INT NOT NULL,
PRIMARY KEY (id)
);

CREATE TABLE tags_2 (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
tag VARCHAR(255) NOT NULL,
count INT NOT NULL,
PRIMARY KEY (id)
);

And Here is my PHP script below.

<?php 

require_once ('./mysqli_connect.php');

if (isset($_POST['submitted'])){

  $mysqli = new mysqli("localhost", "root", "", "sitename");
  $dbc = mysqli_query($mysqli,"SELECT * FROM tags");
 if (!$dbc) {
  print mysqli_error($mysqli);
 }

$page = $_SERVER['SCRIPT_FILENAME'];

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

$mysqli = new mysqli("localhost", "root", "", "sitename");
$dbc = mysqli_query($mysqli,"SELECT id, count, page FROM tags WHERE tag='$tag' AND page = '$page'");

if(mysqli_num_rows($dbc)){
  $tag_info = mysqli_fetch_array($dbc);
  $tag_info_id = $tag_info["id"];
  $tag_info_count = $tag_info["count"] + 1;

  $mysqli = new mysqli("localhost", "root", "", "sitename");
  $dbc = mysqli_query($mysqli,"UPDATE tags SET count='$tag_info_count' WHERE id='$tag_info_id'");

  echo "$tag now with $tag_info_count instances";
} else {
  $mysqli = new mysqli("localhost", "root", "", "sitename");
  $clean_url = mysqli_real_escape_string($mysqli, $page);
  $dbc = mysqli_query($mysqli,"INSERT INTO tags (tag, count, page) VALUES ('$tag', 1, '$clean_url')");

if (!$dbc) {
  print mysqli_error($mysqli);
}
echo "1 record added";
}
mysqli_close($mysqli);
}
?>

Will the tables look better like this.

CREATE TABLE tags (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
page TEXT NOT NULL,
tag VARCHAR(255) NOT NULL,
count INT NOT NULL,
PRIMARY KEY (id)
);

CREATE TABLE tags_2 (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
tag_id INT UNSIGNED NOT NULL DEFAULT 0, 
tag VARCHAR(255) NOT NULL,
count INT NOT NULL,
PRIMARY KEY (id)
);
+2  A: 

First off, your schema isn't normalized. Put all the data about tags in one table. Put data about pages in another table. In the Pages table, include a foreign key that refers to the primary key of the tags table:

CREATE TABLE pages(
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
page TEXT NOT NULL,
tag_id INT UNSIGNED,
tag_count INT NOT NULL DEFAULT 0, //count is a reserved word
PRIMARY KEY (id),
FOREIGN KEY (tag_id) REFERENCES tags(id)
                      ON DELETE RESTRICT

);

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

Then, in pseudocode, your queries become:

$var= SELECT id FROM tags WHERE tag='somestring';
if $var!=0 { 
  UPDATE pages SET tag_id=$var, 
                   tag_count=tag_count+1 
  WHERE page_id='a page id';
}
dnagirl
wouldn't it be better if I just check to see if the tags name matches the other tag name in the first table?
bot-2
takes mysql longer to check against VARCHAR then it does to check against INT... if your DB grows checking each name is going to take longer and longer to return a result... best to implement like dnagirl suggests.
luckykind
but I will not no which tag belongs to which id when users submit the tag. How do I battle this problem.
bot-2
you look up your tag in the tags table and get its id. If the tag exists, use the id to update the pages table. If the tag doesn't exist, insert it into the tags table, get its id and then update the pages table.
dnagirl
exactly how would I implement your code to my code?
bot-2