-1

I have a table where user inserts data and the user id should be automatically added to the table which he is accessing.

$sql = "INSERT INTO leadinform (lead_compname,lead_add,lead_city,lead_compno,lead_cp,lead_cpd,lead_cpno,lead_cpemail,prodtype,prodmodel,value,clodate,uid) VALUES ('$leadname','$leadadd','$leadcity','$leadcompno','$leadcpname','$leadcpdesig', '$leadcpno','$leadcpmail','$prodtype', '$model','$value','$clodate') SELECT admin.uid FROM admin where admin.username = '$user_check' ";

This is the code I am using but it doesnt seem to work.

where am I wrong ?

Kusum
  • 501
  • 2
  • 11
  • 30
  • 3
    Duplicate? http://stackoverflow.com/questions/74162/how-to-do-insert-into-a-table-records-extracted-from-another-table – M. Eriksson Jul 11 '16 at 05:43
  • hi, i need to add only one column from another table and all the other data are given by the user himself... – Jayashree venugopal Jul 11 '16 at 05:51
  • anyway you should make something like `INSERT INTO leadinform (lead_compname,lead_add,lead_city,lead_compno,lead_cp,lead_cpd,lead_cpno,lead_cpemail,prodtype,prodmodel,value,clodate,uid) SELECT '$leadname','$leadadd','$leadcity','$leadcompno','$leadcpname','$leadcpdesig', '$leadcpno','$leadcpmail','$prodtype', '$model','$value','$clodate', admin.uid FROM admin where admin.username = '$user_check' ";` – Iwo Kucharski Jul 11 '16 at 06:28

3 Answers3

2

Use this:

$sql = "INSERT INTO leadinform 
(lead_compname,lead_add,lead_city,lead_compno,lead_cp,lead_cpd,lead_cpno,lead_cpemail,prodtype,prodmodel,value,clodate,uid)
SELECT '$leadname','$leadadd','$leadcity','$leadcompno','$leadcpname','$leadcpdesig', '$leadcpno','$leadcpmail','$prodtype', '$model','$value','$clodate', admin.uid
FROM admin
WHERE admin.username = '$user_check'"

And please check insert-select.

Blank
  • 12,308
  • 1
  • 14
  • 32
  • hi, only one column ie the user id should be added from another table and all the other info are given by the user thru php – Jayashree venugopal Jul 11 '16 at 05:48
  • @Jayashreevenugopal Yes? No? Sorry, I could not get you. – Blank Jul 11 '16 at 05:52
  • i have 7 columns in my table,6 columns are entered by user and the 7th column(user id) shud be added dynamically from the other table , – Jayashree venugopal Jul 11 '16 at 05:55
  • @Jayashreevenugopal what's wrong with this answer? Did you actually try it? – Strawberry Jul 11 '16 at 05:55
  • 1
    @Jayashreevenugopal - His query does exactly what you want. Try "copy/paste" and test it. When you write `SELECT '$leadname', ...` it means that it will return the value of `$leadname` and not a column from the database. – M. Eriksson Jul 11 '16 at 05:57
  • sorry,i tested ,it doesnt work.... i dont still get it... the admin table has only 3 columns,i am trying to add only the uid from the admin table to another table – Jayashree venugopal Jul 11 '16 at 06:04
0

try this

    $sql = "INSERT INTO leadinform (lead_compname,lead_add,lead_city,lead_compno,lead_cp,lead_cpd,lead_cpno,lead_cpemail,prodtype,prodmodel,value,clodate,uid) 
    VALUES ('$leadname','$leadadd','$leadcity','$leadcompno','$leadcpname','$leadcpdesig', '$leadcpno','$leadcpmail','$prodtype', '$model','$value','$clodate',(SELECT admin.uid FROM admin where admin.username = '$user_check' limit 1))  ";
JYoThI
  • 11,977
  • 1
  • 11
  • 26
0

Check this:

$sql_query = "INSERT INTO leadinform (lead_compname,lead_add,lead_city,lead_compno,lead_cp,lead_cpd,lead_cpno,lead_cpemail,prodtype,prodmodel,value,clodate,uid) 
    VALUES ('$leadname','$leadadd','$leadcity','$leadcompno','$leadcpname','$leadcpdesig', '$leadcpno','$leadcpmail','$prodtype', '$model','$value','$clodate',(SELECT admin.uid FROM admin where admin.username = '$user_check' limit 1))  ";
Thilina
  • 93
  • 2
  • 7