-1
$sql = "INSERT INTO memberadd (username,password,profilepic) VALUES ('$username','$password','$profilepic')";

The code above is register with image upload

$imagedataprofile = "INSERT INTO imagedbgallery (postid,imageupload) VALUES ('$postid','$profilepic')";

the code above is trying to pass the data to another table

my memberadd table (below)

userid(PK) | username | password | profilepic

my imagedbgallery table (below)

imageid(PK) | userid | imageupload

the things that i try to achieve is that when user insert a record: memberadd table will show

1 | tommy | 12345 |tommyprofile.png

and in imagedbgallery table will show

1,1,tommyprofile.png

=========================================================================== But when i tried to run the code,

memberadd table will show

1 | tommy | 12345 |tommyprofile.png

and in imagedbgallery table will show

1,0,tommyprofile.png
Isaac Bennetch
  • 11,830
  • 2
  • 32
  • 43
VoxVola
  • 59
  • 1
  • 1
  • 8
  • 1
    And what's the question? Do you get some error? Show what you have tried please – Sfili_81 Oct 12 '18 at 06:58
  • No error, however when i tried to run the code , in my imagedbgallery table , the userid is 0. the question that i want to ask is that where did i do wrong ? the answer that i want is when i i try register, the imagedbgallery table will show userid(from memberadd table) – VoxVola Oct 12 '18 at 06:59
  • Hem because you don't insert it – Jules R Oct 12 '18 at 07:00
  • @JulesR , oh , then how should i insert it ? – VoxVola Oct 12 '18 at 07:01
  • Please update the question with the correct answer and the issue you get – Sfili_81 Oct 12 '18 at 07:03
  • Possible duplicate of [PDO get the last ID inserted](https://stackoverflow.com/questions/10680943/pdo-get-the-last-id-inserted) – Alex Oct 22 '18 at 19:32

1 Answers1

0

just set the userid(PK) autoincrement in your memberadd table then execute the query

 $sql = "INSERT INTO memberadd (username,password,profilepic) VALUES ($username, $password, $profilepic)";
 mysql_query($sql);

now fetch the last inserted id using this query

$lastId = mysql_insert_id();

now pass this id in

$imagedataprofile = "INSERT INTO imagedbgallery (imageid, postid,imageupload) VALUES ($lastId, $postid, $profilepic)";
khan Farman
  • 346
  • 3
  • 11
  • tried the code, but imagedbgallery is not updating – VoxVola Oct 12 '18 at 07:15
  • did you execute the first sql query like this mysql_query($sql); before this $lastId = mysql_insert_id(); ? – khan Farman Oct 12 '18 at 07:21
  • yeah , sorry my bad for the lack of explanation , as in not updating is that (same result, still showing 0 ) – VoxVola Oct 12 '18 at 07:22
  • 1
    Please don't use mysql_query!!! This extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. – Sfili_81 Oct 12 '18 at 07:23
  • it may be because you still haven't set userid as autoincrement in memberadd table in xampp or wamp server whichever you are using – khan Farman Oct 12 '18 at 07:24
  • yeah you are right it is removed in php7, you can go for the mysqli_query. read the docs here http://php.net/manual/en/mysqli.query.php – khan Farman Oct 12 '18 at 07:26