How to save price with product name in database mysql

Hell
I using PHP Simple HTML DOM Parser .

Now I write the mini script .

This script find the products online shopping with price.

<?php


include_once('simple_html_dom.php');
// Create DOM from URL
header("Refresh: 300;");
$html = file_get_html('http://www.example.com/Product/DKP-33708/Kenwood-KMM023-Food-Processor/%D8%BA%D8%B0%D8%A7%D8%B3%D8%A7%D8%B2-%DA%A9%D9%86%D9%88%D9%88%D8%AF-KMM023');
$con = mysql_connect ("localhost","root", "");
                mysql_select_db("price", $con);
   if (!$con)
                {
  die(mysql_error());
   }else {

// Find all article blocks
foreach($html->find('div#content') as $article) {
                $item['title']   = $article->find('h1', 0)->plaintext;
                $item['intro']  = $article->find('h3', 0)->plaintext;
                $item['details'] = $article->find('span.finalprice', 0)->plaintext;
                $articles[] = $item;
}
   $articles = array();
mysql_select_db ( "price", $con );
"CREATE TABLE Dadoo(id INT NOT NULL AUTO INCREMENT,title VARCHAR(30),intro INT NOT NULL,details VARCHAR(30) NOT NULL,PRIMARY KEY(id)) ENGINE=MyISAM" or die(mysql_error());
$debugquery = mysql_query("INSERT INTO Dadoo (title, intro, details) VALUES ('$articles')");
                if (!$debugquery)
   {
   die(mysql_error());
  }
}
echo
$title="<b>&#1606;&#1575;&#1605; &#1605;&#1581;&#1589;&#1608;&#1604;:</b>". $item['title'] . "</br>  ";
echo
$dec="<b>&#1578;&#1608;&#1590;&#1740;&#1581;&#1575;&#1578;:</b> ". $item['intro'] . "</br> ";
echo
$pr="<b>&#1602;&#1740;&#1605;&#1578;:</b> ". $item['details'] . " </br> ";

//to fetch all images from a webpage
$images = array();
foreach($html->find('img') as $img) {
$images[] = $img->src;
}
echo " $images[29]";

?> 

When run the top code I giving this error

Table ‘price.dadoo’ doesn’t exist

how fix it?

Welcome to the forums :slight_smile:

create the DADOO table beforehand, make sure it IS already in your system before trying to insert data into it.

Please be aware that the mysql_* extension is now deprecated as of the current version of PHP and will very likely be removed from the next 5.x version and will likely not be in PHP 6.x (when it eventually is released). You should migrate over to either the mysqli_* extension or to PDO. PDO is a better choice as it doesn’t tie you down so much to a particular database server software.

Once you have migrated you should use Prepared Statements to prevent SQL Injection attacks. Have a read of this article from the PHP manual, it shows how to use prepared statements with PDO and also explains the principle.

As noted, move away from mysql.
Assuming you will have permission to create a DB table with php with your host, then it is possible.
As to the problem, you are not executing a query.

mysql_query("CREATE TABLE Dadoo(id INT NOT NULL AUTO INCREMENT,title VARCHAR(30),intro INT NOT NULL,details VARCHAR(30) NOT NULL,PRIMARY KEY(id)) ENGINE=MyISAM") or die(mysql_error());

OR

mysql_query("CREATE table IF NOT EXISTS Dadoo(id INT NOT NULL AUTO INCREMENT,title VARCHAR(30),intro INT NOT NULL,details VARCHAR(30) NOT NULL,PRIMARY KEY(id)) ENGINE=MyISAM") or die(mysql_error());

2 question :
1- why it cant’t make auto table in db?
2- when make Dadoo table in db give this error:::
Column count doesn’t match value count at row 1

Thank’s

when create query

mysql_query("CREATE table IF NOT EXISTS Dadoo(id INT NOT NULL AUTO INCREMENT,title VARCHAR(30),intro INT NOT NULL,details VARCHAR(30) NOT NULL,PRIMARY KEY(id)) ENGINE=MyISAM") or die(mysql_error());  

give this error

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 ‘AUTO INCREMENT,title VARCHAR(30),intro INT NOT NULL,details VARCHAR(30) NOT NULL’ at line 1

You need the underscore between AUTO INCREMENT. Also adding back tics for table name and fields might help.

mysql_query("CREATE table IF NOT EXISTS `Dadoo` (`id` INT NOT NULL AUTO_INCREMENT, `title` VARCHAR(30), `intro` INT NOT NULL, `details` VARCHAR(30) NOT NULL, PRIMARY KEY(`id`)) ENGINE=MyISAM") or die(mysql_error());

very good.

Thank’s.

question:

with each refreshing page. the same value stored in new row … not updating and change value.

example:

1 Lenovo Ideatab S6000 - 16GB 859,000 تبلت لنوو آيديا تب اس 6000 - 16 گيگابايت
2 Lenovo Ideatab S6000 - 16GB 859,000 تبلت لنوو آيديا تب اس 6000 - 16 گيگابايت
3 Lenovo Ideatab S6000 - 16GB 859,000 تبلت لنوو آيديا تب اس 6000 - 16 گيگابايت

now I have after refresh webpage . update ’ intro ’ filed of dadoo table. how can?

example::
Lenovo Ideatab S6000 - 16GB 859,000 تبلت لنوو آيديا تب اس 6000 - 16 گيگابايت

after refreshing

Lenovo Ideatab S6000 - 16GB 6700,000 تبلت لنوو آيديا تب اس 6000 - 16 گيگابايت

after refreshing

Lenovo Ideatab S6000 - 16GB 100000,000 تبلت لنوو آيديا تب اس 6000 - 16 گيگابايت

Set $articles as array BEFORE build.

   $articles = array();
// Find all article blocks
foreach($html->find('div#content') as $article) {
                $item['title']   = $article->find('h1', 0)->plaintext;
                $item['intro']  = $article->find('h3', 0)->plaintext;
                $item['details'] = $article->find('span.finalprice', 0)->plaintext;
                $articles[] = $item;
}

I’m not sure what your articles array looks like, but you might want to build it like this. (untested)

// Find all article blocks
$articles = array();
foreach($html->find('div#content') as $article) {
                $title   = $article->find('h1', 0)->plaintext;
                $intro  = $article->find('h3', 0)->plaintext;
                $details = $article->find('span.finalprice', 0)->plaintext;
                $articles[] = "('$title','$intro','$details')";
}

$sql = "INSERT INTO Dadoo (title, intro, details) VALUES";
$sql .= implode(",",$articles);
$debugquery = mysql_query($sql);
if (!$debugquery)
{
	die(mysql_error());
}

Thanks .

now how update the field

You’ll need a record id in a display form where you can edit that record. Based on this id, you would update the record.

How far into your project are you? You should really consider PDO for making connection and binding all values before executing queries.

I can’t learn pdo because it’s difficult …

how can update and echo data from table

$articles = array();
mysql_select_db ( "price", $con );
"CREATE TABLE Dadoo(id INT NOT NULL AUTO INCREMENT,title VARCHAR(30),intro INT NOT NULL,details VARCHAR(30) NOT NULL,PRIMARY KEY(id)) ENGINE=MyISAM" or die(mysql_error());
$debugquery = mysql_query("INSERT INTO Dadoo (title, intro, details) VALUES ('$articles')");
                if (!$debugquery)
   {
   die(mysql_error());
  }