Confusing INSERT Problem.

Discussion in 'PHP' started by scottlpool2003, May 14, 2009.

  1. #1
    I can't work out why this is happening but I'm trying to enter a searched term into a database but it inserts twice for some bizzare reason. Can anyone work out why it's doing this?

    Here's the code, i've seperated the INSERT code from the rest.:
    
    
    <?
    //connect to your database ** EDIT REQUIRED HERE **
    mysql_connect("","",""); //(host, username, password)
    
    //specify database ** EDIT REQUIRED HERE **
    mysql_select_db("scott_movies") or die("Unable to select database"); //select which database we're using;
    ?><?
      // Get the search variable from URL
    $s = @$_GET['s'];
      $var = @$_GET['q'] ;
      $trimmed = trim($var); //trim whitespace from the stored variable
    $str = strtoupper($title);
    
    
    
    
    
    $title=$_GET['q']; 
    mysql_connect("", "", "") or die(mysql_error()); 
    mysql_select_db("") or die(mysql_error()); 
    mysql_query("INSERT INTO `searched` VALUES (
    '$title',
    '$id',
    '$views'
    )
    "); 
    
    
    
    
    
    $title = "Watch " . $var . " Full Movie Online Free";
    $keywords = "Watch " . $var . " Free, Watch " . $var . " movies, watch " . $var . " movies, free online movies, watch free movies, ";
    $description = "Watch " . $var . " Free";
    ?> 
    
    
    <?
    //connect to your database ** EDIT REQUIRED HERE **
    mysql_connect("","",""); //(host, username, password)
    
    //specify database ** EDIT REQUIRED HERE **
    mysql_select_db("") or die("Unable to select database"); //select which database we're using;
    ?>
    <?php include"includes/head.php";?>
    <?php include"includes/head2.php";?>
    <?php include"includes/topadsense.php";?>
      <div id="myGallery">
        				 <div class="imageElement">
    
    <h2><a href="#">Latest Movies</a></h2>
    
    
    
    <div class="entry">
    <p>
    <table align="center">
    <?php
    
      // Get the search variable from URL
    $s = @$_GET['s'];
      $var = @$_GET['q'] ;
      $trimmed = trim($var); //trim whitespace from the stored variable
    
    // rows to return
    $limit=10; 
    
    // check for an empty string and display a message.
    if ($trimmed == "")
      {
      echo "<p>Please enter a search...</p>";
      exit;
      }
    
    // check for a search parameter
    if (!isset($var))
      {
      echo "<p>We dont seem to have a search parameter!</p>";
      exit;
      }
    
    //connect to your database ** EDIT REQUIRED HERE **
    mysql_connect("","",""); //(host, username, password)
    
    //specify database ** EDIT REQUIRED HERE **
    mysql_select_db("") or die("Unable to select database"); //select which database we're using
    
    // Build SQL Query  
    
    $query = "SELECT * FROM films WHERE id"; // EDIT HERE and specify your table and field names for the SQL query
    
     $numresults=mysql_query($query);
     $numrows=mysql_num_rows($numresults);
    // Build SQL Query  
    $query = "SELECT * FROM films WHERE title LIKE \"%$trimmed%\" order by title"; // EDIT HERE and specify your table and field names for the SQL query
     $numresults=mysql_query($query);
     $numrows=mysql_num_rows($numresults);
    
    // If we have no results, offer a google search as an alternative
    
    if ($numrows == 0)
      {
      echo "<h4>Results</h4>";
    
      echo "<p>Sorry, your search: &quot;" . $trimmed . "&quot; returned zero results</p>";
    
    // google
     echo "<p><a href=\"http://www.google.com/search?q=" 
      . $trimmed . "\" target=\"_blank\" title=\"Look up 
      " . $trimmed . " on Google\">Click here</a> to try the 
      search on google</p>";
      }
    
    // next determine if s has been passed to script, if not use 0
      if (empty($s)) {
      $s=0;
      }
    
    // get results
      $query .= " limit $s,$limit";
      $result = mysql_query($query) or die("Couldn't execute query");
    
    // display what the person searched for
    echo "<p>You searched for: &quot;" . $var . "&quot;</p>";
    
    // begin to show results set
    //echo "Results";
    $count = 1 + $s ;
    
    // now you can display the results returned
      while ($row= mysql_fetch_array($result)) {
      $title = $row["1st_field"];
      $title = $row["title"];
      $img = $row["img"];
      $id = $row["id"];
      $views =$row["views"];
     $released =$row["released"];
        echo "<table bgcolor='#f2f8f8' width='50%' align='center' style='border-bottom:2px solid #414141;border-left:2px solid #414141;border-top:2px solid #414141;border-right:2px solid #414141;'><tr>";
    	echo "<tr><td align='left' width='60%' style='border-bottom:1px solid silver;'>TITLE</td> <td width='10%' style='border-bottom:1px solid silver;border-left:1px solid silver;' align='center'>VIEWS</font></td><td width='10%' style='border-bottom:1px solid silver;border-left:1px solid silver;' align='center'>RELEASED</font></td><td width='15%' align='CENTER' style='border-bottom:1px solid silver;border-left:1px solid silver;'>PLAY</td></tr>";
    echo "<tr><td width='73%' align='left' style='border-bottom:1px solid silver;'><a href='http://onlinemovieguru.com/preview.php?id=$id' title='$title'><img src='http://www.onlinemovieguru.com/images/movies/$img' width='90px' height='119px' style='border:2px solid #414141;'><br>$title</a></td><td width='10%' style='border-bottom:1px solid silver;border-left:1px solid silver;' align='center'><font color=red> $views</font></td><td width='10%' style='border-bottom:1px solid silver;border-left:1px solid silver;' align='center'><font color=red> $released</font></td><td width='15%' valign='middle' style='border-bottom:1px solid silver;border-left:1px solid silver;'><a href='' title='' style='font-size:12px;color:blue;'><a href='http://onlinemovieguru.com/preview.php?id=$id' title='$title'><img src='/play.png' border='0' height='100px' width=150px' alt=''></a></td></tr>";
        echo "</table>";			
      $count++ ;
      }
    
    $currPage = (($s/$limit) + 1);
    
    //break before paging
      echo "<br />";
    
    
    
    // calculate number of pages needing links
      $pages=intval($numrows/$limit);
    
    // $pages now contains int of pages needed unless there is a remainder from division
    
      if ($numrows%$limit) {
      // has remainder so add one page
      $pages++;
      }
    
    // check to see if last page
      if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {
    
      // not last page so give NEXT link
      $news=$s+$limit;
    
      echo "<center>&nbsp;<a href=\"$PHP_SELF?s=$news&q=$var\" title=\"Next\">Next 10 &gt;&gt;</a>";
      }
      // next we need to do the links to other results
      if ($s>=1) { // bypass PREV link if s is 0
      $prevs=($s-$limit);
      echo "<center>";
      print "&nbsp;<a href=\"$PHP_SELF?s=$prevs&q=$var\" title=\"Previous\">&lt;&lt; 
      Prev 10</a>&nbsp&nbsp;";
      echo "</center>";
      }
    $a = $s + ($limit) ;
      if ($a > $numrows) { $a = $numrows ; }
      $b = $s + 1 ;
      echo "<p>Showing results $b to $a of $numrows</p>";
      
    ?>
    </table></p>
    
    
    
    
    </div>
    
    
    					
    				  </div> </div>
     </div>
     </div>
    
    
       
          
    </div></div></div></div>
    
    <?php include"includes/nav.php";?>
    <?php include"includes/footer.php";?>
    
    PHP:
    It's this section of the code that I'm inserting the searched term:
    $title=$_GET['q']; 
    mysql_connect("", "", "") or die(mysql_error()); 
    mysql_select_db("") or die(mysql_error()); 
    mysql_query("INSERT INTO `searched` VALUES (
    '$title',
    '$id',
    '$views'
    )
    "); 
    PHP:
    Thanks if you solve it :)
     
    scottlpool2003, May 14, 2009 IP
  2. KnuTz

    KnuTz Well-Known Member

    Messages:
    169
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    130
    Digital Goods:
    1
    #2
    you need to check if q is not empty or is set before you insert it to your database
     
    KnuTz, May 14, 2009 IP
  3. scottlpool2003

    scottlpool2003 Well-Known Member

    Messages:
    1,708
    Likes Received:
    49
    Best Answers:
    9
    Trophy Points:
    150
    #3
    q can't be empty anyway.... if q is empty it won't search. But either way checking if it's empty or not it shouldn't effect the INSERT function.
     
    scottlpool2003, May 14, 2009 IP
  4. yoavmatchulsky

    yoavmatchulsky Member

    Messages:
    57
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    48
    #4
    if $id is an integer in the db, it can't be inserted with quotes.

    also, i would add the fields to insert:

    INSERT INTO searched (title, id, views) VALUES ('$title', $id, $views)
     
    yoavmatchulsky, May 14, 2009 IP
  5. scottlpool2003

    scottlpool2003 Well-Known Member

    Messages:
    1,708
    Likes Received:
    49
    Best Answers:
    9
    Trophy Points:
    150
    #5
    Thanks for your reply. All the data is inserting into the db correctly. It's just inserting the searched term twice.

    I'm not adding views as yet but it's in the db. And '$id' is auto increment. I need $id because I'm calling latest searches from the db and I obviously needed some kind of order to call them by.

    I just need to know why it's inserting twice.
     
    scottlpool2003, May 15, 2009 IP
  6. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #6
    Can you post the complete file, exactly as it is on your server right now? As far as I can see, while reading the code, there is nothing telling it to repeat some action twice, and therefore it's hard to say why it actually does it.

    If that is the complete codefile you've shown us there, the problem is in some other part of the code, most likely. You are including lots of files, so maybe there is something there - also this partial code doesn't contain starting tags etc for HTML, so... full codebase, and maybe it's solveable.
     
    PoPSiCLe, May 15, 2009 IP
  7. Sabbir

    Sabbir Banned

    Messages:
    210
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    58
    #7
    you are connecting and selecting the db twice, try removing one from both. i hope it will help. but not sure :)
     
    Sabbir, May 15, 2009 IP
  8. scottlpool2003

    scottlpool2003 Well-Known Member

    Messages:
    1,708
    Likes Received:
    49
    Best Answers:
    9
    Trophy Points:
    150
    #8
    Hi guys thanks for your help. I did have multiple connections to the database. I now only have one but it's still doing the same thing.

    PoPSiCLe, it shouldn't make a difference really because the only files I'm including is the site layout.
     
    scottlpool2003, May 16, 2009 IP
  9. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #9
    So - the code you are showing us is the complete code of the insert? There are no similar variables or anything added before the actual insert into-code? And this code is taken from the live version running on your server? (Back-up files might not always show the exact same code - been there, done that ;) )
     
    PoPSiCLe, May 16, 2009 IP