Database Code

  Home arrow Database Code arrow insert data into mysql table
DATABASE CODE

insert data into mysql table
By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2002-05-01

    Table of Contents:

     
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement
    Once connected to a database call this function to add data to a table.

    usage:
    insertData($inputTable, $inputField, $inputValue)

    The first parameter is the table name.
    The second parameter is the name of the field that will have data added to it. This can be an array of field names if updating more than one field.
    The last parameter is the value to add to the table, again if adding more than one value make this parameter an array.
    If adding an array of values then the array of field names and the array of values to add should be the same size (as you would expect).

    By : everytime

    <?php
    function fixQuotes ($what = "")
    {
    $what = ereg_replace("'","''",$what);
    $counter = 0;
    while (eregi("\\\\'", $what) && $counter < 10)
    {
    $what = ereg_replace("\\\\'","'",$what);
    #print "<p>counter=[$counter] what=[$what]</p>\n";
    }

    return $what;
    };

    function insertData($inputTable, $inputField, $inputValue)
    {
    $i = 1;
    $fieldList = "";
    $valueList = "";
    $fieldCount = 0;

    $fieldCount = count($inputField);
    foreach ($inputField as $v)
    {
    $fieldList = $fieldList . $v;
    $i++;
    if ($i <= $fieldCount)
    {
    $fieldList = $fieldList . ", ";
    };
    };
    # reset local vars
    $i = 1;
    reset($inputField);

    foreach ($inputValue as $v)
    {
    $v = fixQuotes($v);
    $valueList = $valueList . chr(39) . $v . chr(39);
    $i++;
    if ($i <= $fieldCount)
    {
    $valueList = $valueList . ", ";
    };
    };


    $query = "INSERT INTO " . $inputTable . " (" . $fieldList . ") VALUES (" . $valueList . ");";

    print "<br />";
    #print $query;

    $result = mysql_query($query);
    if (!$result)
    {
    print "<p>Insert failed, dammit.</p>\n";
    }
    else
    {
    #print "<p>Inserted ok.</p>";
    };
    };
    ?>
    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

    More Database Code Articles
    More By Codewalkers

    blog comments powered by Disqus

    DATABASE CODE ARTICLES

    - Converting CSV Files to MySQL Insert Queries...
    - Examples and Tools for Database Design
    - Relationships, Entities and Database Design
    - Modeling and Designing Databases
    - Data extract to Excel
    - Oracle database class 0.76
    - The opposite of mysql_fetch_assoc
    - On line Thermal Transmitance Calculation
    - pjjTextBase
    - PHP Object Generator
    - FastMySQL
    - RC4PHP
    - SQL function with integrated sprintf()
    - DB Interaction Classes v1.1
    - deeMySQLParser


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 5 - Follow our Sitemap