Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7217861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T21:24:26+00:00 2026-05-28T21:24:26+00:00

I am trying to get values from an form with arrays them I need

  • 0

I am trying to get values from an form with arrays them I need to get the ids from the bd to update, but it isnt working, maybe u give me an idea, I’ll post the code, so u may understand. Thank you in advance for your help!

<?php

include "conecta.php";
include "verifica.php";



$sql1 = "SELECT idcategorias FROM categorias";
$res = mysql_query($sql1);
while ($row=mysql_fetch_array($res)){
$gosma = $row["idcategorias"];
}

foreach ($_POST['ordem'] as $key => $value){


$sql = "UPDATE categorias SET ordem='$value' WHERE idcategorias='$gosma'";
$res = mysql_query($sql) or die ("Erro ao alterar") . mysql.error();
}

if($res) {
echo("<script>alert('DADOS ALTERADOS COM SUCESSO ');</script>");
echo "<meta HTTP-EQUIV='refresh' CONTENT='1;URL=lista_categorias.php'>";

 }

else{
echo("<script>alert('ORDEM INALTERADA, TENTE NOVAMENTE');</script>");
echo "<meta HTTP-EQUIV='refresh' CONTENT='1;URL=altera_ordem_categoria.php'>";
}

?>

AND THE FORM IS HERE

<form action="altera_ordem_cat.php" method="post">
<?php
include "conecta.php";
include "verifica.php";
$sql = "SELECT * FROM categorias ORDER BY ordem";
$res = mysql_query($sql) or die (mysql_error());
while ($linha=mysql_fetch_array($res)) {
$linha[2] = $real;
?>

<tr>
<td><?=$linha['2']; echo "&#32;<input name='ordem[]' type='text' id='textfield'    size='2' maxlength='2' value=''/>" ?></td>
<td><?=$linha['1']; ?></td> 
</tr>

 <?php }?>


  </table>
  <br/>
  <table align="center">
  <tr>
 <td>
 <input type="submit" name="alterar" type="button" value="Alterar">
 </td>
 </tr>
  </table>
   </form>  
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T21:24:26+00:00Added an answer on May 28, 2026 at 9:24 pm

    Your while cycle overwrites the $row each time. Don’t you mean to do this?

    $gosma = array();
    while ($row=mysql_fetch_array($res)) {
        $gosma[] = $row["idcategorias"];
    }
    

    You should always escape before inserting into a query:

    $sql = "UPDATE categorias SET ordem='".mysql_real_escape_string($value)."' WHERE idcategorias='".mysql_real_escape_string($gosma)."'";
    

    You should put the mysql_error inside the die function (and don’t use dot in the name)

    $res = mysql_query($sql) or die ("Erro ao alterar: ".mysql_error());
    

    What is it you’re trying to do in this cycle?

    foreach ($_POST['ordem'] as $key => $value) {   
        $sql = "UPDATE categorias SET ordem='$value' WHERE idcategorias='$gosma'";
        $res = mysql_query($sql) or die ("Erro ao alterar") . mysql.error();
    }
    

    Don’t you want something like this?

    foreach ($_POST['ordem'] as $key => $value) {   
        $sql = "UPDATE categorias SET ordem='".mysql_real_escape_string($value)."' WHERE idcategorias='".mysql_real_escape_string($key)."'";
        $res = mysql_query($sql) or die ("Erro ao alterar: ".mysql_error());
    }
    


    EDIT:

    <form action="altera_ordem_cat.php" method="post">
      <table>
    <?php
    include "conecta.php";
    include "verifica.php";
    $sql = "SELECT idcategorias, ordem FROM categorias ORDER BY ordem";
    $res = mysql_query($sql) or die (mysql_error());
    
    while ($linha = mysql_fetch_array($res)) {
    ?>
      <tr>
        <td><input name='ordem[<?php echo $linha[0]; ?>]' type='text' id='textfield' size='2' maxlength='2' value='<?php echo $linha[1]; ?>'/></td>
        <td></td> 
    </tr>
    <?php
    }
    ?>
      </table>
    <!-- ........ -->
    

    And afterwards:

    <?php
    
    include "conecta.php";
    include "verifica.php";
    
    foreach ($_POST['ordem'] as $key => $value) {   
      $sql = "UPDATE categorias SET ordem='".mysql_real_escape_string($value)."' ".
             "WHERE idcategorias='".mysql_real_escape_string($key)."'";
      $res = mysql_query($sql) or die ("Erro ao alterar: ".mysql_error());
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to get the values from a pointer to a float array, but
I'm trying get values from a GridView using the following code: foreach (GridViewRow row
I'm trying to get all property names / values from an Outlook item. I
I'm trying to get the pixel rgb values from a 64 x 48 bit
I'm trying to get the $url value to display from the MySQL database but
I'm trying to get a query working that takes the values (sometimes just the
I am trying to make this form with checkboxes get values 1 or 0,
I am trying to get the data from the form I created where I
I am trying to update array values using the response from multiple getJSON requests.
I'm learning Javascript for a project. I'm trying to try and get values from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.