My script is not inserting into my database and is producing a strange error.

Discussion in 'PHP' started by meroving, Mar 25, 2009.

  1. #1
    My script is designed to download source code from other websites and insert it into a database, however the script is failing to insert or update...

    
    <?php
      include("config/config.php");
      include("config/db_connect.php");
      
      function FetchUrlList ($db) {
          $query = "select url_id, url, category_id, vendor_id";
          $query .= " from scraper_url";
          $query .= " where active = 1";
            $result = $db->query($query);
            $num_results = $result->num_rows;
          for ($i=0; $i<$num_results; $i++) {
            $row = $result->fetch_assoc();
            $url_id = $row['url_id'];
            $url = $row['url'];
            $category_id = $row['category_id'];
            $vendor_id = $row['vendor_id'];
            //build arrays
                $urllist[$url_id]['url'] = $url;
                $urllist[$url_id]['category_id'] = $category_id;
                $urllist[$url_id]['vendor_id'] = $vendor_id;
          }
          return $urllist;             
      }
      
      function DownloadCategorySourceCode ($db, $urllist) {
      //progress bar variables
      $iteration = 1;
      $width = 0;
      $total_iterations = sizeof($urllist);
      $width_per_iteration = 500 / $total_iterations;     
          while (list($key) = each($urllist)) {
              $vendor_id = $urllist[$key]['vendor_id'];                    
            //select vendor website
            $query = "select vendor_website";
            $query .= " from scraper_vendor";
            $query .= " where vendor_id = '$vendor_id'";
            $result = $db->query($query);
            $row = $result->fetch_assoc();
            $vendor_website = $row['vendor_website'];
    		
            
            //select category name
            $category_id = $urllist[$key]['category_id'];
            $query = "select category_name";
            $query .= " from jos_vm_category";
            $query .= " where category_id = '$category_id'";
            $result = $db->query($query);
            $row = $result->fetch_assoc();
            $category_name = $row['category_name'];
           
    		
            //download source code
            $urllisthtml[$key] = file_get_contents($vendor_website . $urllist[$key]['url']); 
            
            //check for update or insert into the database
            $source_code = $urllisthtml[$key];
            $query = "select url_id from scraper_category_sc where url_id = '$key'";
            $result = $db->query($query);
            $num_results = $result->num_rows;
            echo "Number of results: $num_results";
                if ($num_results > 0) {
                    $query = "update scraper_category_sc set";
                    $query .= " source_code = '".$source_code."'";
                    $query .= " where url_id = '".$key."'";				
    				if ($db->query($query) === TRUE) {
        				echo "Record Updated...";
    					} else {
    					echo "Unable to update record: " . mysqli_error($db) ."";
    					}
                } else {
                    $query = "insert into scraper_category_sc set";
                    $query .= " url_id = '".$key."',";
                    $query .= " source_code = '".$source_code."'";
                    if ($db->query($query) === TRUE) {
        				echo "Record Inserted...";
    					} else {
    					echo "Unable to insert record: " . mysqli_error($db) ."";
    					}
                } 
            //update progress bar    
            echo '<div class="progress_wrapper"><div class="progress_bg" style="width:500px;"><div class="progress" style="width:' . $width . 'px;"></div></div></div><br>';
            echo '<div class="progress_wrapper"><div class="progress_text">Finished Downloading Source Code: ' . $iteration . ' of ' . $total_iterations . " - " . $category_name . '</div></div>';
            $width += $width_per_iteration;
            $iteration++;
            ob_flush();
            flush();
          }
          //finalize progress bar
          echo '<div class="progress_wrapper"><div class="progress_bg" style="width:500px;"><div class="progress" style="width:' . $width . 'px;"></div></div></div><br>';
          echo '<div class="progress_wrapper"><div class="progress_text">Finished Downloading/Updating All Source Code</div></div>';
      }
    
    ?>
    <h2><a href="stepcrawl.php?ext_cat_sc=1">Step 1</a>: Extract Category Source Code</h2>
    <h2><a href="stepcrawl.php?parse_cat_sc=1">Step 2</a>: Parse Source Code for Products</h2>
    <h2><a href="stepcrawl.php?ext_detail_sc=1">Step 3</a>: Extract Product Details Source Code</h2>
    <h2><a href="stepcrawl.php?parse_detail_sc=1">Step 4</a>: Parse Source Code for Product Details</h2>
    <h2><a href="stepcrawl.php?download_images=1">Step 5</a>: Download Images</h2>
    <h2><a href="stepcrawl.php?finishing_touches=1">Step 6</a>: Finishing Touches</h2>
    <h2><a href="stepcrawl.php?update_gd=1">Step 7</a>: Update WebSite</h2>
    
    
    <?php
    if (isset($_GET['ext_cat_sc'])) {    
    $urllist = FetchUrlList($db);
    $urllisthtml = DownloadCategorySourceCode($db, $urllist);
    }
    ?>
    
    Code (markup):
    The error produced is:

    Number of results: 1Unable to update record: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Please select an option'); return false;} else { form.action = 'cate' at line 1

    Finished Downloading Source Code: 1 of 44 - Category1
    Number of results: 1Unable to update record: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Please select an option'); return false;} else { form.action = 'cate' at line 1

    Finished Downloading Source Code: 2 of 44 - Category2
    Number of results: 0Unable to insert record: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Please select an option'); return false;} else { form.action = 'cate' at line 1


    There is nothing in my script has nothing in it that says "Please select an option'); return false;} else { form.action = 'cate'"

    I am halfway new to PHP and I am at a loss...can someone help?
     
    meroving, Mar 25, 2009 IP
  2. steelaz

    steelaz Peon

    Messages:
    47
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    It looks like you're not escaping quotes in your sql query, try using mysql_real_escape_string($source_code)
     
    steelaz, Mar 25, 2009 IP
  3. meroving

    meroving Peon

    Messages:
    135
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    That was it, thank you very much!
     
    meroving, Mar 25, 2009 IP