Note that there are some explanatory texts on larger screens.

plurals
  1. POMySQL INSERT command is converting inputs into different formats/values
    primarykey
    data
    text
    <p>I have an HTML input form that is being sent as a PHP_SELF and then stored on a MySQL table. The input field is dynamically controlled with JS. There are 4 inputs: number (text), amount (int), Approval (text), and Date (text). There inputs are all arrays when they are sent to the PHP file via POST. They're stored in a MySQL table with column fields as: ID (primary key), num (text(4)), amnt (decimal(8,2)), app (text(10)), and date (varchar(10)). I'm getting three significant errors when I try to run the page.</p> <ol> <li><p>If a number such a 0125 is entered in the number field on the form, it is stored as 125 in the MySQL table. This is regardless of the fact that the field is being stored as a text string.</p></li> <li><p>The Approval field works perfectly fine when only integers are entered, however, when only text or a combination of text and numbers is inserted, the MySQL query produces the following error: Error: Unknown column 'aft859' in 'field list'. For example, when 853234 is entered, everything goes through fine, but when aft859 is entered, the error is produced.</p></li> <li><p>When a date is entered, it gets inputted into the MySQL table as a decimal value. For example, a date of 08/07/2012 is saved as '0.00056802'.</p></li> </ol> <p>I have checked every line to make sure that nothing is being converted in the PHP or HTML process, and I've echoed every line to make sure that the values are being handled properly. After much debugging, I'm lead to believe that the following two sections might be causing my problem:</p> <pre><code>//Check To See If User Has Already Created Table $sql = "CREATE TABLE IF NOT EXISTS $tablename_cc ( ID int NOT NULL AUTO_INCREMENT, PRIMARY KEY(ID), cc_num TEXT(4), cc_amnt DECIMAL(8,2), cc_app TEXT(10), cc_date VARCHAR(10) );"; </code></pre> <p>or it might be this:</p> <pre><code>if (!mysql_query('INSERT INTO ' .$tablename_cc. '(cc_num, cc_amnt, cc_app, cc_date) VALUES '.implode(',',$pairs))) die('Error: ' . mysql_error()); else echo '&lt;strong&gt;', "Your information have been submitted and will be added to your account upon approval.", '&lt;/strong&gt;', "&lt;/br&gt;", "&lt;/br&gt;", "&lt;/br&gt;"; </code></pre> <p>I'm not too familiar with PHP, HTML, or MySQL (this is my first program ever), and I'm not sure if I've missed something. I tried to check all quotes and make sure they were right. I'm running all this in Wordpress, just in case that's culprit. Here is my code in its entirety for reference:</p> <pre><code>&lt;?php if(isset($_POST['submit'])) { //Get Current User Login global $current_user; $current_user = wp_get_current_user(); $ulog = $current_user-&gt;user_login; $tablename_cc = "cc_".$ulog; //Check To See If User Has Already Created Table $sql = "CREATE TABLE IF NOT EXISTS $tablename_cc ( ID int NOT NULL AUTO_INCREMENT, PRIMARY KEY(ID), cc_num TEXT(4), cc_amnt DECIMAL(8,2), cc_app TEXT(10), cc_date VARCHAR(10) );"; mysql_query($sql); $cc_num = $_POST['cc_num']; $cc_amnt = $_POST['cc_amnt']; $cc_app = $_POST['cc_app']; $cc_date = $_POST['cc_date']; $items = array_map(null,$cc_num,$cc_amnt,$cc_app,$cc_date); $pairs = array(); foreach ($items as $sub) { if(implode(',', $sub) != ",,,") $pairs[] = '('.implode(',', $sub).')'; } echo implode(',',$pairs); if (!mysql_query('INSERT INTO ' .$tablename_cc. '(cc_num, cc_amnt, cc_app, cc_date) VALUES '.implode(',',$pairs))) die('Error: ' . mysql_error()); else echo '&lt;strong&gt;', "Your information has been submitted and will be added to your account upon approval.", '&lt;/strong&gt;', "&lt;/br&gt;", "&lt;/br&gt;", "&lt;/br&gt;"; } ?&gt; &lt;!--raw--&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;&lt;/title&gt; &lt;script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"&gt;&lt;/script&gt; &lt;script src="jquery.maskedinput.js" type="text/javascript"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; $(document).ready(function() { $('#btnAdd').click(function() { var num = $('.ccinput').length; // how many "duplicatable" input fields we currently have var newNum = new Number(num + 1); // the numeric ID of the new input field being added // create the new element via clone(), and manipulate it's ID using newNum value var newElem = $('#input' + num).clone().attr('id', 'input' + newNum); // insert the new element after the last "duplicatable" input field $('#input' + num).after(newElem); // enable the "remove" button $('#btnDel').attr('disabled',''); $("*#date").mask("99/99/9999"); // business rule: you can only add 20 names if (newNum == 20) $('#btnAdd').attr('disabled','disabled'); }); $('#btnDel').click(function() { var num = $('.ccinput').length; // how many "duplicatable" input fields we currently have $('#input' + num).remove(); // remove the last element // enable the "add" button $('#btnAdd').attr('disabled',''); // if only one element remains, disable the "remove" button if (num-1 == 1) $('#btnDel').attr('disabled','disabled'); }); $("*#date").mask("99/99/9999"); }); &lt;/script&gt; &lt;/head&gt; &lt;body&gt; Please fill in your information in the form below and press submit. If you need to add more, please click the "Add" button at the bottom of the form. You may enter a maximum of 20 at a time. Leave all unused fields blank. &lt;form method="post" action="&lt;?php echo htmlentities($PHP_SELF); ?&gt;"&gt; &lt;fieldset&gt; &lt;legend&gt;Information:&lt;/legend&gt; &lt;div id="input1" class="ccinput"&gt; # (last 4 digits): &lt;input id="cnum" type="text" name="cc_num[]" maxlength="4" size="4" /&gt; CC Amount: &lt;input id="camnt" type="int" name="cc_amnt[]" /&gt; Approval Code: &lt;input id="appr" type="text" name="cc_app[]" maxlength="10" size="10" /&gt; Date: &lt;input id="date" type="text" name="cc_date[]" /&gt; &lt;/br&gt; &lt;/div&gt; &lt;div id="input2" class="ccinput"&gt; # (last 4 digits): &lt;input id="cnum" type="text" name="cc_num[]" maxlength="4" size="4" /&gt; CC Amount: &lt;input id="camnt" type="int" name="cc_amnt[]" /&gt; Approval Code: &lt;input id="appr" type="text" name="cc_app[]" maxlength="10" size="10" /&gt; Date: &lt;input id="date" type="text" name="cc_date[]" /&gt; &lt;/br&gt; &lt;/div&gt; &lt;div id="input3" class="ccinput"&gt; # (last 4 digits): &lt;input id="cnum" type="text" name="cc_num[]" maxlength="4" size="4" /&gt; CC Amount: &lt;input id="camnt" type="int" name="cc_amnt[]" /&gt; Approval Code: &lt;input id="appr" type="text" name="cc_app[]" maxlength="10" size="10" /&gt; Date: &lt;input id="date" type="text" name="cc_date[]" /&gt; &lt;/br&gt; &lt;/div&gt; &lt;div id="input4" class="ccinput"&gt; # (last 4 digits): &lt;input id="cnum" type="text" name="cc_num[]" maxlength="4" size="4" /&gt; CC Amount: &lt;input id="camnt" type="int" name="cc_amnt[]" /&gt; Approval Code: &lt;input id="appr" type="text" name="cc_app[]" maxlength="10" size="10" /&gt; Date: &lt;input id="date" type="text" name="cc_date[]" /&gt; &lt;/br&gt; &lt;/div&gt; &lt;div id="input5" class="ccinput"&gt; # (last 4 digits): &lt;input id="cnum" type="text" name="cc_num[]" maxlength="4" size="4" /&gt; CC Amount: &lt;input id="camnt" type="int" name="cc_amnt[]" /&gt; Approval Code: &lt;input id="appr" type="text" name="cc_app[]" maxlength="10" size="10" /&gt; Date: &lt;input id="date" type="text" name="cc_date[]" /&gt; &lt;/br&gt; &lt;/div&gt; &lt;div&gt; &lt;input type="button" id="btnAdd" value="Add" /&gt; &lt;input type="button" id="btnDel" value="Remove" /&gt; &lt;/div&gt; &lt;/fieldset&gt; &lt;input type="submit" value="Submit" name="submit" /&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; &lt;!--/raw--&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload