floridaflatlander Posted July 18, 2013 Share Posted July 18, 2013 I have a select that allows a user to select an album. I then run a query to check the post of the select to make sure it's a real album number. So what I do is run the query then use in_array to see if the entered number is a good album number My problem is the first one doesn't return an array and when I use print_r I only get one value I can't get this one to work, why won't the in_array work? I thought $aid_rows = mysqli_fetch_array($ar, MYSQLI_ASSOC) would give me an array // Check for an album id if (empty($_POST['aid'])) { $errors [] = '<h5 style="color: red;">* You forgot to select an album.</h5>'; } elseif (is_numeric($_POST['aid'])) { $aid_entry = (int)(trim($_POST['aid'])); $aq ="SELECT aid FROM cpg15x_albums"; $ar = mysqli_query($dbc3, $aq);// or die("Error: ".mysqli_error($dbc3)); $aid_rows = mysqli_fetch_array($ar, MYSQLI_ASSOC); if (in_array($aid_entry, $aid_rows)) { $aid = $aid_entry; } else {$errors[] = '<h5 style="color: red;">* Album number error.</h5>';} } else {$errors[] = '<h5 style="color: red;">* Album string error.</h5>';} My new one that works but I have to use a while loop // Check for an album id if (empty($_POST['aid'])) { $errors [] = '<h5 style="color: red;">* You forgot to select an album.</h5>'; } elseif (is_numeric($_POST['aid'])) { $aid_entry = (int)(trim($_POST['aid'])); $aq ="SELECT aid FROM cpg15x_albums"; $ar = mysqli_query($dbc3, $aq);// or die("Error: ".mysqli_error($dbc3)); $aid_rows = mysqli_fetch_array($ar, MYSQLI_ASSOC); $aids = array(); while ($aid_rows = mysqli_fetch_array($ar, MYSQLI_ASSOC)) { $aids[] = $aid_rows['aid']; } if (in_array($aid_entry, $aids)) { $aid = $aid_entry; } else {$errors[] = '<h5 style="color: red;">* Album number error.</h5>';} } else {$errors[] = '<h5 style="color: red;">* Album string error.</h5>';} Quote Link to comment Share on other sites More sharing options...
fastsol Posted July 18, 2013 Share Posted July 18, 2013 You need to use the WHERE clause in the queries, that way you are checking for that specific aid in the db. Also your is_numeric check is pointless, just cast the post to an int like you are (without the trim()), when you runt the query it will filter out if the album actually exists. if (empty($_POST['aid'])) { $errors [] = '<h5 style="color: red;">* You forgot to select an album.</h5>'; } else { $aid_entry = (int)$_POST['aid']; $aq ="SELECT `aid` FROM `cpg15x_albums` WHERE `aid` = $aid"; $ar = mysqli_query($dbc3, $aq);// or die("Error: ".mysqli_error($dbc3)); if (mysqli_num_rows($ar) == 1) { $aid = $aid_entry; } else {$errors[] = '<h5 style="color: red;">* Album number error.</h5>';} } else {$errors[] = '<h5 style="color: red;">* Album string error.</h5>';} I also moved some things around and got rid of pointless lines and code. Quote Link to comment Share on other sites More sharing options...
Solution floridaflatlander Posted July 18, 2013 Author Solution Share Posted July 18, 2013 Jesus, thanks fastsol, I don't even think of using the mysqli_num_rows, that does look cleaner. thanks Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.