I have a table that's called People and each row has a name structure, and other rows like age and such...
Anyways, I want to check if the row that has a specific name is already exists to insert a new one or not to.
This is what I went with so far:
$name = $_GET["name"];
$age = $_GET["age"];
$location = $_GET["location"];
$query = $connection->query("SELECT * FROM people WHERE name='" . $name . "'");
$exist = mysqli_num_rows($query);
if ($exist > 0) {
//exist
$connection->query("INSERT INTO people (name,age,location) VALUES ($name, $age, $location)");
} else {
//doesn't exist
}
But it ain't working, is it because INSERT shouldn't be executed like that? I really don't know, anyways thanks in advance.
EDIT:
After searching I just got the following not working:
$prepare = $connection->prepare("SELECT * FROM people WHERE name = ?");
$prepare->bind_param("s", $name);
$prepare->execute();
$result = $prepare->get_result();
$numRows = $result->num_rows;
if ($numRows <= 0) {
$insert = $connection->prepare("INSERT INTO people (name, age, location) VALUES (?,?,?)");
$insert->bind_param("sis", $name, $age, $loc);
$insert->execute();
echo "true," . $title;
} else {
echo "false," . $title;
}
But it gives me this error:
Fatal error: Call to a member function bind_param() on a non-object in C********* on line 19