0

"users" と "expenses" の 2 つのテーブルを持つデータベースがあります。テーブル USERS: id - プライマリ、自動インクリメント。ユーザー名パスワードなど...

テーブル費用: id (これは自動インクリメントではありません)、ユーザー名、金額、日付の理由 -> その他のフィールド。

私は getuserfields() という関数を使用して USERS テーブルからフィールドを取得しています (すべての php で必要な core.php ファイルに格納されています):

if (!function_exists('getuserfield')) {
     function getuserfield($field) {  //
        //echo $_SESSION['user_id'];
        $query = "SELECT `$field` FROM `users` WHERE `id`='".$_SESSION['user_id']."'";
        /*$query2 = "SELECT `$field` FROM `expenses` WHERE `id`='".$_SESSION['user_id']."'";*/
        if ($query_run = mysql_query($query)) {
             if($query_result = mysql_result($query_run, 0, $field)){ // get the result form the query, row 0 (1 row) cause that is gonna return 1 row/
                  return $query_result;
               }

          }
  }
}

私のindex.phpファイルでは、変数を宣言しました:

if (loggedin()){
  $firstname =  getuserfield('firstname');
  .....
  $spent =  getuserfield('spent');      //until here they work as these are variables from users table

  $ammount =  getuserfield('ammount'); //the following are variables from expenses table, and are not working
  $date =  getuserfield('date');
  $reason =  getuserfield('reason');
  $username =  getuserfield('username');

私はこの関数を介してそれらをデータベースに書き込もうとしています:

$query = "INSERT INTO `expenses` VALUES (id,'".mysql_real_escape_string($username)."','".mysql_real_escape_string($ammount)."','".mysql_real_escape_string($date)."','".mysql_real_escape_string($reason)."' )";
if ($query_run = mysql_query($query)) {
    header ('Location: register_success.php');
    } else {
    echo 'Sorry, we couldn\'t add your expense for the moment.';
    }

もちろん、クエリが実行されないときにトリガーされるクエリからエコー メッセージを取得します。私は次のようなフォームからそれらを取得するために _POST を使用しています:

<form action="register.php" method="POST">
<strong>Add expense</strong> <br/>
Ammount: <br/> 
<input type="text" placeholder="Ammount" name="ammount" value="<?php echo isset($_POST['ammount'])?$_POST['ammount']:null; ?>"><br/>
Date: <br/> 
<input type="date" placeholder="Enter Date" name="date" value="<?php echo isset($_POST['date'])?$_POST['date']:null; ?>"><br/>
Reason of expense: <br/> 
<input type="text" placeholder="Reason for expense" name="reason" value="<?php echo isset($_POST['reason'])?$_POST['reason']:null; ?>"><br/>
<input type="submit" name="Submit">
</form>

私はかなり新しいので...何か提案はありますか?ありがとう

4

1 に答える 1