Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

php insert into multiple mysql tables

Featured Replies

i am trying to take data from a form and insert it into multiple mysql tables. here is the feilds and php script

 

author - auth_id, auth_first, auth_second, auth_telephone, auth_email

book - book_id, auth_id, book_genre_id, book_title, book_num_pages, book_synopsis, book_description, book_rating

book genre - book_genre_id, book_genre

 

that is all the fields i have in my database

 

this is the code i have, the problem is that it will insert a new author, but wont add in the book from the form.

 

<form id="contact-form" method="post" action="database.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
  $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[genre]', '$_POST[numpages]','$_POST[description]','$_POST[synopsis]')";


mysql_query($sql_book, $connection);
  
echo "1 record added";

mysql_close($connection);
?>

any help would be much appreciated

 

regards

 

collie

You're not checking for errors on the query, just echoing that a record was added regardless.

 

You're also not sanitizing user inputs, this script is just asking for SQL injection attacks. You can use http://php.net/manual/en/mysqli.real-escape-string.php to prevent this, and I've linked the mysqli version because you're using the mysql functions which will be removed from PHP soon - so you've got a little updating to do!

 

In terms of your query not running, there's no comma after book_genre_id and the POST numpages and genre are the wrong way round by the looks of it (but you really really really shouldn't be putting POST data straight into queries) :)

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.