Jump to content

Inserting into multiple tables and multiple entries


Go to solution Solved by Barand,

Recommended Posts

I have a form that looks like this:

 

<form name="form" action="multiCheck2.php" method="POST" enctype="multipart/form-data">
	<table width="381" border="0">
    	<tr>
        	<td colspan="2">Dates Name: <input name="name" type="text" size="39" /></td>
        </tr>
        <tr>
        	<td>Date: <input name="date" type="text" /></td>
            <td> Time: <input name="time" type="text" /></td>
        </tr>
        <tr>
        	<td colspan="2">Additional Information:<br />
       	    <textarea name="comments" cols="45" rows="7"></textarea></td>
        </tr>
    	<tr>
        	<td>
            
            	Whos is this for ? <br />
              
              <p>
              <label>
                <input type="checkbox" name="who[]" value="Coaches" />
                Coaches</label>
              <br />
              <label>
                <input type="checkbox" name="who[]" value="Conveners" />
                Conveners</label>
              <br />
              <label>
                <input type="checkbox" name="who[]" value="Officials" />
                Officials (Both)</label>
                <br />
              <label>
                <input type="checkbox" name="who[]" value="Ref" />
                Ref</label>
                <br />
              <label>
                <input type="checkbox" name="who[]" value="Linesmen" />
                Linesmen</label>
              <br />
              <label>
                <input type="checkbox" name="who[]" value="Admin" />
                Admin</label>
              <br />
              <label>
                <input type="checkbox" name="who[]" value="All" />
                All</label>
              <br />
            </p>
            
            
            </td>
            <td valign="bottom" align="right">
            
            <br /><br />
          <input name="submit" type="submit" value="ADD UPDATE" /></td>
        </tr>
	</table>
	<input type="hidden" name="MM_insert" value="form" />
</form>

 

I have 2 tables I want to interact with.  One is called "soccer_dates" and the other is called "soccer_dates_who".

 

I want soccer_dates to take the values (without the date_who) and insert that information. I have this working fine...

 

In the soccer_dates_who, I want to take the new ID from the newly created soccer_dates entry and insert it, along with the the value from the checked boxes.

 

Also, if there is more then one box checked, i want to make it so there is a new entry in soccer_dates_who for each checked box..

I have been working on this for a few days and but biggest issue is getting the new ID from soccer_dates and putting it into the new table all in one go.  I can do it in 2 different pages, but it adds an extra step.

Does anyone have any advice ? Im not really even sure what to look up for examples..

I have it working like this:

 

<?php
mysql_query("INSERT INTO soccer_dates (date_id, date_name, date_date, date_time, date_content, date_email)
VALUES ('', '".$_POST['name']."', '".$_POST['date']."','".$_POST['time']."','".$_POST['comments']."','')");

$parent = mysql_insert_id();
//$whoCat = print_r($_POST['who']);
//$whoCat = substr(implode(‘, ‘, $_POST['tags']), 0);

foreach($_POST['who'] as $option_key => $option_name)
{
mysql_query("INSERT INTO soccer_dates_who (who_id, who_parent, who_who)
VALUES ('', '".$parent."', '".$option_name."')");
}



echo "<br /><br />Thank you for uploading<br /><br />";



?>

 

Everything seems to be inserting correctly in both tables.

 

Is this a good way of doing it ? or is there an obvious flaw ?

 

Im just happy right now that it's working...

  • Solution
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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