(PHP MySQL) UPDATE


The UPDATE statement is used to modify data in a database table. The following example creates and populates an Items table using the following SQL commands:

CREATE TABLE Items (
  ID   INT AUTO_INCREMENT PRIMARY KEY,
  name VARCHAR(16) NOT NULL );
   
INSERT INTO Items( name ) VALUES( '$name1' );

and updates the item using:

UPDATE Items SET name='$name2' WHERE ID=$ID;

 <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 table    Create table    Select all            
             Insert ( name = "" )                         
             Update ( SET name = "" WHERE ID = "")      
  if ( $action == "Drop" ) {
    // Drop table Items.
    $conn->query( "DROP TABLE Items" );
    echo "Table Items dropped";
  }
  elseif ( $action == "Create" ) {
    // Create table Items.
    $sql = "CREATE TABLE Items (
              ID   INT AUTO_INCREMENT PRIMARY KEY,
              name VARCHAR(16) NOT NULL )";
    echo $sql;
    $conn->query( $sql );
    echo "Table Items created";
  }
  elseif ( $action == "Select all" ) {
    // Select all.
    $sql = "SELECT * FROM Items";
    echo $sql;
    $result = $conn->query( $sql );
    while ( $row = $result->fetch_assoc( ) )
      echo $row['ID'] . " 🠞 " . $row['name'];
  }
  elseif ( $action == "Insert" ) {
    // Insert item.
    $sql = "INSERT INTO Items( name ) VALUES( '$name1' )";
    $conn->query( $sql );
    echo "Item $name1 entered"
 
    // Select all.
    $result = $conn->query( "SELECT * FROM Items" );
    while ( $row = $result->fetch_assoc( ) )
      echo $row['ID'] . " 🠞 " . $row['name'];
  }
  elseif ( $action == "Update" ) {
    // Update item.
    $sql = "UPDATE Items SET name='$name2' WHERE ID=$ID";
    $conn->query( $sql );

    // Select all.
    $result = $conn->query( "SELECT * FROM Items" );
    while ( $row = $result->fetch_assoc( ) )
      echo $row['ID'] . ", " . $row['name'];
  }

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




      For me trying to have just one beer is kinda like    
      trying to fall down just one step of a staircase.    
      — Tommy Gill