简体   繁体   English

如何为两个或多个单独的MySQL表进行PHP插入

[英]How do I do a PHP insert for two or more separate MySQL tables

The HTML: HTML:

<html> 
<?php include 'C:\xampp\htdocs\paxdb\head.php';  
include 'config/menu.php';?>  
<div id="dataentry"> 

<!--This section is the demographic text field area--> 
<form method="post" action="dataentered.php"> 
First Name:&nbsp;<input type="text" name="First_Name"/></br> 
</br> 
Last Name:&nbsp;<input type="text" name="Last_Name"/></br> 
</br> 
E-mail:&nbsp;<input type="text" name="email"/></br> 
</br> 

<!--This section is the age range checkbox selection area--> 
<p><u><b>Age Range</b></u></p> 
<input type="checkbox" name="age[]" id="20-25" value="20-25"/>&nbsp;20-25</br> 
<input type="checkbox" name="age[]" id="26-30" value="26-30"/>&nbsp;26-30</br> 
<input type="checkbox" name="age[]" id="31-35" value="31-35"/>&nbsp;31-35</br> 
</div> 
<div id="checkboxes"> 
</div> 

<!--This section is the trips take checkbox area--> 
<div id="tripstodatetype"> 
<p><u><b>WHAT TYPE OF TRIPS TO DATE HAVE YOU TAKEN?</b></u></p> 
<input type="checkbox" name="trip2date[]" id="Bus" value="Bus">&nbsp;Bus&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</br> 
<input type="checkbox" name="trip2date[]" id="Car" value="Car">&nbsp;Car</br> 
<input type="checkbox" name="trip2date[]" id="Weekend fly-in" value="Weekend fly-in">&nbsp;Weekend fly-in&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</br> 
</div> 
<div id="tripstodateborder"> 
</div> 

<!--This section is the type of trip client likes best checkbox area--> 
    <div id="triplikebest"> 
<p><u><b>WHAT TYPE OF TRIP DO YOU LIKE BEST?</b></u></p> 
<input type="checkbox" name="triplikebest[]" value="Bus">&nbsp;Bus&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</br> 
<input type="checkbox" name="triplikebest[]" value="Car">&nbsp;Car</br> 
<input type="checkbox" name="triplikebest[]" value="Weekend fly-in">&nbsp;Weekend fly-in&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</br> 
</div> 
<div id="triplikeborder"> 
</div> 

and the PHP: 和PHP:

<html>
<?php
include 'head.php';
include 'config/menu.php'; 
$host="localhost";
$username="somename";
$password="somepass";
$dbname="pax";

$dbc = mysql_connect($host, $username, $password, $dbname); 
if (!$dbc) 
{ 
    die('Error connecting to MySQL server' . mysql_error()); 
    } 
mysql_select_db($dbname, $dbc); 

