简体   繁体   中英

Need help for database update query in php form - Joomla database

Let's say I have a table with 10 records and I want to take name , lastname and rank from those 10 records. First I do something like this:

<?php // DATABASE SELECT QUERY
      $db =& JFactory::getDBO();
      $query="SELECT name, lastname, rank
                FROM table
               ORDER BY rank ASC";
      $db->setQuery($query);
      $rows = $db->loadObjectList(); ?>

Then, I add some fields in to my form that contain table's values, so I can edit them through form:

<form action="#" method="post" name="form">

<table><?php $count    = 0; while ($count < 10){
             $name     = $rows[$count]->name;
             $lastname = $rows[$count]->lastname;
             $rank     = $rows[$count]->rank; ?>
<tr>

<td><input name="name" value="<?php echo $name ?>" type="text" /></td>
<td><input name="lastname" value="<?php echo $lastname ?>" type="text" /></td>
<td><select name="rank">
    <option value="<?php echo $rank ?>"><?php echo $rank ?></option>
    <option disabled="disabled" value="...">...</option>
    <?php for ($i = 0; $i <= 100; $i++){ ?>
    <option value="<?php echo $i ?>"><?php echo $i ?></option> <?php } ?>
    </select></td>  

</tr><?php $count++;}?>

</table>

<input class="something" name="updatemod" type="submit" value="UPDATE" />
</form>

Next, before Select query, I have add an update query using this method below, so when I press the update button, update my DB:

// DATABASE UPDATE QUERY
if (isset($_POST['updatemod']) or isset($_GET['updatemod'])){
$db =& JFactory::getDBO();
$query = "UPDATE table
             SET name = '".$_POST["name"]."',
             SET lastname = '".$_POST["lastname"]."',
             SET rank = '".$_POST["rank"]."'
             ";
$db->setQuery($query);
$db->query();}

But... Nothing is working!!! I have done exactly the same thing for an other form and it's working perfect! The only difference between those two forms, is that I am not using this while loop at the other form. So, maybe it has to do with this or something??? I don't know, at this point is where my knowledge confused, so I need your help!

I think you are trying to update all the rows in your table.

If that is the case,You need to do something like this.

<?php // DATABASE SELECT QUERY // also you should select your unique id field
      $db =& JFactory::getDBO();
      $query="SELECT name, lastname, rank,id
                FROM table
               ORDER BY rank ASC";
      $db->setQuery($query);
      $rows = $db->loadObjectList(); ?>

In your form you are placing element name as single but your requirement is to edit all the rows at one click you should use input names as array.Also here you have to introduce a new id field too for update condition

    <form action="#" method="post" name="form">

    <table><?php $count    = 0; while ($count < 10){
                 $name     = $rows[$count]->name;
                 $lastname = $rows[$count]->lastname;
                 $rank     = $rows[$count]->rank;
                  $id      = $rows[$count]->id; ?>
    <tr>

    <td><input name="uid[]" value="<?php echo $id?>" type="hidden" />
<input name="name[]" value="<?php echo $name ?>" type="text" /></td>
    <td><input name="lastname[]" value="<?php echo $lastname ?>" type="text" /></td>
    <td><select name="rank[]">
        <option value="<?php echo $rank ?>"><?php echo $rank ?></option>
        <option disabled="disabled" value="...">...</option>
        <?php for ($i = 0; $i <= 100; $i++){ ?>
        <option value="<?php echo $i ?>"><?php echo $i ?></option> <?php } ?>
        </select></td>  

    </tr><?php $count++;}?>

    </table>

    <input class="something" name="updatemod" type="submit" value="UPDATE" />
    </form>

Also in your Update Query also required loop and where cluase

// DATABASE UPDATE QUERY
if (isset($_POST['updatemod']) or isset($_GET['updatemod'])){
$db =& JFactory::getDBO();
$total_rows = sizeof($_POST["uid"]);
for($i =0; $i<$total_rows;$i++){

$query = "UPDATE table
             SET name = '".$_POST["name"][$i]."',
             lastname = '".$_POST["lastname"][$i]."',
             rank = '".$_POST["rank"][$i]."'
              WHERE id = '".$_POST["uid"][$i]."'
             ";
$db->setQuery($query);
$db->query();
}
}

I think this will solve your problem.

Here i just mentioned the "id" as your unique key of the table that may differ.But you will get the idea , i hopes

But this is a worst case update condition Bcoz you just imagine the table with 1000 rows. It will take too long to get the result.

Try to update single row with proper method.It is the best method.

Hope this may help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM