Jump to content

What am i doing wrong with this array?


Go to solution Solved by floridaflatlander,

Recommended Posts

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.