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>';} Link to comment https://forums.phpfreaks.com/topic/280279-what-am-i-doing-wrong-with-this-array/ 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. Link to comment https://forums.phpfreaks.com/topic/280279-what-am-i-doing-wrong-with-this-array/#findComment-1441233 Share on other sites More sharing options...
floridaflatlander Posted July 18, 2013 Author Share Posted July 18, 2013 Jesus, thanks fastsol, I don't even think of using the mysqli_num_rows, that does look cleaner. thanks Link to comment https://forums.phpfreaks.com/topic/280279-what-am-i-doing-wrong-with-this-array/#findComment-1441235 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.