PHP and MySQL validating problem
Posted
by IPADvsSLATE
on Stack Overflow
See other posts from Stack Overflow
or by IPADvsSLATE
Published on 2010-04-27T11:51:37Z
Indexed on
2010/04/27
11:53 UTC
Read the original article
Hit count: 249
I'm trying to check if a color is already entered into the database if it is the color should not be entered and stored into the database and the following error code <p>This color has already been entered!</p>
should be displayed. But for some reason I cant get this to work, can someone please help me?
The color names are entered into $_POST['color']
which is an array entered by the user.
Here is the html code that collects the colors.
<input type="text" name="color[]" />
<input type="text" name="color[]" />
<input type="text" name="color[]" />
<input type="text" name="color[]" />
<input type="text" name="color[]" />
<input type="text" name="color[]" />
<input type="text" name="color[]" />
<input type="text" name="color[]" />
<input type="text" name="color[]" />
Here is the PHP & MySQL code.
for($i=0; $i < count($_POST['color']); $i++) {
$color = "'" . $_POST['color'][$i] . "'";
}
$mysqli = mysqli_connect("localhost", "root", "", "sitename");
$dbc = mysqli_query($mysqli,"SELECT *
FROM colors
WHERE color = '$color'
AND user = '$user_id' ");
if(mysqli_num_rows($dbc) == TRUE) {
echo '<p>This color has already been entered!</p>';
} else if(mysqli_num_rows($dbc) == 0) {
// enter the color into the database
}
© Stack Overflow or respective owner