Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在表格1中插入数据mysqli和php,然后返回到新的表格2_Php_Forms_Mysqli - Fatal编程技术网

在表格1中插入数据mysqli和php,然后返回到新的表格2

在表格1中插入数据mysqli和php,然后返回到新的表格2,php,forms,mysqli,Php,Forms,Mysqli,大家好,我是mysqli和php的新手(目前正在学习并尝试在测试数据库上工作-目前我还没有使用安全措施,不会公开),我正在尝试获取我刚刚提交的表单中的信息,以新的表单显示,该表单将接收进一步的用户输入,然后提交到数据库。以下是我迄今为止所做工作的一个例子: 表1(客户表-客户id=主键) 客户详细信息,如姓名、地址、电话等 动态下拉框-由4个选项组成。(希望在此处选择任何选项以返回特定表单) 表单当前正在数据库中正确提交,但我希望在表单提交到数据库后返回客户信息(包括客户id,因为这是新表中的关

大家好,我是mysqli和php的新手(目前正在学习并尝试在测试数据库上工作-目前我还没有使用安全措施,不会公开),我正在尝试获取我刚刚提交的表单中的信息,以新的表单显示,该表单将接收进一步的用户输入,然后提交到数据库。以下是我迄今为止所做工作的一个例子:

表1(客户表-客户id=主键) 客户详细信息,如姓名、地址、电话等 动态下拉框-由4个选项组成。(希望在此处选择任何选项以返回特定表单)

表单当前正在数据库中正确提交,但我希望在表单提交到数据库后返回客户信息(包括客户id,因为这是新表中的关系)和表单2上的信息(服务表-服务id是主键)因此,用户可以在表单中输入更多数据并提交

希望这是有意义的,任何帮助将不胜感激。 谢谢

答复1
谢谢你的回答,我可能还没说清楚

表格1,其中动态下拉列表是-当用户提交表格时,我希望它返回表格2,并附上我们在表格1中插入的客户信息

表格1

<!doctype html>
<html>
<head>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
    <title>test</title>
</head>
<body>
<form action="newbookingcode.php" method="post">
<p>First Name: <input type="text" name="firstname"/>
    Last Name: <input type="text" name="lastname"/></p>
    <p>Business Name: <input type="text" name="businessname"/></p>
<p>Contact Number: <input type="text" name="number"/>
 Alt Number: <input type="text" name="altno"></p>
<p>Email Address:<input type="text" name="email"></p>
  <p>Street No:<input tyep="text" name="streetno">
   Street Name:<input type="text" name="street"></p>
    <p>Suburb:<input type="text" name="suburb">
  Postal Code:<input type="text" name="postalcode">
    State: <input type="text" name="state"></p>

     **<p>Type of Service Required: <select id="category" name="category" > 
<option value="nill">---Select Service---</option**

<?php
$servername = "";
$username = "";
$password = "";
$dbname = "";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT * FROM serviceType";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
       echo '<option value= "'.$row['jobType'].'" >' . $row['jobType'] . '</option>';
    }
} else {
    echo "0 results";
}
$conn->close();
?>
</p>
 </select>
 <p>
    <input type="submit"/>
</p>
       </form>   

</body>
</html>

测试
名字:
姓氏:

企业名称:

联络电话: Alt编号:

电邮地址:

街道号: 街道名称:

郊区: 邮政编码: 声明:

** 所需服务类型: ---选择服务---

在单独文件中查询

