PHP MySQL if row exists not doing anything

320

Solution:

 if(mysql_num_rows($check)>0) {
  echo " ";                                                                                         
 }
  else
  {

change your line of code to this, this would check if it already exist. Flies Away

436

Answer

Solution:

Try checking in the following manner

 if(mysql_num_rows($check) !== FALSE)

Also try printing the query to see how the value is embedding for debugging purpose...

920

Answer

Solution:

If it's a ban list based on a unique username why not alter the table to make the column unique. This code will also delete duplicates in the column 'Ban'.

ALTER IGNORE TABLE ingamebanlist ADD UNIQUE (Ban);

Next, mysql_num_rows() is deprecated in php 5.5

http://php.net/manual/en/function.mysql-affected-rows.php

Your best bet (even if your mysql functions work) is to change the mysql function calls to mysqli function calls. They are more secure and up to date.As well as making sure there is a constant in your code so your not switching between them.

If you change the mysql commands to mysqli this should work.

mysqli_affected_rows($conn);

Lastly, if you change the 'Ban' column to unique and make use of the mysqli_affected_rows this code should be all you need to insert when doesn't exist.

mysqli_query($conn,"INSERT INTO `ingamebanlist` (Ban, Timestamp) VALUES ('$line', '$timestamp')");

$num = mysqli_affected_rows($conn);

if($num > 0)
{
echo "Successfully Inserted";
}
else if($num == 0)
{
echo "Line already exists";
}
else
{
echo "Insert Error";
} 

People are also looking for solutions to the problem: php - String of file_get_html can't be edited?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.