【发布时间】:2013-08-23 10:35:37
【问题描述】:
我是这个网站和编码的新手,所以如果你遇到一些菜鸟错误,请放轻松。
我有一个表单,提交时会将数据插入两个单独的表(用户、用户地址)。用户地址应通过用户 ID 链接回用户。
我已经看到了几种不同的方法可用于解决此类问题(没有哪一种 IC 可以工作),但我正在寻求帮助,看看哪种方法是最好的。
这是我目前所拥有的:
public function createNewUser($details, $active)
{
$password = $details["password"];
$username = strtolower($details["username" ]);
$firstname = strtolower($details["firstname"]);
$lastname = strtolower($details["lastname" ]);
$email = strtolower($details["email" ]);
$sex = strtolower($details["sex" ]);
$datepicker = strtolower($details["datepicker" ]);
$disabled = ($active) ? "0" : "1";
$address1 = strtolower($details["address1" ]);
$address2 = strtolower($details["address2" ]);
$province = strtolower($details["province" ]);
$city = strtolower($details["city" ]);
$district = strtolower($details["district" ]);
$zipcode = strtolower($details["zipcode" ]);
$
$sql = "INSERT INTO users VALUES (NULL, LOWER('$username'), MD5('$password'), LOWER('$firstname'), LOWER('$lastname'), LOWER('$email'), LOWER('$sex'), LOWER('$datepicker'), 0, NOW(), $disabled, 0)";
$resultSet = $this->db->query($sql);
return $this->db->getInsertId();
$sql = "INSERT INTO users_addresses VALUES (NULL, LOWER('$userid'), LOWER('$address1'), LOWER('$address2'), LOWER('$province'), LOWER('$city'), LOWER('$district), LOWER('$zipcode')";
$resultSet = $this->db->query($sql);
return $this->db->getInsertId();
}
【问题讨论】:
-
您非常想将值设为小写。您首先在 PHP 中使用
strtolower,然后在 MySQL 查询中使用LOWER()。奇怪。 -
越低越好哈哈:D
标签: php mysql insert transactions