I've got this weird problem in getting an error
Undefined variable: sqlConn
And it can't be a connection to DB problem because it would yell another error, whats even stranger is that te require_once thats identical in a file thats in the same directory works just fine. I don't even know what to think. Here's one (WORKING) file:
<?php require_once("connect.php");?>
<html>
<head>...
..........
...<?php
foreach($sqlConn->query('SELECT * FROM permatable') as $row) {
echo "<tr><td>".$row["Art"]."</td><td>".$row["Ime"]."</td><td>".$row["Uvoz"]."</td><td>".$row["Opis"]."</td><td>".$row["Cijena"]."</td></tr>";
}
?>
And the other (Getting an error):
<?php require_once("connect.php");?>
<?php
if(isset($_POST["AddFromDB"], $_POST["ArtNo"])){
addExisting($_POST["ArtNo"]);
}
else if(isset($_POST["AddNew"], $_POST["Art"], $_POST["ImeProizvoda"], $_POST["Dobava"], $_POST["Cijena"])){
newItem($_POST["Art"], $_POST["ImeProizvoda"], $_POST["Dobava"], $_POST["Cijena"]);
}
if(isset($_POST["clearList"])){
clearPermaTB();
}
else if (!isset ($_POST)){
echo "error";
}
//Dodaj postojeci proizvod
function addExisting ($Art) {
echo "5";
//Trazi u bazi
$result_set = $sqlConn->query("SELECT Art, Ime, Uvoz, Opis, Cijena FROM proizvod WHERE Art = '{$Art}'");
So again, both are in the same directory, both use similar code, but for some reason one gets the error and the other doesen't.
Here's a connect file:
//Database Connection
$sqlConn = new mysqli($server, $user, $pass, $name);
if ($sqlConn->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
?>
Am I making an sql syntax mistake?
That's because sqlConn
is out of scope.
You're trying to call it from your function.
You could pass it as an argument to your function or use global
even if it's not a good design
Solution 1:
addExisting(xxx, $sqlConn);
// ...
function addExisting($Art, $sqlConn)
{
// use $sqlConn here
}
Solution 2:
addExisting(xxx);
// ...
function addExisting($Art)
{
global $sqlConn;
// use $sqlConn here
}