Jump to content

Updating database records with PHP


dreampho

Recommended Posts

Hi.

 

I need some advice on how would be best to achieve the below:

 

I have a list of database entries. Each entry is a row in the database, and has an entry_id assigned to it. I am loading each entry into a unordered list item and using jQuery ui to drag and drop their order. In the database I have a column in the same table as the entries, named sort_order. I need to update the sort_order column.

 

I think the best way would be to submit a form which would send the entry_id and sort_order to a php script that then updates the database for each row.

 

The problem is I am not sure how I should send the data for multiple rows at once, and then not sure how to enter the sort_order for each row separately.

 

I would be very grateful if someone can give me some advice on this.

 

Thank you

 

Link to comment
https://forums.phpfreaks.com/topic/263980-updating-database-records-with-php/
Share on other sites

Thanks Barand.

 

I see, so each list item will have a form input which has something like:

 

<input type="text" name="sort_order[$entry_id]" value="$sort_order" />

 

My next question would be, is there a way to assign an order. So the first li item is 1, then the next is 2, and so on?

 

Thank you

simple example

 

<?php
if (isset($_POST['sort_order'])) 
{
    foreach ($_POST['sort_order'] as $id => $sortorder)
    {
            $id = intval($id);
            $sortorder = intval($sortorder);

           // update record using $id and $sortorder here
    }
}
?>


<form method="post">
<ol>
<li><input type="text" name="sort_order[1]" value="2" /></li>
<li><input type="text" name="sort_order[2]" value="3" /></li>
<li><input type="text" name="sort_order[3]" value="1" /></li>
</ol>
<input type="submit" name="btnSubmit" value="Submit">
</form>

Thank you Barand.

 

I have been trying to get this to work all afternoon, but my lack of knowledge is showing.

 

For my foreach statment I am getting an error:

 

Warning: Invalid argument supplied for foreach() in /home/chrisdav/public_html/template/process-sortable.php on line 12

 

foreach ($_GET['listItem'] as $sortorder => $id)
{
$id = intval($id);
    $sortorder = intval($sortorder);

mysql_query("UPDATE exp_channel_data SET field_id_90 = $sortorder WHERE entry_id = $id");
}

 

Can you see what I have done wrong, and possibly explain what it is to me?

 

Many thanks

Input names had id as the index

<input type="text" name="sort_order[$entry_id]" value="$sort_order" />

 

When using foreach to traverse an array the syntax is

foreach (array as key => value)

 

therefore you need

foreach ($_GET['listItem'] as $id => $sortorder)

 

since the array key will be the id and the value will be the sortorder

 

Is your form method GET or POST?  If POST you need foreach($_POST['listitem'] ...

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.