Rahul Dev Posted January 17, 2011 Share Posted January 17, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/224738-insert-array-variable-into-database/ Share on other sites More sharing options...
Pikachu2000 Posted January 17, 2011 Share Posted January 17, 2011 What happens when you try? Are there any errors returned? Quote Link to comment https://forums.phpfreaks.com/topic/224738-insert-array-variable-into-database/#findComment-1160862 Share on other sites More sharing options...
taquitosensei Posted January 17, 2011 Share Posted January 17, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/224738-insert-array-variable-into-database/#findComment-1160863 Share on other sites More sharing options...
Rahul Dev Posted January 17, 2011 Author Share Posted January 17, 2011 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'> Quote Link to comment https://forums.phpfreaks.com/topic/224738-insert-array-variable-into-database/#findComment-1160875 Share on other sites More sharing options...
Rahul Dev Posted January 17, 2011 Author Share Posted January 17, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/224738-insert-array-variable-into-database/#findComment-1160876 Share on other sites More sharing options...
Pikachu2000 Posted January 17, 2011 Share Posted January 17, 2011 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) . "'"; Quote Link to comment https://forums.phpfreaks.com/topic/224738-insert-array-variable-into-database/#findComment-1160879 Share on other sites More sharing options...
Pikachu2000 Posted January 17, 2011 Share Posted January 17, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/224738-insert-array-variable-into-database/#findComment-1160883 Share on other sites More sharing options...
Rahul Dev Posted January 22, 2011 Author Share Posted January 22, 2011 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()); } Quote Link to comment https://forums.phpfreaks.com/topic/224738-insert-array-variable-into-database/#findComment-1163434 Share on other sites More sharing options...
Pikachu2000 Posted January 22, 2011 Share Posted January 22, 2011 Is your table named literally 'articles(oringinal_text)' ? Quote Link to comment https://forums.phpfreaks.com/topic/224738-insert-array-variable-into-database/#findComment-1163435 Share on other sites More sharing options...
Pikachu2000 Posted January 22, 2011 Share Posted January 22, 2011 On second look, INSERT queries don't take a WHERE clause. Are you trying to INSERT a new record, or UPDATE an existing record? Quote Link to comment https://forums.phpfreaks.com/topic/224738-insert-array-variable-into-database/#findComment-1163436 Share on other sites More sharing options...
Rahul Dev Posted January 22, 2011 Author Share Posted January 22, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/224738-insert-array-variable-into-database/#findComment-1163439 Share on other sites More sharing options...
Rahul Dev Posted January 22, 2011 Author Share Posted January 22, 2011 Everything works fine now, Thnx for the help Pikachu2000 Quote Link to comment https://forums.phpfreaks.com/topic/224738-insert-array-variable-into-database/#findComment-1163467 Share on other sites More sharing options...
Rahul Dev Posted January 22, 2011 Author Share Posted January 22, 2011 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?? Quote Link to comment https://forums.phpfreaks.com/topic/224738-insert-array-variable-into-database/#findComment-1163470 Share on other sites More sharing options...
PFMaBiSmAd Posted January 22, 2011 Share Posted January 22, 2011 How do you know that only article_id is saved? What method are you using to look at what was stored in the database table? Quote Link to comment https://forums.phpfreaks.com/topic/224738-insert-array-variable-into-database/#findComment-1163477 Share on other sites More sharing options...
Rahul Dev Posted January 22, 2011 Author Share Posted January 22, 2011 How do you know that only article_id is saved? What method are you using to look at what was stored in the database table? i manually checked the database, the article id is inserted as i want it but there's nothing in summarized text! Quote Link to comment https://forums.phpfreaks.com/topic/224738-insert-array-variable-into-database/#findComment-1163483 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.