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

时间:2022-09-25 19:09:54

The data inserts into the first table, but the code for getting the ID numbers doesn't seem to work, and the data is not inserted into the next two tables. The code runs and the Thank you message appears thanking the person for submitting their details.

数据插入到第一个表中,但获取ID号的代码似乎不起作用,并且数据未插入到下两个表中。代码运行,并显示感谢信息,感谢此人提交他们的详细信息。

There are three pages of code. The connect code is in one file. The processing code file and the form file.

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

I won't include the connect code here, because it works.

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

Here is the form code:

这是表单代码:

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>

The code for inserting the form data into the three database tables:

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

<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>

There are three database tables called cakeorder, customerdetails and preferred appointment. I don't think the multiple table insert works with earlier versions PHP, which is what I was using to start with, but I am now using xampp 5.5.24 and PHP 5.5.24. I stripped out most the formatting of the html, so I may have left a hanging tag somewhere here, but there isn't one on the actual web page. I am not very proficient in PHP, so a lot of this is put together from looking through this website. Any help would be gratefully received. Thank you

有三个数据库表,称为cakeorder,customerdetails和首选约会。我不认为多表插入适用于早期版本的PHP,这是我开始使用的,但我现在使用xampp 5.5.24和PHP 5.5.24。我删除了html的大部分格式,所以我可能在这里留下了悬挂标签,但实际网页上没有。我不是很精通PHP,所以很多这个都是通过浏览这个网站来组合在一起的。我们将非常感激地提供任何帮助。谢谢

1 个解决方案

#1


Thank you for your feedback. It's not for professional use, so I am not so worried about vulnerability it is just trying to get the stuff to work. As I said I don't know much about php code hence the mistake of using my_sqli_insert_id. It may be better to create a stored procedure, but I am just learning the basics at the moment.

感谢您的反馈意见。这不是专业用途,所以我并不担心漏洞只是试图让这些东西发挥作用。正如我所说,我对PHP代码知之甚少,因此使用my_sqli_insert_id的错误。创建存储过程可能更好,但我现在只是学习基础知识。

#1


Thank you for your feedback. It's not for professional use, so I am not so worried about vulnerability it is just trying to get the stuff to work. As I said I don't know much about php code hence the mistake of using my_sqli_insert_id. It may be better to create a stored procedure, but I am just learning the basics at the moment.

感谢您的反馈意见。这不是专业用途,所以我并不担心漏洞只是试图让这些东西发挥作用。正如我所说,我对PHP代码知之甚少,因此使用my_sqli_insert_id的错误。创建存储过程可能更好,但我现在只是学习基础知识。