Note that there are some explanatory texts on larger screens.

plurals
  1. POphp/mysql two variable array
    primarykey
    data
    text
    <p>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. </p> <p>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></p> <p>Table Cats</p> <pre><code>id | catname | userid </code></pre> <p>Form page:</p> <pre><code>//retrieve existing cats if any...also get catids echo '&lt;form action = storecats.php method=post&gt;'; $i=1; //Get all existing cats while($row = mysql_fetch_array($res)) { $catname = $row['catname']; $catid = $row['id']; echo '&lt;input type = "text name="cat[]" value="'.$catname.'"&gt;Cat 1'; echo '&lt;input type="hidden" name="id[]" value = "'.$catid.'"&gt;'; $i = $i+1; } //empty boxes up to ten. while($i &lt;= 10){ echo '&lt;input type="text" size=18 name="cat[]" value=""&gt; Cat '.$i.'&lt;br&gt;'; $i = $i+1; }//end appending of blank categories echo '&lt;input type="submit" name="submit" value="submit"&gt;&lt;/form&gt;'; </code></pre> <p>On receiving end, I can collect the array for cat and id. </p> <pre><code>$idarray = $_POST['id']; //there will be as many elements as there were ids $catarray = $_POST['cat']; //there will be ten elements in array </code></pre> <p>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. </p> <p>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. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload