I was trying to connect to my local database through the php code but I get this error:
Syntax error, unexpected '$result' (T_VARIABLE), expecting ',' or ')'
and I don't understand where's the problem. Here's my code:
<?php
$hostname = "localhost";
$username = "root";
$password = "";
$databaseName = "newspage";
$dbConnected = @mysqli_connect($hostname, $username, $password);
$dbSelected = @mysqli_connect($databaseName, $dbConnected);
$query = "INSERT INTO news(titolo, testo, data)VALUES('".$_POST["titolo"]."', '".$_POST["testo"]."', NOW())";
$result = @mysqli_query($query);
if(!$result){
echo("Errore aggiunta news: " . mysqli_error(mysqli $result));
exit();
}
else {
mysqli_close(mysqli $dbConnected);
echo('News caricata!<br><a href="add.php">Clicca qui</a> per aggiungere altre news.<br><a href="edit.php">Clicca qui</a> per apportare modifiche alle news.<br><a href="../index.php">Clicca qui</a> per tornare alla pagina principale.');
}
?>
@when you're still debugging.mysqli_connect()should bemysqli_select_db(), and the arguments are in the wrong order.@) obscures problems with your code and makes debugging issues like this a whole lot more complicated. That's a tool of last resort and should only be used in exceptional circumstances. You should display an error message for the user, log a problem, initiate some kind of retry, or all of these things in conjunction.mysqliis significantly less verbose, making code easier to read and audit, and is not easily confused with the obsoletemysql_queryinterface. Before you get too invested in the procedural style it’s worth switching over. Example:$db = new mysqli(…)and$db->prepare("…")The procedural interface is an artifact from the PHP 4 era whenmysqliAPI was introduced and should not be used in new code.mysqliyou should be using parameterized queries andbind_paramto add user data to your query. DO NOT use string interpolation or concatenation to accomplish this because you have created a severe SQL injection bug. NEVER put$_POST,$_GETor any user data directly into a query, it can be very harmful if someone seeks to exploit your mistake.