Jump to content

Why cant this insert to database?


JJohnsenDK

Recommended Posts

Hey

 

Why doesnt this code not insert the data into the database?

 

<?php
// Goes through all the words in url/website and insert them into the database
function _harvest($url)
{
	   if($this->_checkURL($url))
	   {
		      echo "URL is not valid: $url.";
	   }elseif($data = $this->_getData($url)){
		      $words = preg_split('/[\s,.]+/', $data);
		      array_walk($words, array($this, '_prune'), $words);
		      sort($words);
		      $url_id = $this->_db->getone("SELECT id FROM urls WHERE url = '$url'");
		      if($url_id)
		      {
			         $this->_db->query("DELETE FROM keywords WHERE url_id = '$url_id'");
		      }else{
			         $this->_db->query("INSERT INTO urls SET url = '$url'");
			         $url_id = mysql_insert_id();
		      }

		      $values = "($url_id, '$words[0]')";
		      $numwords = count($words);
		      for($i=1; $i < $numwords; $i++)
		      {
			         $values .= ", ($url_id, '$words[$i]')";
		      }

		      $this->_db->query("INSERT INTO keywords VALUES $values");
	   }   
}
?>

 

i get this error:

Invalid query: 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 '' at line 1

 

*the $this->_db->query is holding mysql_query() in ohter class.

Link to comment
Share on other sites

If you echo out $values before the insert then you should see your problem.

 

Your insert statement should be in this format:

<?php
$sql = "INSERT INTO keywords (url_id, keyword) VALUES ('$url_id', '$keyword')";
?>

 

When you are building your $values string it is all jumbled up.

Link to comment
Share on other sites

well, yes it is jumbled up with a lot of values of the insert query. So it contains for exampel:

 

(1, 'Skotland'), (1, 'Skotland'), (1, 'Skotsk'), (1, 'Slagskibe')

 

and this is what $values prints out when i echo it out. So i cant see the why mysql is getting me the Invalid query error because my insert query would look like this:

 

$this->_db->query("INSERT INTO keywords (url_id, keyword) VALUES (1, 'Skotland'), (1, 'Skotland'), (1, 'Skotsk'), (1, 'Slagskibe')");

 

why is that wrong?

 

Link to comment
Share on other sites

when i put this in my script:

 

$this->_db->query("INSERT INTO keywords (url_id, keyword) VALUES (1, 'Skotland'), (1, 'Skotland'), (1, 'Skotsk'), (1, 'Slagskibe')");

 

it inserts it into the database??? i simply cant see why i cant use that $values variable for this? when it contains the same information?

Link to comment
Share on other sites

You are trying to put 8 values into 2 fields of your database.

 

I think for what you are trying to do you are going to need to do do more than one insert query.

 

So if you change this:

<?php
		      $numwords = count($words);
		      for($i=1; $i < $numwords; $i++)
		      {
			         $values .= ", ($url_id, '$words[$i]')";
		      }

		      $this->_db->query("INSERT INTO keywords VALUES $values");
?>

To something like:

<?php
$numwords = count($words);
		      for($i=1; $i < $numwords; $i++)
		      {
                               $keyword = $words[$i];				
         $this->_db->query("INSERT INTO keywords (url_id, keyword) VALUES ('$url_id', '$keyword')");	     
   }
?>

 

That is assuming that you want the url_id to always be the same, which it is at the moment.

 

Are you sure that url_id is not an auto_increment field?

 

Then you would want:

<?php
$numwords = count($words);
		      for($i=1; $i < $numwords; $i++)
		      {
                               $keyword = $words[$i];				
         $this->_db->query("INSERT INTO keywords (keyword) VALUES ('$keyword')");	     
   }
?>

 

Link to comment
Share on other sites

crap... this gives me the same error as before. I am now using this:

 

<?php
$numwords = count($words);
		for($i=1; $i < $numwords; $i++)
		{
                   $keyword = $words[$i];				
         		   $this->_db->query("INSERT INTO keywords (url_id, keyword) VALUES ('$url_id', '$keyword')");	     
   			}
?>

