-1
<form method="post" action="formProcessing.php">
  <input type="text" name="uses[]">
  <input type="text" name="uses[]">
  <input type="text" name="uses[]">
</form>

I have two database tables one called info other uses. info table contain column name inf_num, Is there a way where i can get the last row inf_num and insert the above inputs in uses in one query. For instance if i was to do it manually i would check the last row myself so if it's 10 i would do the below:

INSERT INTO uses (id, uses) VALUES (10, 'useZero'), (10, 'useOne'), (10, 'useTwo');


How would i go about doing it dynamically with php using the above form:

4 Answers4

0

you can create trigger on uses table to set last inserted id of info table.

CREATE TRIGGER uses_before_insert_trigger BEFORE INSERT ON `uses` FOR EACH ROW
      BEGIN                            
           SET NEW.id = (select id from info order by id desc LIMIT 1);    
      END

After, create trigger you can execute insert query directly.

INSERT INTO uses (uses) VALUES ('10'),('20'),('26');
Mayank Majithia
  • 1,916
  • 16
  • 21
0

INSERT INTO user( id, uses ) SELECT MAX(id), 'userOne' FROM info UNION ALL SELECT MAX(id), 'userTwo' FROM info;

-1

Try To Make A Query Like This

<?php

//Your input is array of your input.
$yourInput = array(10,20,26);

$query = "INSERT INTO uses (id, uses) VALUES ";

foreach($yourInput as $value ){
 $query .= "(10, '$value')".",";
}

echo $query;
//Output
INSERT INTO uses (id, uses) VALUES (10, '10'),(10, '20'),(10, '26')

Tested Here Than Execute this Query

But Remember Youe code is not secure. it is possible to do a sql injection so kindly read this note. and make it more secure.

TarangP
  • 2,711
  • 5
  • 20
  • 41
-1

We can make it possible via query as well.

INSERT INTO uses( id, uses ) VALUES ((SELECT MAX(inf_num) from info), 
'useOne', (SELECT MAX(inf_num) from info), 'useTwo', (SELECT MAX(inf_num) from 
info), 'useThree')