data type not updating in mySQL Workbench

Viewed 1067

I cant seem to be able to change the datatype of my "first_name" column, I would like it to be VARCHAR(45), but it's not changing from INT(11) as seen in the diagram table and connection below.

Diagram picture of table Connection picture

I have tried to forward engineer the diagram several times, but it does not update from the initial first "iteration" of what is shown in the right-hand picture.

update: I have tried to delete the files, and rewriting the tables and forward engineering them with the correct information and it still doesn't change from an integer.... arghh.

Posted my code below:

PHP:

<?php

    //connection information
    $client = "localhost";
    $user = "root";
    $key = "root";
    $dbname = "formDB";


    // Attempt MySQL server connection
    $connection = new mysqli($client, $user, $key, $dbname);


    // Check connection
    if($connection === false){
        die("ERROR: Could not connect. " . mysqli_connect_error());
    }

    // Escape user inputs for security
    $first_name = mysqli_real_escape_string($connection, $_POST['first_name']);
    $last_name = mysqli_real_escape_string($connection, $_POST['last_name']);
    $email_address = mysqli_real_escape_string($connection, $_POST['email_address']);

    // attempt insert query execution
    $sql = "INSERT INTO person (first_name, last_name, email_address) VALUES ('$first_name', '$last_name', '$email_address')";

    if(mysqli_query($connection, $sql)){
        echo "Records added successfully!";
    } else{
        echo "ERROR! not able to execute: <br> $sql <br> " . mysqli_error($connection);
    }

    // close connection
    mysqli_close($connection);
?>

HTML:

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>form to db</title>
  </head>
  <body>

    <!-- create form -->
    <form class="details" action="insert.php" method="post">
      <p>
        <label for="first_name"> First name:</label>
        <input type="text" name="first_name" id="first_name">
      </p>
      <p>
        <label for="last_name">Last Name:</label>
        <input type="text" name="last_name" id="last_name">
      </p>
      <p>
        <label for="email_address">Email Address:</label>
        <input type="text" name="email_address" id="email_address">
      </p>

    <input type="submit" value="submit">
    </form>
  </body>
</html>
1 Answers

In MySQL, you can use the following syntax to change the datatype of an existing column:

alter table person modify first_name varchar(45);

There should not be conversion problems turning an int(11) to a varchar(45), even if your data contains data already.

Related