1

mysql と php を使用してユーザー登録システムを作成し、「date_expires」(DATE NOT NULL を使用) というユーザー テーブルに列を追加して、登録ユーザーの登録日を期限切れにします。my form ユーザーは登録期間を選択できます。例: 1 年、2 年、3 年。など。ユーザーがフォームを送信すると、登録期間の値を取得しました..このように

$registrationPeriod = $_POST['registration_period]; 

私の問題は、上記の値で有効期限をユーザーテーブルに挿入する方法ですか?

ユーザーテーブルにデータを挿入しようとしていますが、「date_expires」列でそれを行う方法がわかりません。

これはこれまでの私のコードです...

$q = "INSERT INTO users (username, email, pass, first_name, last_name, date_expires) 
      VALUES ('$u', '$e', '$p, '$fn', '$ln', ????????????? )";

誰かがこれについて私を助けてくれることを願っています..ありがとう。

4

2 に答える 2

2

2 つの方法で行うことができます。

PHP

使用strtotime("+2 years"):

$expireYears = 2;
$expireDate = strtotime("+" . $expireYears . " years");

MySQL

使用DATE_ADD(NOW(), INTERVAL 2 YEAR):

$expireYears = 2;
$q = "INSERT INTO users (username, email, pass, first_name, last_name, date_expires) 
      VALUES ('$u', '$e', '$p, '$fn', '$ln', DATE_ADD(NOW(), INTERVAL $expireYears YEAR))";
于 2013-01-21T14:35:50.510 に答える
1

あなた$_POST['registration_period']が次のようになった場合1 year, 2 year... 次に、整数値を最も簡単に取り除き、MySQLで日付計算を実行できNOW() + INTERVAL n YEARますn

// Extract it from the registration_period
// Since it is formatted as "n years" with a space between,
// we can split the string on the space.  list() assigns an array (returned from explode())
// to individual variables. Since we only actually need one of them (the number), 
// we can throw away the second (which is the string "years") by just giving list() one variable
// It still needs a placeholder for the second though, hence the extra comma.
list($years,) = explode(" ", $_POST['registration_period']);
// Make sure it is an int to protect against SQL injection...
$years = intval($years);

VALUES ()クエリで、リストの日付計算に数値を代入します。

INSERT INTO users (.......) VALUES (....., (NOW() + INTERVAL $years YEAR));

MySQLi や PDO など、準備済みステートメントをサポートする API への切り替えを検討してください。クエリの現在の形式で、すべてのクエリ入力変数が正しくサニタイズされ、SQL インジェクションに対してフィルター処理されていることを期待し、想定することしかできません。

$u = mysql_real_escape_string($_POST['u']);
$e = mysql_real_escape_string($_POST['e']);
// etc for all query vars...

(詳細についてはlist()

于 2013-01-21T14:35:57.137 に答える