Book Image

Practical Web Development

By : Paul Wellens
Book Image

Practical Web Development

By: Paul Wellens

Overview of this book

Table of Contents (23 chapters)
Practical Web Development
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
10
XML and JSON
Index

Adding data


We mentioned using phpMyAdmin to propagate our tables. Once our application is running, we need to know how to add a date using PHP code, for instance, to add an order to the system. We will provide an example that adds a book to the books table. All the strings we use can, of course, be replaced by PHP variables. The SQL command we need here is INSERT:

<?php
$insertsql = 'INSERT INTO books  (title, author_id, price) VALUES ("My new book", "3", "38);'
$mysqli->query($insertsql);
$newestbook = $mysqli->insert_id;
?>

This will automatically add a row to your table, create your primary key with a value one higher than all others, insert the title, author_id, and insert the price into that row. All other fields will get the default value you specified while building your tables with phpMyAdmin.

The insert_id function is quite handy if you want to retrieve the value of the newly created primary key. We will use this now to change the price from 38 to 39.

Updating data

Let's...