Reputation: 5063
I'd like to store banned words in a database to later determine if some word is present (hence being a banned word, which should be censored).
Word input form (ban.php) :
<form name="form" method="post" action="add.php">
<input type="text" name="bad" id="bad">
<input type="submit" name="submit" id="submit" size="12" value="submit">
</form>
PHP code (add.php) :
<?PHP
require_once("config.php"); // db conn
$bad = $_POST['bad'];
$bad = mysql_real_escape_string($bad);
$sql = "insert into my_table set bad='$bad'";
mysql_query($sql, $conn) or die(mysql_error());
echo "Done bad word added";
?>
Let's say we banned the word ugly
. Now I want do this :
<?PHP
require_once("config.php"); // db conn
$qry = "select * from my_table";
$result = mysql_query($qry) or die($qry);
$test = "ugly"; // Example
if ($test == any of the words in the db my_table){
echo "banned";
}else{
echo "passed";
{
?>
How to do this? There are many added words in my_table
:
id,bad (1,'ugly')
id,bad (2,'sick')
id,bad (3,'manal')
id,bad (4,'fog')
Upvotes: 1
Views: 438
Reputation: 375
You should attempt to select the word from the database, this is much faster than having to go through an array.
Something like this should work.
<?php
require_once("config.php"); // db conn
$test = "ugly"; // remember to use mysql_real_escape_string in the implementation
$qry = "SELECT * FROM `my_table` WHERE bad='{$test}'"; // select by value
$result=mysql_query($qry);
if( mysql_num_rows( $result ) ){ // we have a row with the 'bad' word
echo "banned";
}else{
echo "passed";
}
?>
Upvotes: 4
Reputation: 5522
Your SQL of inserting does not make sense. You are inserting, but using the update syntax. Your query should be "insert into my_table values (1, '$bad');
In terms of looking for a banned word, you are better looking for the bad word through your query:
Select count(1) from my_table where word = banned word.
If you run mysql_num_rows over the query, anything greater than 0 means it is banned.
Upvotes: 2
Reputation: 15844
Use a different SQL statement
$qry = "select * from my_table where bad = '".$test."'";
Than just test the result if there is anything or nothing (banned or passed).
Upvotes: 3