在我的Android应用程序中,有3种类型的UI。在后端我使用PHP在android中调用web服务。在我的情况下,有一个表即注册详细信息,其中列有名字,姓氏,电子邮件,密码,性别,城市,国家等。现在,我为插入3字段创建php Web服务即名字,姓氏或电子邮件。它已成功插入数据库。现在为了密码,生日性别我已经在null列上启动了更新查询然后它被成功插入但我的问题是当表有任何空字段然后用新插入的记录更新那个空字段。我不知道我的概念是对还是错,请指正或引导我。
Php代码
public function StoreInfo($firstname, $lastname, $email)
{
$stmt = $this->conn->prepare("INSERT INTO ibeInsert(firstname,lastname,email) VALUES(?,?,?)");
$stmt->bind_param("sss", $firstname,$lastname,$email);
$result = $stmt->execute();
$stmt->close();
if($result)
{
$stmt = $this->conn->prepare("SELECT firstname,lastname,email FROM ibeInsert WHERE email = ?");
$stmt->bind_param("s",$email);
$stmt->execute();
$stmt->bind_result($token2,$token3,$token4);
while( $stmt->fetch() )
{
$user["firstname"]=$token2;
$user["lastname"]=$token3;
$user["email"]=$token4;
}
$stmt->close();
return $user;
}
else
{
return false;
}
}
在php文件中调用函数:
<?php
require_once 'update_user_info.php';
$db = new update_user_info();
$response = array("error" => FALSE);
if (isset($_POST['firstname']) && isset($_POST['lastname']) && isset($_POST['email']))
{
// receiving the post params
$firstname = $_POST['firstname'];
$lastname =$_POST['lastname'];
$email = $_POST['email'];
// create a new user
$user = $db->StoreSocialInfo($firstname,$lastname,$email);
if ($user) {
// user stored successfully
$response["error"] = FALSE;
$response["user"]["firstname"] = $user["firstname"];
$response["user"]["lastname"]=$user["lastname"];
$response["user"]["email"] = $user["email"];
echo json_encode($response);
}
else
{
// user failed to store
$response["error"] = TRUE;
$response["error_msg"] = "Unknown error occurred in registration!";
echo json_encode($response);
}
} else {
$response["error"] = TRUE;
$response["error_msg"] = "Required parameters (Firstname,lastname,email) is missing!";
echo json_encode($response);
}
?>
现在我的第二部分(GENDER,COUNTRY,CITY)
Update query fired on same table
public function StoreInfoTo($gender,$country,$city)
{
$stmt = $this->conn->prepare("UPDATE ibeInsert SET gender=?,country=?,city=? WHERE id=? OR gender IS NULL OR country IS NULL OR city IS NULL");
$stmt->bind_param("sssi", $gender, $country, $city,$id);
$result = $stmt->execute();
$stmt->close();
if ($result) {
$stmt = $this->conn->prepare("SELECT gender, country,city,id FROM ibeInsert");
$stmt->execute();
$stmt-> bind_result($token2,$token3,$token4,$token1);
while ( $stmt-> fetch() ) {
$user["gender"] = $token2;
$user["country"]=$token3;
$user["city"] = $token4;
$user["id"]=$token1;
}
$stmt->close();
return $user;
} else {
return false;
}
}
在数据库中,当我将新记录插入数据库时,最后插入的记录会在性别国家/地区城市中更新:
id:1名称:riche姓氏:shah性别:女,国家:印度城市:孟买 id:2 name:xyz lastname:pos性别:female,国家:India city:Mumbai
当新记录插入数据库时,最后3列会更新。
答案 0 :(得分:0)
我猜问题在你的更新查询中,
$stmt = $this->conn->prepare("UPDATE ibeInsert SET gender=?,country=?,city=? WHERE id=? OR gender IS NULL OR country IS NULL OR city IS NULL");
我看不到您将ID传递给更新功能
public function StoreInfoTo($gender,$country,$city)
因为你正在使用OR条件到where子句,你将获得最后插入的值总是为null,每当你将触发更新时,它将获取这些空值并更新它们。
您不应在where子句中使用空值,而是按ID或任何索引值进行搜索以识别记录。
还建议不要对NULL值启动更新,因为您的表可能有多个空值,并且update将替换所有空值,除非您使用AND与表的标识索引。 希望这会有所帮助:)