0

I want to create PHP CLI CRUD Application. I started by simply creating MySQL connection and writing basic CRUD functions. Now I want to be able to accept data using PHP CLI, so I need to substitute $_POST with fgets(atleast, I think so). Never dealed with that before, so would appreciate any help.

<?php

echo "Welcome to Company Register App" . "\n\n";

//establish database connection
define("DB_SERVER", "localhost");
define("DB_USER", "root");
define("DB_PASSWORD", "root");
define ("DB_NAME", "mydatabase");

$mysqli = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME);
if($mysqli === false) {
    echo "Sorry, there seems to be a connection issue.";
} else {
    echo "We are connected". "\n\n";
}

//register function
function addCompany() {

    $name = "";
    $code = "";
    $phone = "";
    $comment = "";
    $id = 0;
    $update = false;

    if (isset($_POST['save'])) {
        $name = $_POST['name'];
        $code = $_POST['code'];
        $phone = $_POST['phone'];
        $comment = $_POST['comment'];

        mysqli_query($mysqli, "INSERT INTO mydatabase (name, code, email, phone, comment) VALUES ('$name', '$code', '$phone', '$comment')"); 

    }
}

?>

I expect to be able to run this file and be prompted to enter info.

Thank you!

dotty
  • 7
  • 3
  • Not sure if it is a duplicate - so will add link instead -https://stackoverflow.com/questions/6543841/php-cli-getting-input-from-user-and-then-dumping-into-variable-possible – Nigel Ren May 30 '19 at 11:04

1 Answers1

1

Check for $argc and $argv variables. $argc should contain number of passed parameters and $argv is the array that contains passed parameters:

https://www.igorkromin.net/index.php/2017/12/07/how-to-pass-parameters-to-your-php-script-via-the-command-line/

Check it on php.net:

https://www.php.net/manual/en/reserved.variables.argv.php

https://www.php.net/manual/en/reserved.variables.argc.php

But also check ont getopt() function:

https://www.php.net/manual/en/function.getopt.php

MilanG
  • 6,994
  • 2
  • 35
  • 64
  • Please add your code to your question instead of writing it in the comment. It's not very readable here. – MilanG May 30 '19 at 11:30