-1
I’m scheduling a product management page, and I want to include a search engine to give users a search engine that can save time while they’re looking for the desired product, but after following a tutorial for the same I am encountering two errors:
Warning: mysqli_query() expects at least 2 Parameters, 1 Given in C: xampp htdocs administracao produtos index.php on line 82
Warning: mysqli_fetch_array() expects Parameter 1 to be mysqli_result, null Given in C: xampp htdocs administracao produtos index.php on line 84
The PHP code is as follows::
<!-- Pesquisar PHP ------------->
<?php
if (isset($_POST['submit'])) {
if (isset($_GET['go'])) {
if (preg_match("/[A-Z | a-z]+/", $_POST['name'])) {
$name = $_POST['name'];
//connect to the database
$db = mysqli_connect("localhost", "root", "") or die('I cannot connect to the database because: ' . mysqli_error());
//-select the database to use
mysqli_select_db($db, "pap_database") or die("cannot select DB");
$sql = "SELECT idprodutos, nome, ano_atual FROM produtos WHERE nome LIKE '%" . $name . "%' OR ano_atual LIKE '%" . $name . "%'";
//-run the query against the mysql query function
$result = mysqli_query($sql);
//-create while loop and loop through result set
while ($row = mysqli_fetch_array($result)) {
$name = $row['nome'];
$ano = $row['ano_atual'];
$ID = $row['idprodutos'];
//-display the result of the array
echo "<ul>\n";
echo "<li>" . "<a href=\"index.php?idprodutos=$ID\">" . $name . " " . $ano . "</a></li>\n";
echo "</ul>";
}
} else {
echo "<p>Please enter a search query</p>";
}
}
}
?>
<!------------------------------>
HTML code:
<form action="index.php?go" method="post" id="searchform">
<input type="text" name="name" placeholder="Pesquisa etc" />
<input type="submit" name="submit" value="Search" />
<?php
//end of search form script
if (isset($_GET['by'])) {
$letter = $_GET['by'];
//connect to the database
$db = mysqli_connect("localhost", "root", "") or die('I cannot connect to the database because: ' . mysqli_error());
//-select the database to use
$mydb = mysqli_select_db("pap_database");
//-query the database table
$sql = "SELECT idprodutos, nome, ano_atual FROM produtos WHERE nome LIKE '%" . $letter . "%' OR ano_atual LIKE '%" . $letter . "%'";
//-run the query against the mysql query function
$result = mysqli_query($sql);
//-count results
$numrows = mysqli_num_rows($result);
echo "<p>" . $numrows . " results found for " . $letter . "</p>";
//-create while loop and loop through result set
while ($row = mysqli_fetch_array($result)) {
$name = $row['nome'];
$ano = $row['ano_atual'];
$ID = $row['idprodutos'];
//-display the result of the array
echo "<ul>\n";
echo "<li>" . "<a href=\"search.php?id=$ID\">" . $name . " " . $ano . "</a></li>\n";
echo "</ul>";
}
}
//end of our letter search script
if (isset($_GET['idprodutos'])) {
$contactid = $_GET['idprodutos'];
//connect to the database
$db = mysqli_connect("localhost", "root", "") or die('I cannot connect to the database because: ' . mysql_error());
//-select the database to use
$mydb = mysqli_select_db("pap_database");
//-query the database table
$sql = "SELECT * FROM produtos WHERE idprodutos=" . $contactid;
//-run the query against the mysql query function
$result = mysqli_query($sql);
//-create while loop and loop through result set
while ($row = mysqli_fetch_array($result)) {
$name = $row['nome'];
$ano = $row['ano_atual'];
$ID = $row['idprodutos'];
// $PhoneNumber=$row['PhoneNumber'];
// $Email=$row['Email'];
//-display the result of the array
echo "<ul>\n";
echo "<li>" . $name . " " . $ano . "</li>\n";
//echo "<li>" . $PhoneNumber . "</li>\n";
//echo "<li>" . "<a href=mailto:" . $Email . ">" . $Email . "</a></li>\n";
echo "</ul>";
}
}
?>
</form>
Possible duplicate of Error in select using mysqli_query
– 8biT
@8bit the situation dealt with in this topic is different from my situation.
– UniX
yes, the situation is probably different but the mistakes are the same, you checked the errors that are in the question ? And the Answers ?
– 8biT
So what should I do to fix this?
– UniX