So what I don't understand is what the heck is going on with my $num variable.
I am passing 3 Tags, 2 exist, 1 doesn't. So on the third loop through the foreach() it should go into the 'if' part of my if statement, as $num should equal 0, which my echo statements on the next page tells me it does equal 0, but it still goes into the 'else' part of my 'if'.
My Code:
<?php
$username="blanked";
$password="blanked";
$database="blanked";
$Title = $_POST['Title'];
$Post = $_POST['Post'];
$Author_ID = $_POST['Author_ID'];
$Date_Posted = $_POST['Date_Posted'];
$in_Tags = $_POST['Tags'];
$con = mysql_connect("blanked",$username,$password);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db($database) or die( "Unable to select database");
/*
Check if Tag-on-Blog_Post Exists in 'tags' table
If Tag doesn't Exist, then add 'id' and 'name' to 'tags' table
Since Tag Should Exist Either Way by Now,
Add Blog_Post_ID and Tag_ID to Blog_Post_Tags - And then it will be tagged
*/
//Resolve Author_ID
$Author_ID = 1;
$query = "INSERT INTO blog_posts VALUES
('','$Title','$Post','$Author_ID','$Date_Posted')";
mysql_query($query);
$blog_post_id = mysql_insert_id($con);
//Resolve Tag_ID
$Tags = explode(",",$in_Tags);
$num = 0;
foreach($Tags as $single_tag)
{
echo '<br><font color=red>Entering Foreach Loop</font>';
$single_tag = trim($single_tag);
$query = "SELECT id FROM tags WHERE name='$single_tag'";
$result = mysql_query($query,$con);
global $num;
$num = 0;
$num = mysql_num_rows($result); //Number of Rows in array of $result
echo ' - <font color=red>Before If</font>';
echo '@Num equals ' . $num . '@';
if ($num = 0) //if tag doesnt exists, create it. Then Revolse Tag ID either way
{
echo ' - <font color=red>In If</font>';
$query = "INSERT INTO tags (id, name)
VALUES ('', '$single_tag')";
mysql_query($query,$con);
$current_tag_id = mysql_insert_id($con);
//$query = "SELECT id FROM tags WHERE name='$single_tag'";
//$result = mysql_query($query,$con);
} //end if
else {
echo ' - <font color=red>In Else</font>';
echo '@Num equals ' . $num . '@';
$current_tag_id = mysql_result($result,0,"id");
}
echo ' - <font color=red>After Else</font>';
echo '@Num equals ' . $num . '@';
//$i=0;
//while ($i < $num) // use if these comments are used /$current_tag_id = mysql_result($result,$i,"Name");
//{
$query = "INSERT INTO blog_post_tags (blog_post_id, tag_id)
VALUES ($blog_post_id, $current_tag_id)";
mysql_query($query,$con);
//$i++;
//} //end while
} //end for each
// ---------------------------------------------------
echo ' - <font color=red>Before MySQL close</font>';
mysql_close();
?>
Success!<br>Blog Posted.<br>Now <a href="index.php">go back</a> and double check. (;
The following page gives me this:
Entering Foreach Loop - Before If@Num equals 1@ - In Else@Num equals 0@ - After Else@Num equals 0@
Entering Foreach Loop - Before If@Num equals 1@ - In Else@Num equals 0@ - After Else@Num equals 0@
Entering Foreach Loop - Before If@Num equals 0@ - In Else@Num equals 0@
PHP Error Message
Warning: mysql_result() [function.mysql-result]: Unable to jump to row 0 on MySQL result index 11 in /public_html/prot/Blog/post_blog.php on line 55
End PHP Error Message
- After Else@Num equals 0@ - Before MySQL close Success!
Blog Posted.
Now go back and double check. (;
(Line 55 by the way is commented out, so yeah, clearly it's not actual line 55)
(Actual line 55 is, "//$query = "SELECT id FROM tags WHERE name='$single_tag'";")
P.S.
If you notice any security holes please lemme know, I'm new to PHP if you can't tell by my crazy comments, and haven't yet embarked on PHP security.