在postgresql表php

时间:2018-05-18 08:17:37

标签: php postgresql pdo insert insert-update

postgresql表中的数据不再更新。我想覆盖数据。当我只使用insert into时,会添加新数据,但旧数据仍然存在。我试图使用更新,但后来我得到错误。我想更新所有记录。我认为它可能与语法有关。但我找不到问题。

代码

$dbname = "dbtest";
$host = "localhost";
$username = "postgres";
$password = "pasword";

$dbh = new PDO("pgsql:dbname=$dbname; host=$host", $username, $password);

$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$c = array("Human","Mouse","Rat","Hamster","SV40");       
$b = array("Human HBO gene", "Mouse BB gene", "Human CCB gene", "SV40 TP gene", "Hamster TP53 gene");
$count=0;
foreach($c as $key => $d){
    $e =$b[$key];
    $name = $count++;
    if (strpos($e, $d) !== FALSE) {
        $match = $d;
        $specie = $d;
        $specie = str_replace("Human","Homo Sapiens",$specie);
        $specie = str_replace("Mouse","Mus Musculus",$specie);
        $specie = str_replace("Rat","Rattus norvegicus",$specie);
        $Specie = str_replace("Hamster", "Mesocricetus Auratus",$specie);
        $specie = str_replace("SV40","Simian virus 40",$specie);
    }else{
        $match = "0";
        $specie = "0";
    }

echo $match. " ". $specie. " ";

$var_id = $name;
$var_match = $match;
$var_full_name = $specie;


    #$sql = "INSERT INTO species (id,match,full_name) VALUES ('".$var_id."','".$var_match ."','".$var_full_name."')";
    $sql = "UPDATE species SET id = '".$var_id."', match = '".$var_match ."', full_name='".$var_full_name."'";
    if ($dbh->query($sql)) {
        echo "New Record Inserted Successfully!<br \>\n";
    }else{
        echo "Data not successfully Inserted.<br \>\n";
    } 
}

我得到的错误:

  

致命错误:未捕获PDOException:SQLSTATE [42601]:语法错误:7错误:&gt; “Sapiens”或其附近的语法错误第1行:......种类SET id ='0',match = Human,full_name = Homo Sapiens'^ in /var/www/html/test/Insert.php:59堆栈追踪: #0 /var/www/html/test/Insert.php(59):PDO-&gt;查询('UPDATE species ...')#1 {main}抛出/ var / www / html / test / Insert。第59行的PHP

2 个答案:

答案 0 :(得分:1)

您应该使用选择查询来确定值是新值还是旧值。如果旧更新数据,则插入数据。

功能

$query = 'SELECT * FROM tbl '. 'WHERE "test1" = '. 
        "'".$var_test."'" . 'AND "test2" = '. 
        "'".$var_test2."'";
        $stmt = $dbh->prepare($query);
        $stmt->execute();
        $result = $stmt->setFetchMode(PDO::FETCH_ASSOC);

        $outcome = $stmt->fetch();

选择

        if ($outcome !== false){
            $sql = "UPDATE tbl SET test1 = '".$var_test1."', test2 = '".$var_test2."', 
            test3 = '".$var_test3."'
            "WHERE id = '".$var_id."' ";
            execute_query($sql,$dbh);

更新

    }else{    
        $sql = "INSERT INTO genes 
        (id,test1,test2,test3) 
        VALUES ('".$var_id."','".$var_test1 ."','".$var_test2."','".$var_test3."')";
        execute_query($sql,$dbh);
    }

INSERT

<h:form>
        <h:panelGroup style="display:block; text-align:center;">
            <ace:pushButton id="show" value="Show Dialog"
                            type="button">
                <ace:ajax execute="@none" render="@none"
                          onStart="ice.ace.instance('#{dialog.clientId}').show(); return false;"/>
            </ace:pushButton>
        </h:panelGroup>
    </h:form>
    <ace:dialog id="dialog" binding="#{dialog}"
                header="A sample dialog overview example"
                closable="false"
                modal="true"
                draggable="false"
                showEffect="clip"
                hideEffect="fade"
                width="400">

        <h:form id="inputForm">
            <h:panelGrid columns="2" width="100%">
                <h:outputLabel id="firstNameLabel" for="firstNameInputField" value="First Name:"/>
                <ace:textEntry id="firstNameInputField"
                               value="#{dialogBean.firstName}" />

                <h:outputLabel id="lastNameLabel" for="lastNameInputField" value="Last Name:"/>
                <ace:textEntry id="lastNameInputField"
                               value="#{dialogBean.lastName}"/>
            </h:panelGrid>

            <h:panelGrid width="100%" style="text-align: center;">
                <ace:pushButton id="submitBtn"
                                value="Click me once done with input">
                    <ace:ajax execute="@form" render="@all"
                              onSuccess="ice.ace.instance('#{dialog.clientId}').hide(); "/>
                </ace:pushButton>
            </h:panelGrid>
        </h:form>

    </ace:dialog>


    <h:form id="outputForm">
        <h:panelGrid id="outputPanel" columns="2">
            <h:outputLabel value="Entered text: " for="enteredName"/>
            <h:outputText id="enteredName" value="#{dialogBean.firstName} #{dialogBean.lastName}"/>
        </h:panelGrid>
    </h:form>

答案 1 :(得分:0)

当我使用以下语法时,错误消失了。此查询需要用于更新。

$sql = "UPDATE species SET match ='".$var_match ."', full_name='".$var_full_name."' WHERE id = '".$var_id."' ";