Click here to Skip to main content
15,896,118 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi Garbel,

I found your article "Simple Way to Convert HTML Table Data into PHP Array" and it happened to create an array beautifully with a project I was working on. I am trying to enter this array into my mysql db. Unfortunately without much success. I need a counter as the html file may have more or less columns and rows and have had a hard time figuring out how to do so. Any input you could give me would be greatly appreciated. I will attach what I have written in code so far so you have an idea of where I am at.

Thanks!

What I have tried:

PHP

<?php
$servername = "localhost";
$username = "user";
$password = "pass";
$database = "ticker";

// Create connection
$conn = new mysqli($servername, $username, $password, $database);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";

$table = file_get_contents("watchlist.html");
$dom = new DOMDocument;
@$dom->loadHTML($table);
$tds = $dom->getElementsByTagName("td"); 
$tags = $dom->getElementsByTagName("th");
$symbol = $tags->item(0)->nodeValue;
$vwap = $tags->item(1)->nodeValue;
$last = $tags->item(2)->nodeValue;
$trend = $tags->item(3)->nodeValue;
$bid = $tags->item(4)->nodeValue;
$ask = $tags->item(5)->nodeValue;
$volume = $tags->item(6)->nodeValue;
$markchg = $tags->item(7)->nodeValue;
$markpcnt = $tags->item(8)->nodeValue;
$shares = $tags->item(9)->nodeValue;
$marketcap = $tags->item(10)->nodeValue; 
$ttmsqz = $tags->item(11)->nodeValue;

foreach($tags as $NodeHeader) 
	{
		$aDataTableHeaderHTML[] = trim($NodeHeader->textContent);
	}
	//print_r($aDataTableHeaderHTML); die();

$i = 0;
$j = 0;
	foreach($tds as $sNodeDetail) 
	{
		$aDataTableDetailHTML[$j][] = trim($sNodeDetail->textContent);
		$i = $i + 1;
		$j = $i % count($aDataTableHeaderHTML) == 0 ? $j + 1 : $j;

 
}
print_r($aDataTableDetailHTML); die();

 //this is where your sql goes
 $query = "INSERT INTO riser (symbol, vwap, last, bid, ask, volume, markchg, markpct, shares, marketcap, ttmsqz) VALUES ('$aDataTableDetailHTML[0][0]', '$aDataTableDetailHTML[0][1]', '$aDataTableDetailHTML[0][2]', '$aDataTableDetailHTML[0][3]', '$aDataTableDetailHTML[0][4]', '$aDataTableDetailHTML[0][5]', '$aDataTableDetailHTML[0][6]', '$aDataTableDetailHTML[0][7]', '$aDataTableDetailHTML[0][8]', '$aDataTableDetailHTML[0][9]', '$aDataTableDetailHTML[0][10]', '$aDataTableDetailHTML[0][11]')";
mysql_query($query);



$conn->close();
?>
Posted
Updated 27-Aug-18 0:03am

You have to execute the SQL insert for each row. So just move the operation into the foreach loop:
PHP
foreach ($tds as $sNodeDetail) 
{
    $row[] = trim($sNodeDetail->textContent);
    $query = "INSERT INTO riser (symbol, vwap, last, bid, ask, volume, markchg, markpct, shares, marketcap, ttmsqz) VALUES ('$row[0]', '$row[1]', '$row[2]', '$row[3]', '$row[4]', '$row[5]', '$row[6]', '$row[7]', '$row[8]', '$row[9]', '$row[10]', '$row[11]')";
    if ($conn->query($query) !== TRUE)
    {
        // Report error here
    }

    // Or better using a parametrised query
    if ($stmt = $conn->prepare("INSERT INTO riser (symbol, vwap, last, bid, ask, volume, markchg, markpct, shares, marketcap, ttmsqz) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"))
    {
        $stmt->bind_param("ssssssssssss", $row[0], $row[1], $row[2], $row[3], $row[4], $row[5], $row[6], $row[7], $row[8], $row[9], $row[10], $row[11]);
        if ($stmt->execute() == FALSE)
        {
            // Handle error here
        }
        $stmt->close();
    }
}
 
Share this answer
 
If you have problems with a CodeProject article, then please post your question in the forum at the end of the article. Only then will the author get notified of your problem.
 
Share this answer
 
PHP
$query = "INSERT INTO riser (symbol, vwap, last, bid, ask, volume, markchg, markpct, shares, marketcap, ttmsqz) VALUES ('$aDataTableDetailHTML[0][0]', '$aDataTableDetailHTML[0][1]', '$aDataTableDetailHTML[0][2]', '$aDataTableDetailHTML[0][3]', '$aDataTableDetailHTML[0][4]', '$aDataTableDetailHTML[0][5]', '$aDataTableDetailHTML[0][6]', '$aDataTableDetailHTML[0][7]', '$aDataTableDetailHTML[0][8]', '$aDataTableDetailHTML[0][9]', '$aDataTableDetailHTML[0][10]', '$aDataTableDetailHTML[0][11]')";

Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
How can I explain SQL injection without technical jargon? - Information Security Stack Exchange[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900