Alen
Monetization Innovator
2
MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1
200 XP
Introduction:
This 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 (http://www.sourcecodester.com/php/7510/retrieving-data-mysqlphp.html).
'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'.
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')");
We could then use our retrieving data techniques to see the new information...
Full Source:
Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.
Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.
FOR YOUR OWN SAFETY, PLEASE:
1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.
Download
This 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 (http://www.sourcecodester.com/php/7510/retrieving-data-mysqlphp.html).
'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'.
- $scores
=
array
(
)
;
- for
(
$i
=
0
;
$i
<
3
;
$i
++
)
{
- array_push
(
$scores
,
rand
(
1
,
99999
)
)
;
- }
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')");
- foreach
(
$scores
as
$key
=>
$value
)
{
- mysqli_query
(
$con
,
"INSERT INTO `test` VALUES ('', '$value
')"
)
;
- }
We could then use our retrieving data techniques to see the new information...
- $q
=
mysqli_query
(
$con
,
"SELECT * FROM `test`"
)
;
//Gets all from table 'test'
- $scores2
=
array
(
)
;
- while
(
$row
=
mysqli_fetch_array
(
$q
)
)
{
- //$row = next row of results/all rows from table 'test' within database 'fln'
- echo
"Before sorting... "
.
$row
[
'score'
]
.
"<br/>"
;
//Echo current $row's column value of 'id'.
- array_push
(
$scores2
,
$row
[
'score'
]
)
;
- }
- arsort
(
$scores2
)
;
- $ind
=
0
;
- foreach
(
$scores2
as
$key
=>
$value
)
{
- echo
'After sorting. ID: '
.
$ind
.
'. Score: '
.
"$value
<br />"
;
- $ind
+=
1
;
- }
Full Source:
- <?php
- $con
=
mysqli_connect
(
'localhost'
,
'root'
,
''
,
'fln'
)
;
//server, username, password, database name
- $scores
=
array
(
)
;
- for
(
$i
=
0
;
$i
<
3
;
$i
++
)
{
- array_push
(
$scores
,
rand
(
1
,
99999
)
)
;
- }
- foreach
(
$scores
as
$key
=>
$value
)
{
- mysqli_query
(
$con
,
"INSERT INTO `test` VALUES ('', '$value
')"
)
;
- }
- $q
=
mysqli_query
(
$con
,
"SELECT * FROM `test`"
)
;
//Gets all from table 'test'
- $scores2
=
array
(
)
;
- while
(
$row
=
mysqli_fetch_array
(
$q
)
)
{
- //$row = next row of results/all rows from table 'test' within database 'fln'
- echo
"Before sorting... "
.
$row
[
'score'
]
.
"<br/>"
;
//Echo current $row's column value of 'id'.
- array_push
(
$scores2
,
$row
[
'score'
]
)
;
- }
- arsort
(
$scores2
)
;
- $ind
=
0
;
- foreach
(
$scores2
as
$key
=>
$value
)
{
- echo
'After sorting. ID: '
.
$ind
.
'. Score: '
.
"$value
<br />"
;
- $ind
+=
1
;
- }
- ?>
Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.
Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.
FOR YOUR OWN SAFETY, PLEASE:
1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.
Download
You must upgrade your account or reply in the thread to view hidden text.