Page 1 of 1

help with insert...

Posted: Fri Apr 03, 2009 6:04 am
by nishmgopal
Hi guys,

I am trying to insert values from the form into two different tables. The skill Name goes to the skill table, in which the Skill_ID is auto increment. and then I want to insert the weight and the last Skill_ID into the ID_Table.

My code so far is below, i no i have to use mysql_last_insert_ID to get the last ID in the skill table, but im not sure how....can anyone please help.

Code: Select all

 
 <?php
  echo "</p>
 </br>
<form id='addmore' method='post' action='add_1.php'>
  <table width='200' border='0' nowrap>
  <tr>
  <td></td>
  <td align='center'><strong>Skill Name</td>
  <td><strong>Weight</td>
    <tr>
      <td nowrap='nowrap'> <font color='#4096EE'><strong>Skill 1</font></td>
      <td bgcolor='#85c329' div id='input'><input type='text' name='skill1' id='skill1' /> </td>
     <td><select name='weight1' id='weight1'>
        <option value='1'>1</option>
        <option value='2'>2</option>
        <option value='3'>3</option>
        <option value='4'>4</option>
        <option value='5'>5</option>
      </select>      </td>
      <td > <input type='submit' name='add1' id='add1' value='Add' /></td>
    
      </form>
   </tr>
  </table>";
  ?>
 
<?php  
 
$skill1=$_POST['skill1'];
$weight=$_POST['weight1'];
 
mysql_query("INSERT INTO Skill_ID 
(Skill_ID, Skill_Name) VALUES('NULL', '$skill1' ) ") 
or die(mysql_error());  
 
mysql_query("INSERT INTO ID_Table 
(ID, Job_ID, Skill_ID, Weight) VALUES('NULL', '1','LAST_INSERT_ID', '$weight' ) ") 
or die(mysql_error());  
 
 
echo "Data Inserted!";
 
?>
 
thanks

Re: help with insert...

Posted: Fri Apr 03, 2009 6:15 am
by susrisha

Code: Select all

 
mysql_query("INSERT INTO Skill_ID
(Skill_ID, Skill_Name) VALUES('NULL', '$skill1' ) ")
or die(mysql_error());  
 
$skill_id = mysql_insert_id($con); //$con is what you get when you do the mysql_connect()
//$skill_id will store the last inserted skill_id 
//i believe that Skill_ID is an integer and you dont need those single quotes around it.
mysql_query("INSERT INTO ID_Table
(ID, Job_ID, Skill_ID, Weight) VALUES('NULL', '1',$skill_id, '$weight' ) ")
or die(mysql_error());  
 
 
http://in.php.net/manual/en/function.my ... ert-id.php

Re: help with insert...

Posted: Fri Apr 03, 2009 6:32 am
by nishmgopal
thank you very much that worked...

now my problem is how to add multiple skills and multiple weights into the tables. My current structure only lets me add one value per table, so 1 skill id, 1 weight, and 1 job id....what if I wanted to add 3 skills with 3 weights for any particular job id.

for example

Job ID Skill ID Weight
1 4 5
1 5 3
1 6 2

all these would go into ID_Table....

is this possible or do i need multiple queries?

Re: help with insert...

Posted: Fri Apr 03, 2009 8:01 am
by susrisha
i think you need to use the "UPDATE" statement for that. not sure enough..