$first_name = mysql_real_escape_string($_POST['First_Name']);  
$last_name = mysql_real_escape_string($_POST['Last_Name']); 
$email = mysql_real_escape_string($_POST['email']); 
$age = $_POST['age']; 
$my_range = ""; 
foreach($age as $range) { 
   $my_range = $my_range . mysql_real_escape_string($range) . ", "; 
} 
$trip2date = $_POST['trip2date'];
$my_triprange = ""; 
foreach($trip2date as $triprange) { 
   $my_triprange = $my_triprange . mysql_real_escape_string($triprange) . ", "; 
} 
mysql_query("INSERT INTO `pax` (`First_Name`, `Last_Name`, `email`, `age`,`trip2date`)    
     VALUES('$first_name','$last_name','$email', '$my_range','$my_triprange')")  
     or die(mysql_error());  
mysql_close($dbc); 
?> 
<div class = "entered">
 <p>Success! Your Data Has Been Submitted.  Please click on <b>'DATA ENTRY'</b> above to enter another. </P>
 </div>
    <?php include 'footer.php';?>
    </div>  
    </div>
    </body>
</html>

If I were to put the triprange data into a separate table, how would I convert the INSERT query to perform the insert into the new table? 如果将三程表数据放入单独的表中,我将如何转换INSERT查询以执行向新表中的插入? (let's say the new/second table is called 'trip'). (假设新/第二张表称为“行程”)。 -OR- Does it make more sense to use a second INSERT query here? -或者-在这里使用第二个INSERT查询是否更有意义? If so, how should it appear to remain connected to the first table/ID 如果是这样,它应该如何保持与第一个表/ ID的连接

thank you in advance. 先感谢您。

looking at your code, are you trying to enter the same data twice into different tables? 查看您的代码,您是否要在同一张表中两次输入相同的数据?

if not you should make your query a variable, so the line would read something like this... 如果不是,则应将查询设为变量,这样该行将读取如下内容...

$query1 = mysql_query("INSERT INTO `pax` (`First_Name`, `Last_Name`, `email`, `age`,`trip2date`)    
     VALUES('$first_name','$last_name','$email', '$my_range','$my_triprange')")  
     or die(mysql_error());

you could look into repeating the insert statement for another table but use the same variables declared in the original code. 您可以考虑对另一个表重复执行insert语句,但要使用原始代码中声明的相同变量。

if i am on the wrong lines, could you maybe explain a little more? 如果我的路线不对,您可以再解释一下吗?

mysqli_multi_query();

Read the manual here: http://php.net/manual/en/mysqli.multi-query.php 在此处阅读手册: http : //php.net/manual/en/mysqli.multi-query.php

EDIT: 编辑:

<?php

  /* connection conf */
  // use p: if you want to have a persistent connection 
  // this will improve the speed and resource usage of opening a connection
  $host     = "p:localhost";
  $username = "somename";
  $password = "somepass";
  $dbname   = "pax";

  /* make connection */
  $lnk = mysqli_connect($host, $username, $password, $dbname);

  /* check connection */
  if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
  }

  /* prepare variables */
  $first_name = mysqli_real_escape_string($_POST['First_Name']);  
  $last_name  = mysqli_real_escape_string($_POST['Last_Name']); 
  $email      = mysqli_real_escape_string($_POST['email']); 
  $age        = $_POST['age']; 

  $my_range = ""; 
  foreach($age as $range) { 
    $my_range = $my_range . mysqli_real_escape_string($range) . ", "; 
  }

  $trip2date = $_POST['trip2date'];
  $my_triprange = ""; 
  foreach($trip2date as $triprange) { 
     $my_triprange = $my_triprange . mysqli_real_escape_string($triprange) . ", "; 
  }

  /* execute query */
  mysqli_query($lnk, "INSERT INTO `pax` (`First_Name`, `Last_Name`, `email`, `age`,`trip2date`) " .
                     "VALUES('$first_name','$last_name','$email', '$my_range','$my_triprange');"
               );  

  /* execute multi query INSERT */
  mysqli_multi_query($lnk, "INSERT INTO `pax` (`First_Name`, `Last_Name`, `email`, `age`,`trip2date`) " .
                           "VALUES('$first_name','$last_name','$email', '$my_range','$my_triprange');"
                           "COMMIT;"
                     );  

  /* execute multi query SELECT */
  $query  = "SELECT CURRENT_USER();";
  $query .= "SELECT `email` FROM `pax` ORDER BY `id` LIMIT 5";

  if (mysqli_multi_query($lnk, $query)) {
    do {
      /* store first result set */
      if ($result = mysqli_store_result($lnk)) {
        while ($row = mysqli_fetch_row($result)) {
          printf("%s\n", $row[0]);
        }
        mysqli_free_result($result);
      }
      /* print divider */
      if (mysqli_more_results($lnk))
       printf("-----------------\n");

    } while (mysqli_next_result($lnk));
  }  

  /* close conenction */
  mysqli_close($dbc); 

  // I used your code and some samples from the PHP manual.
  // Hope this piece of code helps you and others to understand mysqli better.
  // Thanks to PHP for having the best manual.

?>

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

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