PHP_SELFとして送信されてMySQLテーブルに保存されるHTML入力フォームがあります。入力フィールドはJSで動的に制御されます。入力には、数値(テキスト)、金額(int)、承認(テキスト)、および日付(テキスト)の4つがあります。POSTを介してPHPファイルに送信される場合、入力はすべて配列です。これらは、ID(主キー)、num(text(4))、amnt(decimal(8,2))、app(text(10))、およびdate(varchar( 10))。ページを実行しようとすると、3つの重大なエラーが発生します。
フォームの数値フィールドに0125などの数値を入力すると、MySQLテーブルに125として格納されます。これは、フィールドがテキスト文字列として格納されているという事実とは関係ありません。
整数のみが入力されている場合、承認フィールドは完全に正常に機能しますが、テキストのみ、またはテキストと数字の組み合わせが挿入されている場合、MySQLクエリは次のエラーを生成します。エラー:「フィールドリスト」の不明な列「aft859」。たとえば、853234を入力するとすべて正常に処理されますが、aft859を入力するとエラーが発生します。
日付を入力すると、10進値としてMySQLテーブルに入力されます。たとえば、2012年8月7日の日付は「0.00056802」として保存されます。
PHPまたはHTMLプロセスで何も変換されていないことを確認するためにすべての行をチェックし、値が適切に処理されていることを確認するためにすべての行をエコーしました。多くのデバッグを行った後、次の2つのセクションが問題の原因である可能性があると思われます。
//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)
);";
またはそれはこれかもしれません:
if (!mysql_query('INSERT INTO ' .$tablename_cc. '(cc_num, cc_amnt, cc_app, cc_date) VALUES '.implode(',',$pairs)))
die('Error: ' . mysql_error());
else
echo '<strong>', "Your information have been submitted and will be added to your account upon approval.", '</strong>', "</br>", "</br>", "</br>";
私はPHP、HTML、またはMySQL(これは私の初めてのプログラムです)にあまり精通しておらず、何かを見逃したかどうかはわかりません。私はすべての引用をチェックして、それらが正しいことを確認しようとしました。それが原因である場合に備えて、私はこれらすべてをWordpressで実行しています。参考までに、私のコード全体を次に示します。
<?php
if(isset($_POST['submit']))
{
//Get Current User Login
global $current_user;
$current_user = wp_get_current_user();
$ulog = $current_user->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 '<strong>', "Your information has been submitted and will be added to your account upon approval.", '</strong>', "</br>", "</br>", "</br>";
}
?>
<!--raw-->
<html>
<head>
<title></title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
<script src="jquery.maskedinput.js" type="text/javascript"></script>
<script type="text/javascript">
$(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");
});
</script>
</head>
<body>
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.
<form method="post" action="<?php echo htmlentities($PHP_SELF); ?>">
<fieldset>
<legend>Information:</legend>
<div id="input1" class="ccinput">
# (last 4 digits): <input id="cnum" type="text" name="cc_num[]" maxlength="4" size="4" /> CC Amount: <input id="camnt" type="int" name="cc_amnt[]" /> Approval Code: <input id="appr" type="text" name="cc_app[]" maxlength="10" size="10" /> Date: <input id="date" type="text" name="cc_date[]" /> </br>
</div>
<div id="input2" class="ccinput">
# (last 4 digits): <input id="cnum" type="text" name="cc_num[]" maxlength="4" size="4" /> CC Amount: <input id="camnt" type="int" name="cc_amnt[]" /> Approval Code: <input id="appr" type="text" name="cc_app[]" maxlength="10" size="10" /> Date: <input id="date" type="text" name="cc_date[]" /> </br>
</div>
<div id="input3" class="ccinput">
# (last 4 digits): <input id="cnum" type="text" name="cc_num[]" maxlength="4" size="4" /> CC Amount: <input id="camnt" type="int" name="cc_amnt[]" /> Approval Code: <input id="appr" type="text" name="cc_app[]" maxlength="10" size="10" /> Date: <input id="date" type="text" name="cc_date[]" /> </br>
</div>
<div id="input4" class="ccinput">
# (last 4 digits): <input id="cnum" type="text" name="cc_num[]" maxlength="4" size="4" /> CC Amount: <input id="camnt" type="int" name="cc_amnt[]" /> Approval Code: <input id="appr" type="text" name="cc_app[]" maxlength="10" size="10" /> Date: <input id="date" type="text" name="cc_date[]" /> </br>
</div>
<div id="input5" class="ccinput">
# (last 4 digits): <input id="cnum" type="text" name="cc_num[]" maxlength="4" size="4" /> CC Amount: <input id="camnt" type="int" name="cc_amnt[]" /> Approval Code: <input id="appr" type="text" name="cc_app[]" maxlength="10" size="10" /> Date: <input id="date" type="text" name="cc_date[]" /> </br>
</div>
<div>
<input type="button" id="btnAdd" value="Add" />
<input type="button" id="btnDel" value="Remove" />
</div>
</fieldset>
<input type="submit" value="Submit" name="submit" />
</form>
</body>
</html>
<!--/raw-->