$fname=$\u POST['firstname'];
$lname=$\u POST['lastname'];
$bname=$_POST['businessname'];
$phone=$_POST['number'];
$altphone=$_POST['altno'];
$email=$_POST['email'];
$streetno=$_POST['streetno'];
$street=$_POST['street'];
$郊区=$_邮政['郊区];
$postcode=$_POST['postalcode'];
$state=$_POST['state'];
$service=$_POST['category'];
//创建连接
$conn=newmysqli($servername、$username、$password、$dbname);
//检查连接
如果($conn->connect\u错误){
die(“连接失败:”.$conn->connect\U错误);
} 
$sql=“插入客户(contactFirstName、contactLastName、businessName、contactNumber、altNumber、电子邮件、街道号、街道名、郊区、邮政编码、州、服务类型)
值(“$fname”、“$lname”、“$BNName”、“$phone”、“$altphone”、“$email”、“$streetno”、“$street”、“$Subparent”、“$postcode”、“$state”、“$service”);
if($conn->query($sql)==TRUE){
echo“新记录创建成功”;
}否则{
echo“Error:”.$sql.“
”$conn->Error; } $conn->close(); ?>

假设您正在使用像
mysqli
这样的数据库对象进行数据库交互。 说
form1
生成插入查询
$insertQyery
。 您正在执行这个查询(insert语句),如下所示

执行此insert语句后,可以使用

$customerId = $mysqli->insert_id

现在,使用此
$customerId
可以在
form2
上显示记录详细信息,或者使用此
$customerId
作为
form2
上的参考

客户提交表格#1后

在下一页

让我们了解最新的客户详细信息:-

<?php

//after mysqli connection

$contactFirstName = '';
$contactLastName = '';
$businessName = '';

$sql_get_data = "SELECT * FROM customer ORDER BY customerID DESC limit 1 ";
$query_get_data = mysqli_query($conn, $sql_get_data);
while ($row = mysqli_fetch_array($query_get_data, MYSQLI_ASSOC)) {

    //specify which data you want to get from "customer" table 
    $contactFirstName = $row['contactFirstName'];
    $contactLastName = $row['contactLastName'];
    $businessName = $row['businessName'];
}
?>

HTML表格#2:-



检查此代码,如果您有任何问题,请告诉我:

<?php
$servername = "";
$username = "";
$password = "";
$dbname = "";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT * FROM serviceType";
$result = $conn->query($sql);



/************** IMPORTANT *************/
$whichForm = 1;//to know which form to show
$last_customer_id = 0;//to store last customer id
//to store your customer information
$fname=$lname=$bname=$phone=$altphone=$email=$streetno=$street=$suburb=$postcode=$state=$service='';


//To handle post operations after clicking on post.
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
 $whichForm = 2; // to show second form

 //if it is first form
 if($_POST['action'] == 'firstForm')
 {
      $fname=$_POST['firstname'];
      $lname=$_POST['lastname'];
      $bname=$_POST['businessname'];
      $phone=$_POST['number'];
      $altphone=$_POST['altno'];
      $email=$_POST['email'];
      $streetno=$_POST['streetno'];
      $street=$_POST['street'];
      $suburb=$_POST['suburb'];
      $postcode=$_POST['postalcode'];
      $state=$_POST['state'];
      $service=$_POST['category'];
      //Preparing your insert query
      $sql = "INSERT INTO customer (contactFirstName,contactLastName,businessName,contactNumber,altNumber,email,streetNo,streetName,suburb,postalCode,state,serviceType) VALUES ('$fname','$lname','$bname','$phone','$altphone','$email','$streetno','$street','$suburb','$postcode','$state','$service')";
      $conn->query($sql); //insert into db
      //get last insert customer id and you already have your customer information
      //in the variables above $fname, $lname.....etc
      $last_customer_id = $conn->insert_id;
 }
 else{//do something with your second form

      //get last customer info
      $sql = "SELECT * FROM serviceType where id = ".$POST_['last_customer_id']."";
      $result = $conn->query($sql);
      $res = $result->fetch_assoc();
      //you can access customer information like res['contactFirstName']

     }
}

$conn->close(); //close your connection

?>

<!doctype html>
<html>
<head>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
    <title>test</title>
</head>
<body>

