how to insert a query in a button?

72 views Asked by At

I'm a newbie in PHP. I want to insert logged in user's ID into another table in the database on click of a button.Could any body guide me through or answer.

1

There are 1 answers

0
Anwar On

First you need your form :

<!DOCTYPE html>
<html>
   <head>
     <title> my title </title>
     <meta charset = "utf-8" />
   </head>
   <body>
     <form action = "myfile.php" method = "post">
       <input type = "text" placeholder = "id" name = "id" />
       <input type = "text" placeholder = "password" name = "password" />
       <input type = "submit" />
     </form>
   </body>
</html>

This one simply create a form with two input for the id & the pass.

Now you need to write your myfile.php to tell it to do a query :

<?php
    $userId = $_POST['id']; // The id is the name of the input
    $userPassword = $_POST['password'];

    $mytable = "yourTableNameHere"; // You need to create it first with right columns names (in this example, id & pass)
    $myServer = "yourServerNameHere"; // localhost by default
    $myUserName = "yourUserNameHere"; // name entered to access your database
    $myPassword = "yourPassWordHere" // password used to access your database
    $myDataBase = "yourDataBaseName";

    $myQuery = "INSERT INTO " . $mytable . " (id, pass) VALUES('" . $userId . "', '" . $userPassword . "');";

    $mysqli = new mysqli($myServer, $myUserName, $myPassword, $myDataBase);

    $mysqli->query($myQuery); // your query is executed here
?>