-1

我试图将数据插入到两个表中,我需要在两个表中都有一个自动增量,一个是 book_id,它在 books 表中,另一个是 author 表中的 auth_id。

我需要将作者追溯到一本书。

这是我正在使用的 html 代码和 php 代码。

任何帮助将非常感激。

HTML 代码是:-

 <form id="contact-form" method="post" action="database3.php" >
      <label for="fname">First Name: <span class="required">*</span></label>  
      <input type="text" id="fname" name="fname" value="" placeholder="John" required="required" />
      <label for="auth-name">Second Name: <span class="required">*</span></label>  
      <input type="text" id="sname" name="sname" value="" placeholder="Doe" required="required" /><br />
      <label for="email">Email Address: <span class="required">*</span></label>  
      <input type="email" id="email" name="email" value="" placeholder="johndoe@example.com" required="required" />  
      <label for="telephone">Telephone: <span class="required">*</span></label>
      <input type="text" id="telephone" name="telephone" value="" placeholder="" required="required" /><br />

      <label for="genre">Genre:  <span class="required">*</span></label>  
      <select id="genre" name="genre" placeholder="please select one..." required="required">
        <option value="please-select">Please select one..</option>
        <option text="murder_mystery" value="1">Murder Mystery</option>  
        <option text="romance" value="2">Romance</option>  
        <option text="sci_fi" value="3">Sci-Fi</option>  
        <option text="horror" value="4">Horror</option>  
        <option text="thriller" value="5">Thriller</option>  
        <option text="screen_plays" value="6">Screen Play's</option>  
        <option text="poetry" value="7">Poetry</option>  
        <option text="childrens" value="8">Children's</option>  
        <option text="non_fiction" value="9">Non-Fiction</option>  
        <option text="comedy" value="11">Comedy</option>  
        <option text="other" value="10">Other</option>
      </select>
      <label for="numpages">Number of Pages: <span class="required">*</span></label>  
      <select id="numpages" name="numpages">  
        <option value="num-page">Please Select one..</option>  
        <option value="0-100">0-100</option>  
        <option value="100-300">100-300</option>  
        <option value="300-500">300-500</option>
        <option value="500">500+</option>
      </select><br />
      <label for="booktitle">Book Title: <span class="required">*</span></label>  
      <input type="text" id="booktitle" name="booktitle" value="" placeholder="John Doe" required="required" />
      <br />
      <label for="description">Book Description: <span class="required">*</span></label>  
      <textarea id="bookdescription" name="description" placeholder="max 40 words describing your work, this will effectively be how you sell your bookto the reader" required="required" data-minlength="20"></textarea>
      <br />
      <label for="synopsis">Book Synopsis: <span class="required">*</span></label>  
      <textarea id="synopsis" name="synopsis" placeholder="max 100 words of the synopsis of your book,to the reader"
       required="required" data-minlength="20"></textarea>
      <br />
      <label for="file">Filename:</label>
      <input type="file" name="file" id="file">
      <br />
      <label for="check_tc">Do you agree to the T&C<span class="required">*</span></label>
      <input type="checkbox" id="check_tc" name="check_tc" required="required" />
      <br />

      <input type="submit" value="submit" id="submit-button" />  
</form>

php代码是: -

<?php
$connection = mysql_connect("localhost", "root", "");
if(!$connection)
{
 die("database connection failed: " . mysql_error());
}

$db_select = mysql_select_db("good-read", $connection);
if(!$db_select)
{
die("database selection failed: " . mysql_error());
}

$sql = "INSERT INTO author (auth_first, auth_second, auth_email, auth_telephone)
VALUES
('$_POST[fname]','$_POST[sname]','$_POST[email]', '$_POST[telephone]')";

mysql_query($sql, $connection);

echo "1 record added";

$sql_book = "INSERT INTO book (book_title, book_num_pages, book_genre_id, book_description, book_synopsis)
VALUES
('$_POST[booktitle]', '$_POST[numpages]', '$_POST[genre]','$_POST[description]', '$_POST[synopsis]')";


mysql_query($sql_book, $connection);

echo "1 record added";

mysql_close($connection);
?>
4

1 回答 1

-3

mysql_insert_id()返回最后分配的自动增量 ID INSERT。使用它来填充行auth_id中的book列。另外,请注意我已重构您的代码以使用mysqli准备好的语句。这将有助于避免诸如注入攻击之类的事情。

$auth_id = mysql_insert_id($connection);
$sql_book = "INSERT INTO book (auth_id, book_title, book_num_pages, book_genre_id, book_description, book_synopsis)
    VALUES (?, ?, ?, ?, ?, ?)";
$stmt = $mysqli->prepare($sql_book);
$stmt->bind_param("ississ", $auth_id, $_POST[booktitle], $_POST[numpages], $_POST[genre], $_POST[description], $_POST[synopsis]);
$stmt->execute();
$stmt->close();
于 2013-10-02T20:40:03.207 回答