Google Ads

Inserting Data in PHP/MySQL v3 PHP Script


Inserting Data in PHP/MySQL v3 PHP Script

tutorial is going to be on how to insert data in to a MySQL table through PHP. This is in companionship with my ‘How To Retrieve Data’ tutorial found here .

‘Insert’:
To insert data in to a MySQL table, we have to use the ‘INSERT’ keyword command in our query statement.

Steps:
First we need to create the data we are going to insert in to the table. My previous tutorial used a scoring system (found here: ) the example so we are going to be using that table and insert some new ‘highscores’.

 

 
 
  1. $scores = array();
  2. for ($i=0;$i<3;$i++) {
  3.       array_push($scores, rand(1, 99999));
  4. }

 

As you can see, we now create three new scores held within a ‘scores’ array. Each score is using our random function and so will be different each time.

Next we are going to run through each of our scores and create a query for each one. The query should execute upon creation and so we don’t need to get any returns or results from it.

To insert data through a query, we type ‘INSERT INTO {tableName} VALUES({data})’, like so…

mysqli_query(“INSERT INTO `testtable` VALUES (”, ‘1000’)”);

 

 

 
 
  1. foreach ($scores as $key => $value) {
  2.       mysqli_query($con, "INSERT INTO `test` VALUES ('', '$value')");
  3. }

 

 

We could then use our retrieving data techniques to see the new information…

 

 
 
  1. $q = mysqli_query($con, "SELECT * FROM `test`"); //Gets all from table 'test'
  2. $scores2 = array();
  3. while ($row = mysqli_fetch_array($q)) {
  4.       //$row = next row of results/all rows from table 'test' within database 'fln'
  5.       echo "Before sorting... ".$row['score']."<br/>"; //Echo current $row's column value of 'id'.
  6.       array_push($scores2, $row['score']);
  7. }
  8. arsort($scores2);
  9. $ind = 0;
  10. foreach($scores2 as $key => $value) {
  11.       echo 'After sorting. ID: ' . $ind . '. Score: ' . "$value <br />";
  12.       $ind += 1;
  13. }

 

 

Full Source:

 

 
 
  1. <?php
  2.       $con = mysqli_connect('localhost', 'root', '', 'fln'); //server, username, password, database name
  3.       $scores = array();
  4.       for ($i=0;$i<3;$i++) {
  5.             array_push($scores, rand(1, 99999));
  6.       }
  7.       foreach ($scores as $key => $value) {
  8.             mysqli_query($con, "INSERT INTO `test` VALUES ('', '$value')");
  9.       }
  10.       $q = mysqli_query($con, "SELECT * FROM `test`"); //Gets all from table 'test'
  11.       $scores2 = array();
  12.       while ($row = mysqli_fetch_array($q)) {
  13.             //$row = next row of results/all rows from table 'test' within database 'fln'
  14.             echo "Before sorting... ".$row['score']."<br/>"; //Echo current $row's column value of 'id'.
  15.             array_push($scores2, $row['score']);
  16.       }
  17.       arsort($scores2);
  18.       $ind = 0;
  19.       foreach($scores2 as $key => $value) {
  20.       echo 'After sorting. ID: ' . $ind . '. Score: ' . "$value <br />";
  21.       $ind += 1;
  22.       }
  23. ?>

 

 

 


Related Post

Dynamically Add Input Field and Save in Database S... Dynamically Add Input Field and Save in Database Separately Using MySql PHP Script This is different from my last post. You can add input fields...
Desktop Monitoring Software With Screenshots Plus ... Desktop Monitoring Software With Screenshots Plus Cloud AdminDesktop Monitoring Software With Screenshots Plus Cloud AdminComplete windows desktop app...
PHP AJAX CRUD PHP Script Download PHP AJAX CRUD PHP Script Download In this tutorial you will learn how to create, insert, delete, and update data using AJAX in PHP and fetching dat...
AppStore – iOS Apps Affiliate Script PHP Scr... AppStore - iOS Apps Affiliate ScriptAppStore - iOS Apps Affiliate Script Discovering amazing apps has never been this fun, with the AppStore script y...
Quik new v1.3 – Fully Loaded Classified Ads ... Quik new v1.3 - Fully Loaded Classified Ads CMS PHP Script Its an attempt to give a perfect Classified Ads Posting script which is are user friendl...

Have any Question or Comment?

Leave a Reply

Your email address will not be published. Required fields are marked *

"