Jump to content

Insert array variable into database


Rahul Dev

Recommended Posts

Hello Guys,

I want to insert an array variable into the database e.g

 

foreach($html->find('div[class=PostContent]') as $element)
	{
		echo $element;

		$sq = "INSERT INTO articles(original_text) VALUES ('$element') WHERE article_link='$item_url'";
		$result = mysql_query($sql1) or die('Query failed: ' . mysql_error());

	}

 

i want to insert the variable $element into the database but i'm not able to do so for some reason!

$element contains only a paragraph of text. how can i insert the variable $element?

Link to comment
Share on other sites

What happens when you try? Are there any errors returned?

 

Yes i get the foll. error:

 

Query failed: 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 'rel_headline_cmt' width='670px'>

Link to comment
Share on other sites

Don't surround it in single quotes. Try this

$sq = "INSERT INTO articles(original_text) VALUES ('".$element."') WHERE article_link='".$item_url."'";

 

single quotes don't parse and would insert the value "$element" instead of the contents of it.

 

I tried, i still get the error

Link to comment
Share on other sites

The data isn't escaped, and the single quotes are breaking the syntax. You need to run it through mysql_real_escape_string().

$sq = "INSERT INTO articles(original_text) VALUES ('" . mysql_real_escape_string($element) . "') WHERE article_link='" . mysql_real_escape_string($item_url) . "'";

Link to comment
Share on other sites

Variables will be interpolated within single quotes when inside a double-quoted string.

 

<?php
$field1 = 'name';
$field2 = 'address';
$table = 'users';
$array['index'] = 'Frank';
$num = 10;

$string1 = "SELECT `$field1`, `$field2` FROM `$table` WHERE `$field1` = '{$array['index']}' LIMIT $num";
// The strings above and below produce identical output.  //
$string2 = "SELECT `" . $field1 . "`, `" . $field2 . "` FROM `" . $table . "` WHERE `" . $field1 . "` = '" . $array['index'] . "' LIMIT " . $num;

echo "<br>String1: $string1<br>String2: $string2";
?>
Returns:
String1: SELECT `name`, `address` FROM `users` WHERE `name` = 'Frank' LIMIT 10
String2: SELECT `name`, `address` FROM `users` WHERE `name` = 'Frank' LIMIT 10

Link to comment
Share on other sites

I'm still not able to solve the problem. I tried mysql_real_escape but now i get this error:

 

Query failed: 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 'WHERE article_link='http://www.lexpress.mu/story/20263-cour-supreme-les-accusati' at line 1

Any solution please?

foreach($html->find('td[class=rel_headline_cmt]') as $element)
	{
		echo $element;
		$sql1 = "INSERT INTO articles(original_text) VALUES ('" . mysql_real_escape_string($element) . "') WHERE article_link='" . mysql_real_escape_string($item_url) . "'";
		$result1 = mysql_query($sql1) or die('Query failed: ' . mysql_error());

	}

Link to comment
Share on other sites

On second look, INSERT queries don't take a WHERE clause. Are you trying to INSERT a new record, or UPDATE an existing record?

 

Actually its an update, so my sql should be like this:

$sql1 = "UPDATE articles set original_text = '" . mysql_real_escape_string($element) . "' WHERE article_link='" . mysql_real_escape_string($item_url) . "'";
$result1 = mysql_query($sql1) or die('Query failed: ' . mysql_error());

right?

but is still get the same error:

Query failed: 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 'WHERE article_link='http://www.lexpress.mu/story/20263-cour-supreme-les-accusati' at line 1

Link to comment
Share on other sites

i have another similar problem, i cannot save the variable summary into my db

 

$summary = SummarizeArticle($item_id);
			$sql2 = "INSERT INTO summarization (article_id, summarized_text) VALUES ('$item_id', '" . mysql_real_escape_string($summary) . "')";
			$result2 = mysql_query($sql2) or die('Query failed: ' . mysql_error());
			echo $summary;

when i echo it,it is displayed correctly but when i save it it doesnt work! ony article_id is saved!

any1 can help please??

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.