How to Inserting data with php-curl - php

I would like to print the links of the images in the database.
https://www.yelp.com/biz_photos/zesty-gyros-and-deli-grand-rapids
I connected using CURL. I took the links to the pictures using Preg_match_all and listed them as "FOR". But I get the error in the INSERT process to the database.
Parse the data
preg_match_all('#<div class="photo-box photo-box--interactive" data-photo-id=(.*?)>#si', $site, $mydata);
for ($a=0; $a<count($mydata[1]); $a++) {
echo $mydata[1][$a].'<br>';
}
25 results are listed below. But I couldn't do mysql insert.
mysqli_query($link,"INSERT INTO myTable (field) VALUES ($mydata[1][$a])") or die(mysqli_error($link));

use single quotations marks around your $mydata[1][$a] and put in mysqli_real_escape_string function.
$value = mysqli_real_escape_string($link, $mydata[1][$a]);
mysqli_query($link,"INSERT INTO myTable (field) VALUES ('".$value."')") or die(mysqli_error($link));

Related

Not all objects showing on the array

I created a table with two columns (ID and username) using PHPMyAdmin and added 1 row from the same interface. Then using PHP I added multiple rows. On PHPMyAdmin, I see the new added rows --the code I used to add the new rows is below. But when I retrieve the array on the site with the code below, I only see the very first row I added using PHPMyAdmin.
Any idea as to why and also how can I get all the data on the array?
$query = "INSERT INTO `allusers` (`ID`, `Username`)
VALUES (NULL, 'abracadabra')";
if ($result = mysqli_query($link, $query)){
print_r ($row = mysqli_fetch_array($result));
mysqli_fetch_array gives you, in array form, the column data of the "next" row (where "next" is determined by a cursor internal to your resultset object $result).
You should call it subsequent times to get subsequent rows (usually in a loop).
You're only doing it once, so you only get one row.
Read the manual page for this function for more information and examples. But, for now:
if ($result = mysqli_query($link, $query)){
while ($row = mysqli_fetch_array($result))
print_r($row);
}
It's evident from your use of $row = inside the print_r expression that you did in fact take an example like this, but replaced the loop with a single print_r call for some reason.

php insert data from fetch array to other table on version 5.4

I have moved to IIS 8 in PHP 5.4. I am trying to collect data from a table and insert them to a different one, i know my code is correct, but seems to be not working, probably because of the php version, can anyone help me?
here's my code
$query = odbc_exec($conn, "SELECT * FROM member");
while($rows = odbc_fetch_array($query)) {
$querystring = "INSERT INTO oldusers (username, password, regdate) VALUES ('$rows['userid']', '$rows['passwd']', '$rows['registdate']')";
$query2 = odbc_exec($conn, $querystring);
odbc_free_result($query2);
//echo $rows['userid']." ".$rows['passwd']." ".$rows['registdate']."<br>";
}
thanks in advance.
instead trying to insert one by one record, better to insert like below:
INSERT INTO oldusers (username, password, regdate) SELECT userid,passwd,registdate FROM member
for more information :http://dev.mysql.com/doc/refman/5.5/en/insert-select.html
You're placing $rows['passwd'] inside of a double-quoted string. Instead you should do:
$str = "some sql $rows[passwd] rest of sql"; // notice the absence of single quotes
or:
$str = "some sql {$rows['passwd']} rest of sql";
or (I think this way is most readable):
$str = 'some sql' . $rows[passwd] . ' rest of sql';
If your column contains text you'll need to add surrounding single quotes where necessary.
Having said all that, you should instead use parameterized queries (if your database supports it) as it's safer (from SQL injection). If that's unavailable you will at the very least need to escape the data before concatenating it to the string.

Text area values exploded and submitted to MySQL database

if(isset($_POST['usersadded'])){
$value = $_POST['usersadded'];
$lines = explode("\n", $value);
foreach ($lines as $line) {
mysql_query("INSERT INTO Users_$support (Users) VALUES ('$line')");
};
I have a valid connection to the database already, so it's not that that's wrong. But it never submits anything :(
$support is a number, e.g. 19.
I would always suggest using this SQL syntax when inserting something with PHP variables inside.
mysql_query("
INSERT INTO
`table_name`
SET
`field` = '" . $field_variable . "'
");
The error might appear when you don't phrase your PHP variables correctly inside the query string.
Try this
mysql_query("INSERT INTO Users_$support (Users) VALUES (\"'\".$line.\"'\")");

unable to insert into mysql database using php

$db = mysql_connect("localhost","root","123");
mysql_select_db("website_categorization") or die("\n error selecting database" );
$keyword_array = preg_split('/[\s,]+/', $tag);
foreach($keyword_array as $tag1)
{
mysql_query("INSERT INTO category_keyword(ID_Category, Keyword) VALUES(2,$tag1)");
}
echo "\nAffected rows are ".mysql_affected_rows()."\n";
mysql_close($db);
Can u tell me what is the problem with this code??...I intend to insert rows into the category_keyword table from an array $keyword_array. I get errors "Affected rows are -1" and insertion does not work
You should quote and escape string values.
You should also handle errors, to be notified of them.
You should also write distinct statements, to be able to read your code later (as well as let others to read it).
$tag1 = mysql_real_escape_string($tag1);
$sql = "INSERT INTO category_keyword(ID_Category, Keyword) VALUES(2,'$tag1')";
mysql_query($sql) or trigger_error(mysql_error()." in ".$sql);
insert multiple rows via a php array into mysql
You need to encapsulte the string $tag in a query, otherwise mysql will think its a column name
mysql_query("INSERT INTO category_keyword(ID_Category, Keyword) VALUES(2,'".mysql_real_escape_string($tag1)."')");
You should quote and escape your string columns
$tag1 =
mysql_real_escape_string($tag1);
mysql_query("INSERT INTO
category_keyword(ID_Category, Keyword)
VALUES(2,'$tag1')");
You should also handle the mysql query errors to know why the query get failed. With the current code you never know why it is failing.It is better to handle mysql errors.
mysql_query('Your query') or trigger_error(mysql_error());
You can use this:
mysql_query("INSERT INTO category_keyword SET ID_Category=2, Keyword=".$tag1.");
Better syntax to understand :)

using foreach loop for multiple MySQL inserts

I'm using the following snippet to break up an string array, then insert them into the database.
//split tags into individual words
$tag_array = explode(',', $tags);
foreach($tag_array as $tag){
addslashes($tag);
echo $tag." ";
$addTagsQuery = "INSERT INTO `my_blog`.`tags` (`id`, `name`) VALUES
('".$id."', '".$tag."');";
$tagsResult = $db->query($addTagsQuery);
if($tagsResult){
echo "tag added <br />";
}
else {
echo "tag was not added <br />";
}
}
My problem lies within a scenario where multiple tag (strings) are submitted. Unfortunately, only the first string in the array is inserted. Any insight as to why only the first string in the array is inserted into the MySQL database would be appreciated.
$id is not being incremented in the loop. Chances are you are getting a duplicate error, but for whatever reason it is not telling you (poor error handling?).
$addTagsQuery = "INSERT INTO `my_blog`.`tags` (`name`) VALUES
('".$tag."');";
If the ID is auto_incrementing, just omit and it will handle that for you.
You should use an auto-incrementing id instead of setting the id manually.
You don't need to run multiple insert statements. You can do it in one statement:
INSERT INTO my_blog.tags (name) VALUES ('tag1'), ('tag2')
The function addslashes doesn't modify the string so the way you are using it will have no effect.
You should use bind parameters instead of escaping strings.
$id is the id of the blog entry the tags are submitted for? Do you maybe have turned ID into a primary key or otherwise unique? That could cause the problem.
Try it like this:
$tag_array = explode(',', $tags);
$stmt = $db->prepare("INSERT INTO my_blog.tags (id, name) VALUES (?,?)");
foreach($tag_array as $tag){
if ($stmt->execute(Array($id, $tag))){
echo "tag added <br />";
}
else{
echo "tag was not added <br />";
}
$stmt->closeCursor();
}

Categories