【问题标题】:Writing values to another table from database将值从数据库写入另一个表
【发布时间】:2013-11-01 23:52:02
【问题描述】:

我有一个包含两个表的数据库:“用户”和“费用”。 表用户: 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>

我很新,所以...有什么建议吗?谢谢

【问题讨论】:

  • 另外,错误消息“抱歉,我们暂时无法添加您的费用。”加载索引文件时会立即弹出,并且仅在我单击提交按钮时才会弹出。
  • 你真的对每个变量做一个单独的查询吗?为什么不做一个select *,然后访问$row['field']
  • 您使用了一种非常糟糕的方法来获取您的用户数据。一次获取数据,而不是为每个字段运行查询
  • 您需要使您的INSERT 代码依赖于isset($_POST['Submit']),因此它仅在您提交表单时运行。
  • @Barmar 你是对的,我把查询放在 if (isset($_POST['Submit'])) {....} 中,就像你提到的那样。现在,它仅在单击提交按钮时触发。但是,我仍然收到错误消息,这可能意味着它没有连接到费用表。

标签: php html mysql database


【解决方案1】:

id 不是您的费用查询中的变量。您应该在查询中添加错误消息,以帮助您在将来进行调试。

$query = mysql_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)."'
   )")
or die ('Sorry, we couldn\'t add your expense for the moment: ' . mysql_error());

header ('Location: register_success.php');

另外你真的应该使用mysqlipdomysql is deprecated as of php 5.5.x 这样你就可以省去以后重写所有东西的麻烦了。

【讨论】:

  • 我明白了,我用这个替换了它,我得到了错误:注意:未定义的变量:第 89 行的 C:\xampp\htdocs\spr\index.php 中的 id 抱歉,我们不能暂时不要增加您的费用:您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 5 行的 ''csmarand', '', '', '' )' 附近使用正确的语法。它是否与 PDO 有关,每个人都指的是 MySQLi?如果我更换登录名,它会起作用吗?
  • 不,这是因为 $id 没有定义。如果它不是一个自动递增的表,那么你必须为 $id 变量分配一些东西。如果是自增表,则无需传递 ID,但需要指定字段列表。
  • 好的,我正在查看 PDO 并找到了一种连接到我的数据库的新方法。起初,我用它来连接: ....... 现在我像这样连接: .... 但我想我仍然需要进行大量更改才能使代码正常工作。如何替换 IF 语句?
猜你喜欢
  • 1970-01-01
  • 2011-03-30
  • 1970-01-01
  • 2015-03-31
  • 1970-01-01
  • 2014-11-21
  • 2012-10-25
  • 1970-01-01
  • 2023-04-10
相关资源
最近更新 更多