This question already has an answer here:
If my query result is empty I want to insert information otherwise I'd like to update the information. However, I keep getting this error:
Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in...
$info = $_POST['info'];
$gebruiker = $_POST['gebruiker'];
$result = mysql_query("SELECT fysionummer FROM algemene_info WHERE = $gebruiker");
if (mysql_num_rows($result)==0) {
mysql_query("INSERT INTO algemene_info (`info_id`, `omschrijving`, `fysionummer`)
VALUES (NULL, '$info', '$gebruiker')");
//echo $response;
echo "Het toevoegen van de algemene info is geslaagd!";
} else {
mysql_query ("UPDATE algemene_info
SET omschrijving='$info'
WHERE fysionummer='$gebruiker'");
//echo $response;
echo "Het update van de algemene informatie is geslaagd!";
}
</div>
You're obviously missing column name here:
SELECT fysionummer FROM algemene_info WHERE = $gebruiker
It should be like:
SELECT `fysionummer` FROM `algemene_info` WHERE `COLUMN_NAME` = $gebruiker
Please note that mysql_*
functions are deprecated, often unsafe to use. You also insert data from user without ensuring that this won't break your application/database. It's really easy to abuse this and post your own SQL code instead of $_POST['element_name']
.
I would recommend using mysqli_*
functions as they are easy to use for a beginner and much more safer. You can use prepared statements.
Example:
$mysqli = new mysqli("localhost", "user", "password", "database");
$query = $mysqli->prepare("SELECT `fysionummer` FROM `algemene_info` WHERE `COLUMN_NAME` = ?");
$query->bind_param("s", $_POST['gebruiker']);
if($query->execute())
{
// success
// logic
}
Your SQL Query
is incorrect.
$result = mysql_query("SELECT fysionummer FROM algemene_info WHERE = $gebruiker");
WHERE WHAT = '$gebruiker' ??
Correct that and it will resolve your initial problem.
Your code makes no sense. where = $gebruiker
?
where what is $gebruiker?
Anyway, please google for ON DUPLICATE KEY UPDATE
This is probably what you need.
You do a normal insert and on duplicate key violation it will do an update instead.