<?php
if($whichForm == 1){ //if it is first form
?>

<form id="1" action="test2.php" method="post">
     <!-- This input is needed to identify which form -->
     <input type="hidden" name="action" value="firstForm" />
     <p>First Name: <input type="text" name="firstname" value="<?php echo $fname; ?>"/>
     Last Name: <input type="text" name="lastname" value="<?php echo $lname; ?>" /></p>
     <p>Business Name: <input type="text" name="businessname" value="<?php echo $bname; ?>"/></p>
     <p>Contact Number: <input type="text" name="number" value="<?php echo $phone; ?>"/>
     Alt Number: <input type="text" name="altno" value="<?php echo $altphone; ?>"></p>
     <p>Email Address:<input type="text" name="email" value="<?php echo $email; ?>"></p>
     <p>Street No:<input tyep="text" name="streetno" value="<?php echo $streetno; ?>">
     Street Name:<input type="text" name="street" value="<?php echo $street; ?>"></p>
     <p>Suburb:<input type="text" name="suburb" value="<?php echo $suburb; ?>">
     Postal Code:<input type="text" name="postalcode" value="<?php echo $postcode; ?>">
     State: <input type="text" name="state" value="<?php echo $state; ?>"></p>
 **<p>Type of Service Required: <select id="category" name="category" > 
 <option value="nill">---Select Service---</option>
 <?php
      if ($result->num_rows > 0) {
          // output data of each row
          while($row = $result->fetch_assoc()) {
                //this if to save the value of the selected category
                if($row['jobType'] == $service)
                {
                     echo '<option value= "'.$row['jobType'].'" SELECTED>' . $row['jobType'] . '</option>';
                }
                else
                {
                     echo '<option value= "'.$row['jobType'].'">' . $row['jobType'] . '</option>';
                }

          }
      }
 ?>
    </select></p>
    <button type="submit">Submit</button>
</form>
<?php 
}//ending bracket for if($whichForm == 1)
else if($whichForm == 2){ // or just else will do fine 
?>

<form id="2" action="test2.php" method="post">
<!-- This input is needed to identify which form -->
<input type="hidden" name="action" value="secondForm" />
<!-- This input is needed to store the last customer id -->
<input type="hidden" name="last_customer_id" value="<?php echo $last_customer_id; ?>" />

<!-- here you put your second form -->

<button type="submit">Submit</button>
</form>
<?php 
}//ending bracket for else if($whichForm == 2)
?>
</body>
</html>

好的,那么你需要什么帮助呢?我想你需要这样的帮助:插入记录后:$database->insert_id。这将为你提供创建记录的id。谢谢你的回复我可能还没有说清楚,我想知道提交表1时使用什么语法。这是我到目前为止所做的工作的副本,我确信这是一种不正确的编写代码的方法,但它可以在数据库中工作。谢谢,我尝试了sql查询,并得到了我想要的结果,但我对如何创建必要的文件感到困惑。是你给我的两个不同文件的语法吗?或者可以将它们放在一个.php文件中?我不确定我应该在这些领域中输入什么$contactFirstName=''$contactLastName=''$商业名称=“”;非常感谢您花时间提供这些信息,我将实施它,并让您知道我的进展。
<?php

//after mysqli connection

$contactFirstName = '';
$contactLastName = '';
$businessName = '';

$sql_get_data = "SELECT * FROM customer ORDER BY customerID DESC limit 1 ";
$query_get_data = mysqli_query($conn, $sql_get_data);
while ($row = mysqli_fetch_array($query_get_data, MYSQLI_ASSOC)) {

    //specify which data you want to get from "customer" table 
    $contactFirstName = $row['contactFirstName'];
    $contactLastName = $row['contactLastName'];
    $businessName = $row['businessName'];
}
?>
<form id="form2" method="post">
    <input type="text" value="<?php echo 'contactFirstName'; ?>"
    <input type="text" value="<?php echo 'contactLastName'; ?>"
    <input type="text" value="<?php echo 'businessName'; ?>"
</form>
<?php
$servername = "";
$username = "";
$password = "";
$dbname = "";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT * FROM serviceType";
$result = $conn->query($sql);



/************** IMPORTANT *************/
$whichForm = 1;//to know which form to show
$last_customer_id = 0;//to store last customer id
//to store your customer information
$fname=$lname=$bname=$phone=$altphone=$email=$streetno=$street=$suburb=$postcode=$state=$service='';


