How to Insert a Decimal in PHP MySQL

How to Insert a Decimal in PHP MySQL

PHP provides many standard functions for handling MySQL databases. This means that inserting data into a MySQL database table using a PHP script requires only a few simple steps. Any data type allowable in MySQL can be included within such insert statements, including decimal numbers. You can carry out SQL inserts of decimal values on a MySQL database using PHP even if you have very little programming experience.

Instructions

    1

    Create your PHP script. If you have not already done so, create a new file in a text editor and save it with ".php" as the extension. Insert the following outline code, which connects to a MySQL database given the database details, username and password:

    //connect to and select the relevant database

    mysql_connect("localhost", "user", "pass") or die(mysql_error());

    mysql_select_db("dbname") or die(mysql_error());

    //further processing here

    ?>

    Alter the code to suit your database location, name and user details.

    2

    Build your SQL insert statement. To insert data into a MySQL database table, you need to create an SQL statement using the table name, structure and details of the data to be inserted. The following example reflects a database table named "object" with columns called "name" and "length" in which the "length" value is represented in the database as a decimal:

    $insert_statement="INSERT INTO object (name, length) VALUES('Carpet', 100.5)";

    This line does not actually execute the insert statement on the database, it simply builds it as a string variable. Sometimes in PHP programming, it can be easier to break a large task into smaller tasks and tackle each in turn.

    3

    Execute your insert statement on the database. Using the following syntax example, execute your insert statement:

    $insert_result=mysql_query($insert_statement);

    An insert statement in SQL is a type of query, so it is carried out using the same PHP syntax you would use to query a database. The script uses the existing connection and database selected already using the connection functions to execute the query specified.

    4

    Check the results of your insert statement. The "insert_result" variable stores the result of the query executed on your MySQL database table, so you can use it to provide feedback as follows:

    if($insert_result) echo "Insert successful!";

    else echo "Oops - something went wrong!";

    This is particularly useful while testing, as you can see at a glance whether your insert has worked.

    5

    Save your PHP script and upload it to your Web server. Browse to its address in a Web browser to test it. You should be able to see whether the insert is working by reading the feedback message displayed. If the insert is not working, check your syntax, including the database connection code, as well as the SQL insert statement, and try again until it functions correctly.

Blog Archive