我想知道PHP中用于更新PostgreSQL数据库中的行的语法。我创建了一个登录页面,用于检查数据库中的用户名和密码,然后转到显示该用户名的数据库中所有用户信息的页面。我试图允许用户更改某些列,如密码,名称等。因此,我添加了另一个页面,其中包含要更改的每个列的字段。使用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>
'$ 1'是什么样的变量? – hjpotter92 2012-04-01 18:06:26
你需要确保你逃脱你的弦乐。我无法强调足够重要的是防止SQL注入攻击。你可以使用PHP的'addslashses()'或[PDO参数化查询](http://php.net/manual/en/pdo.prepared-statements.php)。 – slashingweapon 2012-04-01 18:22:11
为什么不使用pg_query_params()来避免SQL注入? addslashes()不是100%安全的,只有当您支持多个数据库品牌时,PDO才有用。 – 2012-04-01 18:52:26