php/mysql two variable array

I am allowing user to create up to ten categories using text boxes in a form. To do this I have ten textboxes in the form with name = cat[]. The first time they enter categories--however many, up to ten, on the receiving end, I just collect the array of categories and save them to a table of categories using an insert statement. So far so good.

However, now I want to let them change categories they already set or add extras up to ten total. The categories they already set have ids. So I echo the ids to the form along with the category names. My problem is how do I collect the names and ids now? New ones need to be inserted and existing ones may need to be updated in the table? For some reason, I'm at a total loss how to do this. This is what I have so far>

Table Cats

id | catname | userid

Form page:

//retrieve existing cats if any...also get catids

echo '<form action = storecats.php method=post>';
$i=1;
//Get all existing cats
while($row = mysql_fetch_array($res))
{
    $catname = $row['catname'];
    $catid = $row['id'];
    echo '<input type = "text name="cat[]" value="'.$catname.'">Cat 1';
    echo '<input type="hidden" name="id[]" value = "'.$catid.'">';
    $i = $i+1;
}
//empty boxes up to ten.
while($i <= 10){
    echo '<input type="text" size=18 name="cat[]" value=""> Cat '.$i.'<br>';
    $i = $i+1;
}//end appending of blank categories
echo '<input type="submit" name="submit" value="submit"></form>';

On receiving end, I can collect the array for cat and id.

$idarray = $_POST['id']; //there will be as many elements as there were ids
$catarray = $_POST['cat'];  //there will be ten elements in array

Where there is an id, I want to perform an update whereas where there is no id I want to insert unless the value is blank.

My problem is I can't figure out how to link the id and the cat names being received in the different arrays. Note, there will always be ten cat names but there will only be as many ids as were in the table for that user previously.

Answers


You can do it with 2 array on HTML side:

    $x=0;
    while ($row = mysql_fetch_array($res))
    {
        echo '<input type="text" name="updateCat['.$row["id"].']" value="'.$row["catname"].'"> Cat Name';
        $x++;
    }

    // new ones
    for(;$x<10;$x++)
    {
        echo '<input type="text" name="newCat[]">';
    }

and on php side:

    // update old categories
    foreach ($_POST["updateCat"] as $key => $value)
    {
        mysql_update ("UPDATE table SET name = $value WHERE id = $key");
    }

    // insert 
    foreach ($_POST["newCat"] as $value)
    {
        mysql_update ("INSERT INTO table {...}");
    }

You have also check that there are not more than 10 Categories when inserting new ones.


Name the inputs which are for existing categories cat_update[] and name inputs which are for new categories cat_insert[]. When you process the form data with PHP you have three arrays:

$ids = $_POST['id'];                // Ids for update
$update = $_POST['cat_update'];     // Values for UPDATE
$insert = $_POST['cat_insert'];     // Values for INSERT

for($i = 0; $i < count($ids); $i++) {
    //Execute 'UPDATE `table` SET `value` = $update[$i] WHERE `id` = $ids[$i]
}

for($i = 0; $i < count($insert); $i++) {
    //Execute 'INSERT INTO `table` (`value`) VALUES ($insert[$i])
}

I'm not sure I get it, but you could simply give different names to the two sets of categories.

In your second loop putting 'name = "new_cat[]"' will allow you to distinguish between modified categories ('$_POST[cat]') and new ones to be inserted ('$_POST[new_cat]') in your receiving storecats.php code.


Need Your Help

Synchronous calls on asynchronous processes

javascript angularjs synchronization

I need to execute a function LoginUser after the RegisterUser function has finished.