在postgresql中更新表的特定区域

时间:2010-04-02 19:10:43

标签: php postgresql

好的我正在尝试更新postgresql中表的特定区域

我希望它找到与表一起使用的用户

然后更新我的信息

就像在这种情况下,电子邮件是它需要查找的用户名。

它需要添加$ aboutSelf,$ hobbies,$ music,$ tv,$ sports等领域

所以,我不知道怎么做这个lol ^。^我只知道如何从头开始添加东西。比如创建一个不存在的用户

CREATE TABLE chatterprofileinfo(
    Id SERIAL,
    email VARCHAR(255) NOT NULL PRIMARY KEY,
    aboutSelf VARCHAR(255),
    hobbies VARCHAR(255),
    music VARCHAR(255),
    tv VARCHAR(255),
    sports VARCHAR(255),
    lastLogin DATE
);

PHP目前正在使用

<?php

$error=false;

$aboutSelfError="";
$hobbiesError="";
$musicError="";
$tvError="";
$sportsError="";

if($_SERVER["REQUEST_METHOD"] == "GET") {

    $aboutSelf="";
    $hobbies="";
    $music="";
    $tv="";
    $sports="";
    $error=false;

}
else if($_SERVER["REQUEST_METHOD"] == "POST") {

    $error=false;

    $aboutSelf=trim($_POST["aboutSelfTA"]);
    $hobbies=trim($_POST["hobbiesTA"]);
    $music=trim($_POST["musicTA"]);
    $tv=trim($_POST["tvTA"]);
    $sports=trim($_POST["sportsTA"]);

    if(strlen($aboutSelf)>255) {

        $aboutSelfError="Maximum of 255 characters please shorten";

        $error=true;

    }

    if(strlen($hobbies)>255) {

        $hobbiesError="Maximum of 255 characters please shorten";

        $error=true;

    }

    if(strlen($music)>255) {

        $musicError="Maximum of 255 characters please shorten";

        $error=true;

    }

    if(strlen($tv)>255) {

        $tvError="Maximum of 255 characters please shorten";

        $error=true;

    }

    if(strlen($sports)>255) {

        $sportsError="Maximum of 255 characters please shorten";

        $error=true;

    }

}

?>

2 个答案:

答案 0 :(得分:2)

使用UPDATE查询?

我在大多数SQL参考资料中使用W3schools.com。非常方便的网站!

如果没有架构,可能会过于笼统,但不能更多。

答案 1 :(得分:2)

请参阅http://www.postgresql.org/docs/8.1/static/sql-update.html

UPDATE
  users
SET
  aboutSelf='...',
  hobbies='...',
  music='...',
  tv='...',
  sports='...'
WHERE
  email='something'

编辑:使用pg_prepare()的自包含示例:

$pg = pg_connect("dbname=test user=localonly password=localonly");
if ( !$pg ) {
  die('connect failed ');
}

// create a temporary/test table
pg_query($pg, '
  CREATE TEMPORARY TABLE tmpchatter (
    id SERIAL, 
    email varchar,
    aboutSelf varchar,
    hobbies varchar,
    UNIQUE (email)
  )
');

// fill in some test data
pg_query("INSERT INTO tmpchatter(email, aboutSelf, hobbies) VALUES ('emailA','aboutA','hobbiesA')") or die(pq_last_error($pg));
pg_query("INSERT INTO tmpchatter(email, aboutSelf, hobbies) VALUES ('emailB','aboutB','hobbiesB')") or die(pq_last_error($pg));
pg_query("INSERT INTO tmpchatter(email, aboutSelf, hobbies) VALUES ('emailC','aboutC','hobbiesC')") or die(pq_last_error($pg));

// let's see what we've got so far
$result = pg_query('SELECT email,aboutSelf,hobbies FROM tmpchatter') or die(pq_last_error($pg));
echo "query result #1:\n";
while ( false!==($row=pg_fetch_row($result)) ) {
  echo join(', ', $row), "\n";
}

// now let's update a specific record
// the "interesting" part

// first the parameters we want to use
$email = 'emailB';
$about = 'about me....';
$hobbies = 'breathing, eating';

// prepare the statement. Put placeholders where you want to "insert" parameters
pg_prepare($pg, '', '
  UPDATE
    tmpchatter
  SET
    aboutSelf = $1,
    hobbies = $2
  WHERE
    email = $3
') or die(pg_last_error());

// execute the statement + provide the parameters
// With prepared statements you don't have to worry about escaping the values to avoid sql injections
pg_execute($pg, '', array($about, $hobbies, $email)) or die(pg_last_error());

// let's check the result
$result = pg_query('SELECT email,aboutSelf,hobbies FROM tmpchatter') or die(pq_last_error($pg));
echo "query result #2:\n";
while ( false!==($row=pg_fetch_row($result)) ) {
  echo join(', ', $row), "\n";
}

打印

query result #1:
emailA, selfA, hobbiesA
emailB, selfB, hobbiesB
emailC, selfC, hobbiesC

query result #2:
emailA, selfA, hobbiesA
emailC, selfC, hobbiesC
emailB, about me...., breathing, eating