0

I'm trying to loop this code as this will be dynamically generated. It's getting the number of rows from mysql and looping that many times. The POST is from ajax where it's also looped and it works perfectly. In the php part i'm not sure why it's not working. I've tried a lot of different loop variations.

$queryEle = "SELECT * FROM featureSheetContent WHERE `user_id` = '$userID' AND `feature_id` = '$featureID'";
  $resultEle = mysqli_query($connect,$queryEle);
  $rowEle = mysqli_num_rows($resultEle);

  $i = 1;

  while ($i < $rowEle) {
    $eleID.$i = $_POST['eleID'.$i];
    $eleID .= $eleID.$i;
    $resizeWidth.$i = $_POST['resizeWidth'.$i];
    $resizeWidth .= $eleID.$i;
    $resizeHeight.$i = $_POST['resizeHeight'.$i];
    $resizeHeight .= $eleID.$i;

    $UploadMain = $connect->prepare("UPDATE featureSheetContent SET `ele_width` = ?, `ele_height` = ? WHERE id = ?");
    $UploadMain->bind_param('sss', $resizeWidth, $resizeHeight, $eleID);
    $UploadMain->execute();

    $i++;
  }

Unlooped code works:

  $eleID1 = $_POST['eleID1'];
  $resizeWidth1 = $_POST['resizeWidth1'];
  $resizeHeight1 = $_POST['resizeHeight1'];
  $eleID2 = $_POST['eleID2'];
  $resizeWidth2 = $_POST['resizeWidth2'];
  $resizeHeight2 = $_POST['resizeHeight2'];

  
  $UploadMain = $connect->prepare("UPDATE featureSheetContent SET `ele_width` = ?, `ele_height` = ? WHERE id = ?");
  $UploadMain->bind_param('sss', $resizeWidth1, $resizeHeight1, $eleID1);
  $UploadMain->execute();
  $UploadMain = $connect->prepare("UPDATE featureSheetContent SET `ele_width` = ?, `ele_height` = ? WHERE id = ?");
  $UploadMain->bind_param('sss', $resizeWidth2, $resizeHeight2, $eleID2);
  $UploadMain->execute();
Stephen
  • 1
  • 1

1 Answers1

0

Your syntax for variable variables is wrong; the correct syntax is ${"eleID".$i}. But there's no need to use a different variable each time through the loop. Bind the UPDATE query to a specific set of variables, and reassign those variables from the POST variables.

$eleID = $resizeWidth = $resizeHeight = null; // Create the variables before binding
$UploadMain = $connect->prepare("UPDATE featureSheetContent SET `ele_width` = ?, `ele_height` = ? WHERE id = ?");
$UploadMain->bind_param('sss', $resizeWidth, $resizeHeight, $eleID);

for ($i = 1; $i <= $rowEle; $i++) {
    $eleID = $_POST['eleID'.$i];
    $resizeWidth = $_POST['resizeWidth'.$i];
    $resizeHeight = $_POST['resizeHeight'.$i];
    $UploadMain->execute();
}

BTW, your UPDATE query will update a row for any user, not just user_id = '$userID'. Is that intended?

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Hi I tried your code but it doesn't seem to work. It's weird everything looks right. As for not including the user_id, it's because i'm only showing specific rows to each user and the update will only modify that one row. – Stephen Sep 03 '20 at 22:02
  • What's stopping the user from submitting different IDs than the one you showed them? They can open Developer Tools and change hidden inputs. – Barmar Sep 03 '20 at 22:03
  • Try adding the variable initializations at the top, see if that fixes it. – Barmar Sep 03 '20 at 22:04
  • variable initialization at the top doesn't work either – Stephen Sep 03 '20 at 22:13
  • I can't see any reason why this wouldn't work. Are you sure the `$_POST` variables are filled in? – Barmar Sep 03 '20 at 22:15
  • I strongly recommend that you use array-style input names instead of numbering them explicitly like this. `name="eleID[]"` instead of `name="eleID1"` – Barmar Sep 03 '20 at 22:16
  • Add error checking to all the calls. See https://stackoverflow.com/questions/22662488/how-to-get-mysqli-error-in-different-environments – Barmar Sep 03 '20 at 22:16