0

I have two tables. One table storing the user details and the other stores the files created by the user. I am now trying to save the userid, filename and moddate into the files table.

users table contains: userid, unique_id( hashed value ), username

files table contains: id, userid, filename, moddate

I could get hold of the unique_id value and i used the sql statement below to get the userid from the users table.

$id = mysqli_query($con,"SELECT userid FROM users WHERE unique_id= '$userid'");


 $result = mysqli_query($con,"INSERT INTO files(userid, name, mod_date) VALUES('$id','$name', NOW())");

But the userid value added in files table in the database keeps having the value 0. Did i do it the right way? Is the Insert statement correct?

Correction:

$id = mysqli_query($con,"SELECT userid FROM users WHERE unique_id= '$userid'");

$rowid = mysqli_fetch_row($id);


    $result = mysqli_query($con,"INSERT INTO file_info(userid, name, mod_date) VALUES('$rowid','$name', NOW())");
2
  • May I confirm if this question is related to Android or pure PHP? Commented Apr 14, 2014 at 4:31
  • First you get whole row data and then extract userid and try to use this way... Commented Apr 14, 2014 at 4:34

1 Answer 1

1

The mysqli_query will not return the selected id. It returns the mysqli_result object. You have to use mysqli_fetch_xxx functions to fetch the rows and get data from them. See the example here: http://www.php.net/manual/mysqli-result.fetch-row.php Look at this code:

$result = mysqli_query($con, "SELECT userid FROM users WHERE unique_id = '$userid'");
$row = mysqli_fetch_row($result);

if ($row){
    $id = $row[0];
    ...
    $result = mysqli_query($con, "INSERT INTO file_info (userid, name, mod_date)  VALUES('$id','$name', NOW())");    
} 
Sign up to request clarification or add additional context in comments.

2 Comments

I have added the correction portion above. Do i do it this way?
No, mysqli_fetch_row returns table row as array. You have to use: $rowid[0] to get the ID, but I would also recommend to rename your variables according to their real meaning.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.