(PHP MySQL) INSERT INTO


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
  $username = "user_id";
  $database = "user_id";
  $password = "p-word";
  $host     = "undcemmysql.mysql.database.azure.com";

  // Initializing MySQLi
  $conn     = mysqli_init( );

  // Creating an SSL connection
  mysqli_ssl_set( $conn, NULL, NULL, "DigiCertGlobalRootCA.crt.pem", NULL, NULL );

  // Opening a new connection to the MySQL server
  mysqli_real_connect( $conn, $host, $username, $password, $database, 3306 );

  // Connect to the database.
  if ( $conn->connect_errno )
    die( 'Failed to connect to MySQL: ' . $conn->connect_error );

  $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( " " );
    }
    $sql = "SELECT * FROM  Reports";
    $result = $conn->query( $sql );
    // Print the results row by row.
    if ( mysqli_num_rows( $result ) > 0 )
      while ( $row = $result->fetch_assoc( ) )
        echo $row['reportID'] . " 🠞 " . $row['title'];
    else
      echo "Error selecting reports: " . $conn->error;
  }

  // Close the database connection.
  $conn->close( );
 ?>
 </body></html>
                     




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