//To handle post operations after clicking on post.
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
 $whichForm = 2; // to show second form

 //if it is first form
 if($_POST['action'] == 'firstForm')
 {
      $fname=$_POST['firstname'];
      $lname=$_POST['lastname'];
      $bname=$_POST['businessname'];
      $phone=$_POST['number'];
      $altphone=$_POST['altno'];
      $email=$_POST['email'];
      $streetno=$_POST['streetno'];
      $street=$_POST['street'];
      $suburb=$_POST['suburb'];
      $postcode=$_POST['postalcode'];
      $state=$_POST['state'];
      $service=$_POST['category'];
      //Preparing your insert query
      $sql = "INSERT INTO customer (contactFirstName,contactLastName,businessName,contactNumber,altNumber,email,streetNo,streetName,suburb,postalCode,state,serviceType) VALUES ('$fname','$lname','$bname','$phone','$altphone','$email','$streetno','$street','$suburb','$postcode','$state','$service')";
      $conn->query($sql); //insert into db
      //get last insert customer id and you already have your customer information
      //in the variables above $fname, $lname.....etc
      $last_customer_id = $conn->insert_id;
 }
 else{//do something with your second form

      //get last customer info
      $sql = "SELECT * FROM serviceType where id = ".$POST_['last_customer_id']."";
      $result = $conn->query($sql);
      $res = $result->fetch_assoc();
      //you can access customer information like res['contactFirstName']

     }
}

$conn->close(); //close your connection

?>

<!doctype html>
<html>
<head>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
    <title>test</title>
</head>
<body>

<?php
if($whichForm == 1){ //if it is first form
?>

<form id="1" action="test2.php" method="post">
     <!-- This input is needed to identify which form -->
     <input type="hidden" name="action" value="firstForm" />
     <p>First Name: <input type="text" name="firstname" value="<?php echo $fname; ?>"/>
     Last Name: <input type="text" name="lastname" value="<?php echo $lname; ?>" /></p>
     <p>Business Name: <input type="text" name="businessname" value="<?php echo $bname; ?>"/></p>
     <p>Contact Number: <input type="text" name="number" value="<?php echo $phone; ?>"/>
     Alt Number: <input type="text" name="altno" value="<?php echo $altphone; ?>"></p>
     <p>Email Address:<input type="text" name="email" value="<?php echo $email; ?>"></p>
     <p>Street No:<input tyep="text" name="streetno" value="<?php echo $streetno; ?>">
     Street Name:<input type="text" name="street" value="<?php echo $street; ?>"></p>
     <p>Suburb:<input type="text" name="suburb" value="<?php echo $suburb; ?>">
     Postal Code:<input type="text" name="postalcode" value="<?php echo $postcode; ?>">
     State: <input type="text" name="state" value="<?php echo $state; ?>"></p>
 **<p>Type of Service Required: <select id="category" name="category" > 
 <option value="nill">---Select Service---</option>
 <?php
      if ($result->num_rows > 0) {
          // output data of each row
          while($row = $result->fetch_assoc()) {
                //this if to save the value of the selected category
                if($row['jobType'] == $service)
                {
                     echo '<option value= "'.$row['jobType'].'" SELECTED>' . $row['jobType'] . '</option>';
                }
                else
                {
                     echo '<option value= "'.$row['jobType'].'">' . $row['jobType'] . '</option>';
                }

          }
      }
 ?>
    </select></p>
    <button type="submit">Submit</button>
</form>
<?php 
}//ending bracket for if($whichForm == 1)
else if($whichForm == 2){ // or just else will do fine 
?>

<form id="2" action="test2.php" method="post">
<!-- This input is needed to identify which form -->
<input type="hidden" name="action" value="secondForm" />
<!-- This input is needed to store the last customer id -->
<input type="hidden" name="last_customer_id" value="<?php echo $last_customer_id; ?>" />

<!-- here you put your second form -->

<button type="submit">Submit</button>
</form>
<?php 
}//ending bracket for else if($whichForm == 2)
?>
</body>
</html>