简体   繁体   中英

PHP Creating Form to insert data into Database

I'm creating a website as an activity and I'm not sure how to proceed. I have a database file (.db) that has Customer information (firstname, lastname, address, phone) and I'm wanting to create a form to input more data into those fields.

I've been given a function in a separate file called queryDb.php:

function addCustomer($fname, $lname, $address, $phone) {

      $db = new MyDB();
      if(!$db){
         echo '<script type="text/javascript">alert("'.$db->lastErrorMsg().'");</script>';
      } else {
         //echo "Opened database successfully\n";
      }

      $sql ='INSERT INTO CUSTOMERS (FIRSTNAME, LASTNAME, ADDRESS, PHONE) VALUES ("'.$fname.'", "'.$lname.'", "'.$address.'", "'.$phone.'");';
      $db->query($sql);
   }

In my main php file I have this up the top:

<?php
         require_once "queryDb.php";
         $firstname = $_POST["firstname"];
         $lastname = $_POST["lastname"];
         $address = $_POST["address"];
         $phone = $_POST["phone"];

        ?>

and the form, I've only created it for two inputs, Address and phone to test it:

<form action="reviewsubmit.php" method="post">
  Test Address
  <input type="text" id="address" name="address" placeholder="test">  />
  Test phone
  <input type="text" id="phone" name="phone" placeholder="test">  />
  <input type="submit" name="Submit" value="Submit" />
</form>

I'm not sure how I can call the function from the other file to use in this file. How can I do this?

In your main php file, you should write the following:

if($_POST)
{
     require_once "queryDb.php";
     $firstname = $_POST["firstname"];
     $lastname = $_POST["lastname"];
     $address = $_POST["address"];
     $phone = $_POST["phone"];
     $result = addCustomer($firstname, $lastname, $address, $phone);
     //Some redirect here based on the result
}

Of course you need to make sure that your form's action (reviewsubmit.php) is correct (Your main php file must be reviewsubmit.php, otherwise change the action to be your desired file).

Also you must add firstname and lastname inputs to your form like the following:

Firstname
<input type="text" id="firstname" name="firstname" placeholder="Enter Firstname">  />
Lastname
<input type="text" id="firstname" name="firstname" placeholder="Enter Firstname">  />

Everything seems ok, call the function after this:

<?php
    require_once "queryDb.php";
    $firstname = $_POST["firstname"];
    $lastname = $_POST["lastname"];
    $address = $_POST["address"];
    $phone = $_POST["phone"];

   addCustomer($firstname, $lastname, $address, $phone);

you sould use two separate files, one with your form an the other with rour function. And make an ajax call to your function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM