繁体   English   中英

使用php将数据从表单插入到mysql数据库的多个表中

[英]Inserting data from a form to multiple tables in a mysql database using php

数据插入到第一个表中,但是获取ID号的代码似乎不起作用,并且数据也没有插入到接下来的两个表中。 代码将运行,并显示“谢谢”消息,感谢此人提交了他们的详细信息。

有三页代码。 连接代码位于一个文件中。 处理代码文件和表单文件。

我不会在此处包含连接代码,因为它可以工作。

这是表单代码:

enter code here

    <form method="post" action="formprocess3.php">
    <table>
            <tr>
          <td>Customer Details</td>
          <td>Appointment Preference</td>
          <td>Cupcake Details</td>
    </tr>
    <tr>
      <td>First Name             
      <input name="FirstName" type="text" id="FirstName" maxlength="20" value="<?php if (isset($_POST['FirstName'])) echo $_POST ['FirstName']; ?>"/>
               </td>
      <td>Appointment Date            
              <input name="AppointmentDate" type="date"  id="AppointmentDate" maxlength="10" value="<?php if (isset($_POST['AppointmentDate'])) echo $_POST['AppointmentDate']; ?>"/>
            </td>
           <td>Size     
    <select name="CupcakeSize" id="CupcakeSize" type="radio" maxlength="5" value="<?php if (isset($_POST['CupcakeSize'])) echo $_POST['CupcakeSize']; ?>"/>
            <option></option>
            <option>Small</option>
            <option>Large</option>
            </select></td>
      </tr>
      <tr>
        <td>Surname
             <input name="Surname" type="text"  id="Surname"  maxlength="20" value="<?php if (isset($_POST['Surame'])) echo $_POST['Surname']; ?>"/></td>
          <td>Appointment Time
         <select name="AppointmentTime" type="radio" maxlength="20" value="<?php if (isset($_POST['AppointmentTime'])) echo $_POST ['AppointmentTime']; ?>"/>

              <option></option>
              <option>9.30am -10.30am</option>
              <option>11am - 12pm</option>
              <option>1.30pm - 2.30pm</option>
              <option>3pm - 4pm</option>
              <option>4.30pm - 5.30pm</option>
              <option>7pm - 8pm</option>
            </select>
          </td>
          <td>Quantity             
          <input type="text" name="Quantity" id="Quantity"/></td>
            </tr>
        <tr>
          <td>Email address 
          <input name="EmailAddress" type="email"  id="Email" maxlength="20" value="<?php if (isset($_POST['EmailAddress'])) echo $_POST['EmailAddress']; ?>"/></td>
          <td>Taster 

            <input name="Taster" type="checkbox" id="Taster"/>
             </td>
            <td maxlength="1" type="radio" value="<?php if (isset($_POST['Taster'])) echo $_POST['Taster']; ?>"/>
                     <td>Frosting           
        <select name="CupcakeFrosting" id="CupcakeFrosting" type="radio" maxlength="10" value="<?php if (isset($_POST['CupcakeFrosting'])) echo $_POST['CupcakeFrosting']; ?>"/>
            <option></option>
            <option>Strawberry</option>
            <option>Chocolate</option>
            <option>Vanilla</option>
            <option>Coffee</option>
            <option>Orange</option>
            <option>Blue</option>
            <option>Pink</option>
            <option>Green</option>
            <option>Red</option>
            <option>Purple</option>
            </select></td>
                 </tr>
              <tr>
            <td>Postcode            
          <input name="Postcode" type="text" id="Postcode" style="width: 130px; height: 20px" class="auto-style24" maxlength="10" value="<?php if (isset($_POST['Postcode'])) echo $_POST['Postcode']; ?>"/></td>
      <td>Cake wanted by
      <input name="CakeWantedBy" type="date" id="CakeWantedBy" maxlength="10" value="<?php if (isset($_POST['CakeWantedBy'])) echo $_POST['CakeWantedBy']; ?>"/>
              </td>
          <td>
            <select name="CupcakeFlavour" id="Flavour" type="radio" maxlength="10" value="<?php if (isset($_POST['CupcakeFlavour'])) echo $_POST['CupcakeFlavour']; ?>"/>
            <option></option>
            <option>Banana</option>
            <option>Caramel</option>
            <option>Carrot</option>
            <option>Chocolate</option>
            <option>Vanilla</option>
            <option>Red Velvet</option>
            <option>Oreo</option>
            <option>Coffee</option>
            <option>Decide with taster £20</option>
            </select></td>
            </tr>
        <tr>
            <td>
            <input name="MobileNumber" type="text" id="MobileNumber" maxlength="20" value="<?php if (isset($_POST['MobileNumber'])) echo $_POST['MobileNumber']; ?>"/>          
                            </td>
                             <td>
            <span class="auto-style24">Occasion 
            <select name="Occasion" type="radio" id="Occasion" maxlength="20" value="<?php if (isset($_POST['Occassion'])) echo $_POST['Occassion']; ?>"/>
            <option></option>
            <option>New baby</option>
            <option>Birthday</option>
            <option>Wedding</option>
            <option>New Job</option>
            <option>Christmas</option>
            <option>Easter</option>
            <option>Valentines</option>
            <option>Congratulations</option>
            <option>Anniversary</option>
            <option>Other</option>
            </select></td>
      </tr>
    </table>
    </form>

