Results 1 to 9 of 9

Thread: Day 14 - Storing Data in MySQL - Become PHP Expert in 30 days

  1. #1
    Join Date
    Sep 2003
    Posts
    3,040

    Default Day 14 - Storing Data in MySQL - Become PHP Expert in 30 days

    Storing Data in MySQL

    Today we will see how to store data into MySQL database. This is done with SQL command INSERT.

    The SYNTAX for SQL INSERT command is

    PHP Code:
    INSERT INTO TABLE_NAME SET field1=valuefield2=value,.... 
    To insert data into our chat table, use following command

    PHP Code:
    INSERT INTO `chatSET `name`='Some Name Here', `message`='Some message here'
    day_14_mysql_insert.php

    PHP Code:
    <?php

    $db_server 
    '127.0.0.1';
    $db_user 'fwhphp_user';
    $db_password 'k5BJRaX6SFbs';
    $db_name 'fwhphp_db';

    $mysqli = new mysqli($db_server$db_user$db_password$db_name);

    if (
    $mysqli->connect_errno) {
        echo 
    'Connect failed: ' $mysqli->connect_error;
        exit();
    }

    $sql 'INSERT INTO `chat` SET `name`=\'Anonymous\', `message`=\'Change this message, be creative\'';

    if (
    $mysqli->query($sql) === TRUE) {
        echo 
    '<h1>Data saved into MYSQL</h1>';
    } else {
        echo 
    '<h1>Failed to Insert Data to Database</h1>';
        echo 
    $mysqli->error;
    }
    To see the data you inserted, use the script we created on Day 13 - day_13_mysql_read.php

    Try inserting multiple data into the database.

    Find

    PHP Code:
    $sql 'INSERT INTO `chat` SET `name`=\'Anonymous\', `message`=\'Change this message, be creative\''
    Replace name and message, so we can insert more data into the table.

    For example

    PHP Code:
    $sql 'INSERT INTO `chat` SET `name`=\'Raju\', `message`=\'I asked you to change this text and storing more data into table, so you learn MySQL INSERT.\''
    We use \' to escape single quote.



    Become PHP Expert in 30 days
    FreeMarriage.com - Free Online Matrimonial
    FlashWebHost.com - Professional Web Hosting, Designing.

  2. #2
    Join Date
    Nov 2009
    Posts
    76,596

    Default

    http://php.flashwebhost.com/sherly/msqlmsg.php

    Code:
    <?php
    
    $db_server = '127.0.0.1';
    $db_user = 'fwhphp_user';
    $db_password = 'k5BJRaX6SFbs';
    $db_name = 'fwhphp_db';
    
    $mysqli = new mysqli($db_server, $db_user, $db_password, $db_name);
    
    if ($mysqli->connect_errno) {
        echo 'Connect failed: ' . $mysqli->connect_error;
        exit();
    }
    
    $sql = 'INSERT INTO `chat` SET `name`=\'Neena\', `message`=\'To acquire knowledge, one must study;
    but to acquire wisdom, one must observe\'';
    
    if ($mysqli->query($sql) === TRUE) {
        echo '<h1>Data saved into MYSQL</h1>';
    } else {
        echo '<h1>Failed to Insert Data to Database</h1>';
        echo $mysqli->error;
    }

  3. #3
    Join Date
    Sep 2003
    Posts
    3,040

    Default

    Quote Originally Posted by sherlyk View Post
    http://php.flashwebhost.com/sherly/msqlmsg.php

    Code:
    <?php
    
    $db_server = '127.0.0.1';
    $db_user = 'fwhphp_user';
    $db_password = 'k5BJRaX6SFbs';
    $db_name = 'fwhphp_db';
    
    $mysqli = new mysqli($db_server, $db_user, $db_password, $db_name);
    
    if ($mysqli->connect_errno) {
        echo 'Connect failed: ' . $mysqli->connect_error;
        exit();
    }
    
    $sql = 'INSERT INTO `chat` SET `name`=\'Neena\', `message`=\'To acquire knowledge, one must study;
    but to acquire wisdom, one must observe\'';
    
    if ($mysqli->query($sql) === TRUE) {
        echo '<h1>Data saved into MYSQL</h1>';
    } else {
        echo '<h1>Failed to Insert Data to Database</h1>';
        echo $mysqli->error;
    }
    Good, only problem is when ever some one visit the page, this data get inserted. That is not an issue as we have no real use for this MySQL database or table.

    It will be nice if we show an HTML form with 2 fields. Name and Message, when user submit, data get stored in database, so every time new message can be inserted to MySQL.

    For now, we understand how to store data into mysql, that will be fine for now.
    Become PHP Expert in 30 days
    FreeMarriage.com - Free Online Matrimonial
    FlashWebHost.com - Professional Web Hosting, Designing.

  4. #4
    Join Date
    Sep 2003
    Location
    india
    Posts
    11,527

    Default

    http://php.flashwebhost.com/annie/mysql_insert.php

    Code:
     <?php
    
    $db_server = '127.0.0.1';
    $db_user = 'fwhphp_user';
    $db_password = 'k5BJRaX6SFbs';
    $db_name = 'fwhphp_db';
    
    $mysqli = new mysqli($db_server, $db_user, $db_password, $db_name);
    
    if ($mysqli->connect_errno) {
        echo 'Connect failed: ' . $mysqli->connect_error;
        exit();
    }
    
    $sql = 'INSERT INTO `chat` SET `name`=\'Priya\', `message`=\'Optimism is essential to achievement and it is also the foundation of courage and true progress.
    \'';
    
    if ($mysqli->query($sql) === TRUE) {
        echo '<h1>Data saved into MYSQL</h1>';
    } else {
        echo '<h1>Failed to Insert Data to Database</h1>';
        echo $mysqli->error;
    }

  5. #5
    Join Date
    Sep 2003
    Posts
    3,040

    Default

    Quote Originally Posted by annie View Post
    http://php.flashwebhost.com/annie/mysql_insert.php

    Code:
     <?php
    
    $db_server = '127.0.0.1';
    $db_user = 'fwhphp_user';
    $db_password = 'k5BJRaX6SFbs';
    $db_name = 'fwhphp_db';
    
    $mysqli = new mysqli($db_server, $db_user, $db_password, $db_name);
    
    if ($mysqli->connect_errno) {
        echo 'Connect failed: ' . $mysqli->connect_error;
        exit();
    }
    
    $sql = 'INSERT INTO `chat` SET `name`=\'Priya\', `message`=\'Optimism is essential to achievement and it is also the foundation of courage and true progress.
    \'';
    
    if ($mysqli->query($sql) === TRUE) {
        echo '<h1>Data saved into MYSQL</h1>';
    } else {
        echo '<h1>Failed to Insert Data to Database</h1>';
        echo $mysqli->error;
    }
    Good, you can see inserted data on page

    http://php.flashwebhost.com/annie/mysql.php
    Become PHP Expert in 30 days
    FreeMarriage.com - Free Online Matrimonial
    FlashWebHost.com - Professional Web Hosting, Designing.

  6. #6
    Join Date
    Nov 2004
    Location
    India
    Posts
    65

    Default

    http://php.flashwebhost.com/stefin/mysql_connect.php
    Code:
    <?php 
    
    
    $db_server = '127.0.0.1'; 
    $db_user = 'fwhphp_user'; 
    $db_password = 'k5BJRaX6SFbs'; 
    $db_name = 'fwhphp_db'; 
    
    
    $mysqli = new mysqli($db_server, $db_user, $db_password, $db_name); 
    
    
    if ($mysqli->connect_errno) { 
        echo 'Connect failed: ' . $mysqli->connect_error; 
        exit(); 
    } 
    
    
    $sql = 'INSERT INTO `chat` SET `name`=\'Stefin\', `message`=\'Childhood is the happiest time of life. Be Happy and enjoy your Childhood.\''; 
    
    
    if ($mysqli->query($sql) === TRUE) { 
        echo '<h1>Data saved into MYSQL</h1>'; 
    } else { 
        echo '<h1>Failed to Insert Data to Database</h1>'; 
        echo $mysqli->error; 
    }

  7. #7
    Join Date
    Sep 2003
    Posts
    3,040

    Default

    Quote Originally Posted by stefin View Post
    http://php.flashwebhost.com/stefin/mysql_connect.php
    Code:
    <?php 
    
    
    $db_server = '127.0.0.1'; 
    $db_user = 'fwhphp_user'; 
    $db_password = 'k5BJRaX6SFbs'; 
    $db_name = 'fwhphp_db'; 
    
    
    $mysqli = new mysqli($db_server, $db_user, $db_password, $db_name); 
    
    
    if ($mysqli->connect_errno) { 
        echo 'Connect failed: ' . $mysqli->connect_error; 
        exit(); 
    } 
    
    
    $sql = 'INSERT INTO `chat` SET `name`=\'Stefin\', `message`=\'Childhood is the happiest time of life. Be Happy and enjoy your Childhood.\''; 
    
    
    if ($mysqli->query($sql) === TRUE) { 
        echo '<h1>Data saved into MYSQL</h1>'; 
    } else { 
        echo '<h1>Failed to Insert Data to Database</h1>'; 
        echo $mysqli->error; 
    }

    Nice, working perfectly and good message :)

    You can see the data from MySQL on page

    http://php.flashwebhost.com/stefin/mysql_1.php
    Become PHP Expert in 30 days
    FreeMarriage.com - Free Online Matrimonial
    FlashWebHost.com - Professional Web Hosting, Designing.

  8. #8
    Join Date
    Apr 2005
    Posts
    46,704

    Default

    http://php.flashwebhost.com/mini/mysql_data.php

    Code:
    <?php
    
    $db_server = '127.0.0.1';
    $db_user = 'fwhphp_user';
    $db_password = 'k5BJRaX6SFbs';
    $db_name = 'fwhphp_db';
    
    $mysqli = new mysqli($db_server, $db_user, $db_password, $db_name);
    
    if ($mysqli->connect_errno) {
        echo 'Connect failed: ' . $mysqli->connect_error;
        exit();
    }
    
    $sql = 'INSERT INTO `chat` SET `name`=\'Jiya\', `message`=\'My school days were the happiest days of my life\'';
    
    if ($mysqli->query($sql) === TRUE) {
        echo '<h1>Data saved into MYSQL</h1>';
    } else {
        echo '<h1>Failed to Insert Data to Database</h1>';
        echo $mysqli->error;
    }

  9. #9
    Join Date
    Feb 2007
    Posts
    26,214

    Default

    DAY 14 PHP PROGRAMMING

    http://php.flashwebhost.com/tom/day_14_mysql_insert.php

    PHP Code:

    <?php$db_server = '127.0.0.1';$db_user = 'fwhphp_user';$db_password = 'k5BJRaX6SFbs';$db_name = 'fwhphp_db';
    $mysqli = new mysqli($db_server, $db_user, $db_password, $db_name);
    if ($mysqli->connect_errno) {
    echo 'Connect failed: ' . $mysqli->connect_errno ;
    exit();
    }
    $sql = 'INSERT INTO `chat` SET `name` = \'Mohanlal\' , `message` = \'Nee Po Mone Dineshaa\'';
    if ($mysqli->query($sql) === TRUE) {
    echo ' DATA SAVED INTO MYSQL';
    } else {
    echo ' Failed to Insert Data to Database';
    echo $mysql->error; 
    }

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •