0

O.K. this is some older code, but it's inserting more than it should. Before I clean it up I'm trying to understand why. Right now $_POST[tags] has only 1 value. I'm exploding it and looping through. BUT the insert3 statement towards the bottom is inserting rows with zero's as the value.

I'm at a loss for why.

if($_POST['sec'] == "next") {

      $bloguser = $_POST['bloguser'];
      $blogpassword = $_POST['blogpassword'];
      $blog = $_POST['blog'];

mysql_select_db($database) or die ("Unable to select database!"); 
$insert = mysql_query("INSERT INTO blogs (id, url, user, pass, dname, islocal, cat2post) VALUES ('', '$blog', '$bloguser', '$blogpassword', '','NO','$_POST[cat2blog]')")or die( 'Error: ' . mysql_error());
$taggit1 = mysql_insert_id();

$page->content .= "<p class=\"alert\">Success - External blog Added!</p>";


$tags  = $_POST['tags'];
$pieces = explode(",", $tags);
foreach ($pieces as $l){
$l = trim($l);  
$query = "SELECT id FROM tags WHERE tag = '$l'";
$result = mysql_query($query) or die( "Error: " . mysql_error() . " in query $query");
$row = mysql_fetch_array($result);
$taggit2 = $row[0];

if ($taggit2 == '') {
$insert2 = mysql_query("INSERT INTO tags (id, tag) VALUES ('','$l')")or die( 'Error: ' . mysql_error());
$taggit2 = mysql_insert_id();
$page->content .= "<p class=\"alert\">This tag didn't exist - so I inserted a new tag</p>";
}



$insert3 = mysql_query("INSERT INTO blogstags (id, tag_id, blogstags_id) VALUES ('','$taggit2','$taggit1')")or die( 'Error: ' . mysql_error());
$page->content .= "<p>Inserted one    </p>";
}


/// some page content crap that doesn't matter to the problem goes here///

}

Entering like this - the second record is correct. And the first just shouldn't be inserted -

id    tag_id   blogstags_id
1      1           0
2      1           6

I think the problem is where i'm exploding

$tags  = $_POST['tags'];
$pieces = explode(",", $tags);
foreach ($pieces as $l){
$l = trim($l); 

But the POST value is just a single word, so why would it run through the loop twice?

Ed Charkow
  • 71
  • 6
  • While you're updating it please make sure you escape the input data to fix the SQL injection vulnerability in that code (see http://stackoverflow.com/questions/601300/what-is-sql-injection/601524#601524 ) – John Carter May 11 '11 at 20:52
  • This is something used on localhost - fully aware it's not secure. – Ed Charkow May 11 '11 at 20:55

1 Answers1

0

If $_POST['tags']; doesn't contain a ,, then $pieces = explode(",", $tags); will be an array with only one element.

I suggest doing print_r($pieces); to sanity check that's true.

John Carter
  • 53,924
  • 26
  • 111
  • 144
  • that returns: Array ( [0] => word ) just like it should I think when there is only one word - but that doesn't explain why it's looping more than once right? – Ed Charkow May 11 '11 at 21:05
  • I suppose a better question would be why is a foreach looping more than once with an array with only one item. – Ed Charkow May 11 '11 at 21:37
  • @Ed Charkow: I do not believe that the error is in the code we see. Can you confirm the same behaviour when reducing the code to `$tags = $_POST['tags']; $pieces = explode(",", $tags); var_dump($pieces); die('stop');` at the very top of the file containing your code? – Jürgen Thelen May 11 '11 at 21:45
  • @jurgen - that prints out: array(1) { [0]=> string(7) "fishing" } stop. //// I suppose that could mean the insert statement is firing off multiple times because there is a second loop affecting this whole if. Don't see it though.... so still baffled :) – Ed Charkow May 11 '11 at 21:54
  • I just put a echo statement under the insert3 query and it's only firing off once. and the other insert is only firing off once. So I guess I can confirm that I'm only going through the loop once, but the insert3 query IS going off twice. No clue why. – Ed Charkow May 11 '11 at 22:28
  • @Ed Charkow: yeah, my bet would be an outer loop running the whole thing twice, or s/t like that. If nothing helps, you may try reducing the code to the absolute minimum still reproducing the error. – Jürgen Thelen May 11 '11 at 22:29
  • I've reduced it - and included echo statements. It's just this insert3 query inserting a row with incorrect data prior to the right one. In the OP up there I showed how it was entered data. The second row is correct, the first is not. – Ed Charkow May 11 '11 at 22:41
  • I think this might just be something to do with the templating engine I'm using. However, even when I run on it's own page I'm running into some problems. Opened this thread for some suggestions with out the confusion: http://stackoverflow.com/questions/5972686/help-me-remove-mysql-insert-id – Ed Charkow May 12 '11 at 02:25