PHP MySQL Insert


The INSERT INTO statement is used to insert new records into a database table. The following example performs the tasks below:
  1. Drop the table Reports if requested.
  2. Create the table Reports if requested. The SQL command is as follows:

    CREATE TABLE Reports (
      reportID INT NOT NULL AUTO_INCREMENT,
      title    VARCHAR(32) NOT NULL,
      PRIMARY KEY( reportID ) );

  3. Insert the title values from the Web and then display the table contents. The strtok function splits a string into smaller strings.
           
 <html><body>
 <?php
  $servername = "undcsmysql.mysql.database.azure.com";
  $username   = "user.id@undcsmysql";
  $password   = "password";
  $dbname     = "user_id";         # your database or schema
  // Connect to the database.
  $conn       = new mysqli( $servername, $username, $password, $dbname );

  $action =  Drop    Create                          
             Insert ( $titles = "" ) 
  if ( $action == "Drop" ) {    
    // Drop table Reports.
    $conn->query( "DROP TABLE Reports" );
    echo "Table Reports dropped";
  }
  elseif ( $action == "Create" ) {
    // Create table Reports.
    $sql = "CREATE TABLE Reports (
              reportID INT NOT NULL AUTO_INCREMENT,
              title    VARCHAR(32) NOT NULL,
              PRIMARY KEY( reportID ) )";
    $conn->query( $sql );
    echo "Table Reports created";
  }
  elseif ( $action == "Insert" ) {
    // Insert reports.
    $title = strtok( $_POST['titles'], " " );
    while ( $title != false ) {
      $sql = "INSERT INTO Reports( title ) VALUES( '$title' )";
      $conn->query( $sql );
      $title = strtok( " " );
    }
    // Select all reports.
    $result = $conn->query( "SELECT * FROM Reports" );
    while ( $row = $result->fetch_assoc( ) )
      echo $row['reportID'] . ", " . $row['title'];
  }
  $conn->close( );
 ?>
 </body></html>




      I’m addicted to brake fluid, but I can stop whenever I want.