0

I've got a table (grabbed from another website using php) but now I want to import it's content into my database (instead of just printing out the table).

This is the printout (https://i.sstatic.net/NnbAo.png) from the table using the following code:

<?php
$uitvoer = getTable();
echo "<table">;
foreach ($uitvoer as $row) {
    echo "<tr>";
    foreach ($row as $col) {
        echo "<td>" . $col . "</td>";
    }
echo "</tr>";
}
echo"</table>";
?>

And when I try this nothing happens to my database:

<?php
$con = mysqli_connect("mysql1.000webhost.com","a2902119_lowavol","pswd")
$uitvoer = getTable();
foreach ($uitvoer as $row) {
    foreach ($row as $col) {
        $query += "'" . $col . "', ";
    }
$sql="INSERT INTO test (cel1, cel2, cel3, cel4, cel5, cel6, cel7, cel8, cel9) VALUES ($query)";
mysqli_query($con, $sql);
}
?>

I have little to no knowledge about PHP. Thanks for helping me!

3
  • possible duplicate of filling a database with html table content Commented Sep 19, 2013 at 8:32
  • You're close, you just have to remove the extra comma at the end of the VALUES list. Commented Sep 19, 2013 at 8:33
  • Since you're using mysqli, it would be better to use a prepared statement instead of concatenating strings. Commented Sep 19, 2013 at 8:34

4 Answers 4

1

The concatenation operator for PHP is (.) and not (+)

  <?php
    $con = mysqli_connect("mysql1.000webhost.com","a2902119_lowavol","pswd")
    $uitvoer = getTable();
    foreach ($uitvoer as $row) {
       $query = array();
       $insert = "";
       $insert_values = "";
       foreach ($row as $col) {
           $query[] = $col;
       }
       $insert_values = implode("','", $query) ;
       $insert = "'".$insert_values."'";
       $sql = "INSERT INTO test (cel1, cel2, cel3, cel4, cel5, cel6, cel7, cel8, cel9) VALUES ($insert)";
       mysqli_query($con, $sql);
  }
?>
Sign up to request clarification or add additional context in comments.

4 Comments

works! thanks just had to use the $querry[] = strip_tags($col); instead of $querry[] = $col; to remove html tags before entering them in the database! AWESOME
happy to have helped!
is there a way to put the first 5 rows in table A and the last 5 rows in table B if there were 10 tables in total? So just splitting the number of rows in two and deviding the first half and second half over two different tables?
10 tables or 10 rows?
1

your query will look like this:

INSERT INTO test (....) VALUES (val, val, val, )

which is obviously a syntax error... try and add the fields to an array instead:

foreach ($uitvoer as $row) {
    $fields = array();
    foreach ($row as $col) {
        $fields[] = $col; // TODO: add escaping!
    }
// now you can build the values list with implode
$values = implode(', ', $fields);
$sql="INSERT INTO test (...) VALUES ($values)";

See the docs about implode() for more info.

3 Comments

i'm almost there thanks! but in the echo of the prepared sql statement there are a lot of returns i'm thinking this could be the reason no data is in the database yet? see link thomasdemarez.site88.net/heren1kfetch.php
try $fields[] = trim($col);, although those returns should not be a problem.
Looking at the source though, your values are containing HTML!
0

Use a prepared statement:

$sql="INSERT INTO test (cel1, cel2, cel3, cel4, cel5, cel6, cel7, cel8, cel9) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
$stmt = mysqli_prepare($con, $sql);
mysqli_stmt_bind_param($stmt, "sssssssss", $row[0], $row[1], $row[2], $row[3], $row[4], $row[5], $row[6], $row[7], $row[8]);
foreach ($uitvoer as $row) {
    mysqli_stmt_execute($stmt);
}

1 Comment

doesn't seem to work.. I get two errors when loading the page 1. Warning: mysqli_stmt_execute() expects parameter 1 to be mysqli_stmt, boolean given in /home/a2902119/public_html/heren1kfetch.php on line 22 2. Warning: mysqli_stmt_bind_param() expects parameter 1 to be mysqli_stmt, boolean given in /home/a2902119/public_html/heren1kfetch.php on line 20
-1

Try echo-ing $sql. I think there is a comma in the end.

Use

$query = substr($query, 0, strlen($query) - 1)

in order to remove the last character of $query

(edited the command)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.