0

I need to fetch "UID" row data from db_tags where "Contador" is selected, then insert it into db_rfid. I've tried the fetch commands, but doens't seems to work.



require_once "config.php";
require_once "session.php";



if($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['submit'])){
    
    $contador = trim($_POST['Contador']);   
    $nome = trim($_POST['nome']);

    
    if($query = $db->prepare("SELECT * FROM db_tags.tags2 WHERE CONTADOR = ?")){
        
        $query->bind_param('s', $contador);
        $query->execute();
        
        $query->store_result();
        
                                
                    $insertQuery = $db->prepare("INSERT INTO db_rfid.tbl_rfid (UID, Nome) VALUES (?,?)");
                    $insertQuery->bind_param("ss", $uid, $nome);
                    $result = $insertQuery->execute();
                    header("inserir.php");
                    $insertQuery->close();
                    $query->close();
                
                exit;               
    }
}   
    mysqli_close($db);
    
?>
  • [What do you mean "It doesn't work"?](https://meta.stackexchange.com/questions/147616/what-do-you-mean-it-doesnt-work) – ADyson Jul 19 '22 at 10:58
  • But an obvious question: where is `$uid` supposed to come from? You never create or populate this variable before you try to use it in the insert query. Did you forget to read it from the row returned by the select query? – ADyson Jul 19 '22 at 10:59
  • Also, why not just use an INSERT...SELECT query for this? I don't think you need two separate queries here. – ADyson Jul 19 '22 at 11:00
  • I was using the `$uid` to get "UID" data from db_tags with the fetch commands, but it returns "Unvalid method". And i dont know how to use The Insert...Select query – João Ravasqueira Jul 19 '22 at 11:19
  • I´ve tried this command: `$uid = mysqli_fetch_row($db, 'UID');` – João Ravasqueira Jul 19 '22 at 11:26
  • ...and? That would get you the row, but then you have to get the individual value from the row. Maybe study some simple examples of how to fetch data with mysqli, if you're not familar. https://phpdelusions.net/mysqli#fetch shows you a straightforward example. – ADyson Jul 19 '22 at 11:39

1 Answers1

-1

because a default database is selected in your config.php file.

$db=PDO("...;dbname=XXXX;...");

For this you need to create 2 PDO objects

$dbConnection1=PDO("...;dbname=db_tags;...");
$dbConnection2=PDO("...;dbname=db_rfid;...");

usage:

$query1 = $dbConnection1->prepare("SELECT * FROM tags2 ...");

$query1->execute(...)



$query2= $dbConnection2->prepare("INSERT INTO tbl_rfid ...");

$query1->execute(...)
CodeRDayI
  • 1
  • 1
  • 1
    As long as the mysql user account has permissions to select from both databases it shouldn't matter, you wouldn't need two connections (unless the databases are actually on separate servers) - see https://stackoverflow.com/a/10839173/5947043 (and others) – ADyson Jul 19 '22 at 14:04
  • We can't see the content of config.php. If the dbname field is entered while defining PDO to the $db variable, how to access other databases? – CodeRDayI Jul 19 '22 at 14:07
  • Exactly like the example in the link I showed you, and how the OP's code is doing it - by just putting the database name in front of the table name in the query. As long as the user account has access to it, then it should work. – ADyson Jul 19 '22 at 14:14