我想知道在 PHP 中更新 PostgreSQL 数据库中的行的语法是什么。我制作了一个登录页面,用于检查数据库中的用户名和密码,然后转到一个页面,该页面显示该用户名的数据库中的所有用户信息。我试图允许用户更改某些列,如密码、名称等。因此我添加了另一个页面,其中包含我要更改的每个列的字段。
这是我的查询代码:
if(array_key_exists('save',$_POST))
{
$firstname=$_POST['ifirstname'];
$lastname=$_POST['ilastname'];
$email=$_POST['iemail'];
$password=$_POST['ipassword'];
$conn_string='host=#### port=#### dbname=###### user=####### password=######';
$dbconn=pg_connect($conn_string) or die('Connection failed');
$query="UPDATE project.customer SET FirstName='$firstname',
LastName='$lastname',Email='$email',Password='$password')
WHERE UserName=$1";
$result=pg_query($dbconn,$query);
$row_count= pg_num_rows($result);
pg_free_result($result);
pg_close($dbconn);
}
这是针对字段的:
<div id="header">UPDATE USER INFO</div>
<form id="testform" name="testform" method="post" action="" >
<p> <label for="ifirstname">First Name:</label>
<input name="ifirstname" type="text" id="ifirstname"/>
</p>
<p> <label for="ilastname">Last Name:</label>
<input name="ilastname" type="text" id="ilastname"/>
</p>
<p> <label for="iemail">E-Mail:</label>
<input name="iemail" type="text" id="iemail"/>
</p>
<p>
<label for="ipassword">Password:</label>
<input name="ipassword" type="password" id="ipassword"/>
</p>
<p>
<label for="iconfpass">Confirm Password:</label>
<input name="iconfpass" type="password" id="iconfpass"/>
</p>
<p>
<input type="submit" name="save" value="Register"/>
</p>
</form>
最佳答案
我想一定是这样的。出于安全原因,还让用户在更改数据时写入旧密码。也不要忘记在查询中使用之前过滤您的数据以避免 sql 注入(inject)攻击
$query="UPDATE project.customer
SET (FirstName,LastName,Email,Password) =
('$firstname','$lastname','$email','$password')
WHERE UserName= '$1' and Password = '$oldpassword'";
关于php - 使用 PHP 更新 PostgreSQL 中的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9966853/