用于将表单数据插入三个数据库表的代码:

<html>
<head>
<title>Form Process Message</title>
</head><body>
<?php # 

// This script performs an INSERT query to add a record to the users table.


 if ($_SERVER['REQUEST_METHOD'] == 'POST') {

 // open the database...

 require ('mysqli_connect.php'); 

 // Make the query:

// Customer details

 $t = $_POST[Title];
 $fn = $_POST[FirstName];
 $sn = $_POST[Surname];
 $e = $_POST[EmailAddress];
 $ht = $_POST[HomeTelephone];
 $mn = $_POST[MobileNumber];
 $hn = $_POST[HouseNumberName];
 $s = $_POST[Street];
 $tw = $_POST[Town];
 $c = $_POST[County];
 $pc = $_POST[Postcode];

 // Cake details
 $ct = $POST[CupcakeType];
 $cn = $_POST[CupcakeNumber];
 $cf = $_POST[CupcakeFrosting];
 $o = $_POST[Occassion];

 // Preferred Appointment
 $ad = $_POST[AppointmentDate];
 $at = $_POST[AppointmentTime];
 $ta = $_POST[Taster];
 $cwb = $_POST[CakeWantedBy];

 $q = "INSERT INTO customerdetails(Title, FirstName, Surname, EmailAddress,   HomeTelephone, MobileNumber, HouseNumberName, Street, Town, County, Postcode) VALUES ('$t','$fn', '$sn', '$e', '$ht', '$mn', '$hn', '$s', '$tw', '$c', '$pc')";

 //execute query        
 $r = @mysqli_query ($dbc, $q); 

 //get customer id for preferred appointment 
$ci = my_sqli_insert_id($dbc);

 $q1 = "INSERT INTO cakedetail(CupcakeType, CupcakeNumber, CupcakeFrosting, Occassion) VALUES ('$ct','$cn', '$cf', '$o')";

//execute query
$r1 = @mysqli_query ($dbc, $q1);

//get cakedetail id for preferred appointment
$cdi = my_sqli_insert_id($dbc);

$q2 = "INSERT INTO preferredappointment(AppointmentDate, AppoitmentTime, Taster, CakeWantedBy, EmailAddress) VALUES ($ci, $cdi, '$ad','$at', '$ta', '$cwb', '$e')";

//execute query 
$r2 = @mysqli_query ($dbc, $q2);  

// Run the query.

if ($r) { 

// If it ran OK.
// Print a message:


echo '<h1>Thank you!

<br />
Your request is now registered.
<br />  
<a href="gallery.html">Back to the Gallery page</a></h1>';      
        }       
 else { 

// If it did not run OK.
// Public message:  

 echo '<h1>System Error</h1>

 <p class="error">You could not be registered due to a system error. We apologise for any inconvenience.</p>

 <a href="gallery.html">Back to the Gallery page</a>';      
 // Debugging message:

 echo '<p>' . mysqli_error($dbc) . '<br /><br />
 Query: ' . $q . '</p>';

    } 
 //close the dbc        
 mysqli_close($dbc); 
}

  ?>
  </body>
  </html>

有三个数据库表,分别称为cakeorder,customerdetails和首选约会。 我认为多表插入不适用于早期版本的PHP,这是我最初使用的版本,但现在使用的是xampp 5.5.24和PHP 5.5.24。 我删除了大多数html格式,所以我可能在这里的某个地方留下了一个吊牌,但实际的网页上没有。 我不是很精通PHP,因此通过浏览此网站可以将许多内容整合在一起。 任何帮助将不胜感激。 谢谢

感谢您的反馈意见。 它不是用于专业用途,所以我对漏洞的担心不是那么担心,它只是想使这些东西正常工作。 正如我说的那样,我对php代码不太了解,因此使用my_sqli_insert_id的错误。 创建存储过程可能会更好,但是我现在只是在学习基础知识。

暂无
暂无

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

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