Link to comment
Share on other sites

Sorry, scrap my last post then, I'm an idiot!

 

This should work

<?php
$values = "($url_id, '$words[0]')";
		      $numwords = count($words);
		      for($i=1; $i < $numwords; $i++)
		      {
			         $values .= ", ('$url_id', '$words[$i]')";
		      }

		      $this->_db->query("INSERT INTO keywords VALUES $values");
?>

Need to have the quotes around $url_id

Link to comment
Share on other sites

<?php

require('db.php');

 

class Harvest_Keywords

{

 

// Private variables

var $_db;

var $_urlarray;

var $_stopwords = array('and', 'but', 'are', 'the');

var $_allowwords = array('c++', 'ado', 'vb');

 

// Construtor

function Harvest_Keywords($urls)

{

$this->_db = new DB_Class('test', 'root', '');

$this->_urlarray = trim($urls);

$this->_urlarray = explode("/n", $this->_urlarray);

}

 

// prune function - checks the words

function _prune($item, $array, $key)

{

$item = strtolower($item);

if(((preg_match("/[^a-z0-9'\?!-]/", $item)) || (strlen($item) > 3) || (in_array($item, $this->_stopwords))) && (!in_array($item, $this->_allowwords)))

{

unset($array[$key]);

}else{

$item = addslashes(preg_match("/[^a-z0-9'-]/", $item));

}

}

 

// Check the url to see if it is correct

function _checkURL($url)

{

return preg_match ("/http:\/\/(.*)\.(.*)/i", $url);

}

 

// Gets the information from the URL with fopen and fread

function _getData($url)

{

$filehandel = fopen($url, 'r');

if(!$filehandel)

{

echo "Could not read the URL.";

$return = FALSE;

}else{

$data = fread($filehandel, 25000);

$close = fclose($filehandel);

$data = strip_tags($data);

$data = str_replace('&nbsp', ' ', $data);

$return = $data;

}

 

return $return;

}

 

// Goes through all the words in url/website and insert them into the database

function _harvest($url)

{

if($this->_checkURL($url))

{

echo "URL is not valid: $url.";

}elseif($data = $this->_getData($url)){

$words = preg_split('/[\s,.]+/', $data);

array_walk($words, array($this, '_prune'), $words);

sort($words);

$url_id = $this->_db->getone("SELECT id FROM urls WHERE url = '$url'");

if($url_id)

{

$this->_db->query("DELETE FROM keywords WHERE url_id = '$url_id'");

}else{

$this->_db->query("INSERT INTO urls SET url = '$url'");

$url_id = mysql_insert_id();

}

 

$values = "($url_id, '$words[0]')";

      $numwords = count($words);

      for($i=1; $i < $numwords; $i++)

      {

        $values .= ", ('$url_id', '$words[$i]')";

      }

}

}

 

// Get the script running by calling harvest() function as many times as there are urls

function process()

{

foreach($this->_urlarray as $url)

{

$this->_harvest($url);

}

}

}

 

?>

 

dont know how to get the colors on without putting it the in

 tags... if i put it in the [code] tags it remove all my tabs and the code is unreadable.

[/code]

Link to comment
Share on other sites

Are you still getting any error because you don't currently have the insert in your script?

 

<?php
         $values = "('$url_id', '$words[0]')";
         
               $numwords = count($words);
         
               for($i=1; $i < $numwords; $i++)
         
               {
         
                     $values .= ", ('$url_id', '$words[$i]')";
         
               }
               //Is this part still in your script?      
               $this->_db->query("INSERT INTO keywords (url_id, keyword) VALUES $values");
?>
[code]

[/code]

Link to comment
Share on other sites

Ok, think this is going to have to be my last attempt because if this doesn't work then I'm lost!

 

But first check that you do have 2 columns in your database and they are called exactly url_id and keyword

<?php
               $numwords = count($words);
         
               for($i=0; $i < $numwords; $i++)
         
               {
         
               $keyword = $words[$i];
               
               $this->_db->query("INSERT INTO keywords (url_id, keyword) VALUES ('$url_id', '$keyword')");
         
               }
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.