简体   繁体   English

PHP创建表单以将数据插入数据库

[英]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. 我有一个数据库文件(.db),其中包含客户信息(名字,姓氏,地址,电话),并且我想创建一个表单以在这些字段中输入更多数据。

I've been given a function in a separate file called queryDb.php: 在另一个名为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文件中,我将其放在顶部:

<?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: 在您的主要php文件中,您应该编写以下内容:

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). 当然,您需要确保表单的操作(reviewsubmit.php)是正确的(您的主要php文件必须为reviewsubmit.php,否则将操作更改为所需的文件)。

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. 您可以使用两个单独的文件,一个使用表单,另一个使用rour函数。 And make an ajax call to your function. 并对您的函数进行ajax调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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