1
<?php
  session_start();
  include('function.php');

  site::db();
  $type=$_GET['type'];
  $date=$_GET['date'];
  $amount=$_GET['amount'];

  switch($type) {
    case "in_park":
      $table = "lot_income";
      $col = "date,amount";
      $val = $date $type $amount;
      break;
  }

  $sql = mysql_query("INSERT INTO $table ($col) VALUES ('$val')");
  if(!$sql) {
    die(mysql_error());
  } 

  //header("LOCATION: dashboard.php")
?>

这不起作用,但我假设我将需要分解变量 val 但我如何将逗号也放在那里,这样我就可以将信息放在许多不同的字段中,而不仅仅是一个字段。

4

3 回答 3

1

改这个..

$val=$date $type $amount;

进入这个

$val= "'$date', '$amount'";

和thius

$sql=mysql_query("INSERT INTO $table ($col) VALUES ('$val')");

进入这个

$sql=mysql_query("INSERT INTO $table ($col) VALUES ($val)");
于 2012-03-31T18:34:50.753 回答
1

我认为您的 SQL 语句中缺少一列:

$col = "date, type, amount";

您需要相应地格式化 SQL 值:

$val = "'$date', '$type', '$amount'";

连接它们:

$sql = mysql_query("INSERT INTO $table ($col) VALUES ($val)");
于 2012-03-31T18:42:30.517 回答
1

我通常这样做:

$table = "lot_income";

$data = array(
    'date' => "'".mysql_real_escape_string($date)."'", // date
    'type' => intval($type), // integer
    'amount' => intval($amount), // integer
    'text' => "'".mysql_real_escape_string($sometext)."'" // string
    // etc
  );

// I tend to wrap the following statement in a function for code reuse

$resource = mysql_query(
    "INSERT INTO ".$table." (".implode(", ", array_keys($data).")"
    . "VALUES (".implode(", ", array_values($data).")"
   );

注意:对于转义值(为了避免 SQL 注入),使用 PHP 扩展PDOmysqli绑定变量会更容易/更安全。

于 2012-03-31T18:51:39.580 回答