BardD BardD - 6 months ago 10
SQL Question

This code for inserting variables into a mysql db as rows doesn't work (PHP)

I'm trying to get variables to input as rows to columns in a mysql database (I'm new to both mysql and php) via php. However, it doesn't seem to work and this test code I put in fails every time. Again I'm new to this, so if I'm making a stupid mistake please point it out nicely.

$conn = new mysqli ("localhost", "root", "", "testdb");
echo "Database connection successful";
echo '<br />';
$test1 = "a";
$test2 = "a";
$test3 = "a";
$test4 = "a";
$test5 = "a";
$query = "INSERT INTO testtable (testcolumn1, testcolumn2, testcolumn3, testcolumn4, testcolumn5) VALUES ('$test1', '$test2', '$test3', '$test4', '$test5')";
if ($conn->query($query) === TRUE) {
echo "passed";
} else {
echo "failed";
}


The database and table and columns are all valid and present, it's something to do with how I'm using the query. Can anyone help?

Answer

The script seems to be working fine. I was able to execute the following script without a problem.

<?php

$conn = new mysqli ("localhost", "newuser", "password", "testdb");
echo "Database connection successful";
echo '<br />';
$test1 = "a";
$test2 = "a";
$test3 = "a";
$test4 = "a";
$test5 = "a";
$query = "INSERT INTO testtable (testcolumn1, testcolumn2, testcolumn3, testcolumn4, testcolumn5) VALUES ('$test1', '$test2', '$test3', '$test4', '$test5');";
if ($conn->query($query) === TRUE) {
    echo "passed";
} else {
    echo "failed";
}

Double check your Database connection.