PHP query for updating table with same session_id

天大地大妈咪最大 提交于 2021-01-29 09:09:17

问题


I used session_id as the ID column in my db.There is one more column named Sections where added the whole xml. The xml contains section names and the amount of time user spent there. I want to update the Sections column for same session_ids. How can I do that? Right now it adds a new row for every record. Here is my php code

$id=$_SESSION["id"];
$totaltime=$_POST['total'];
$HomeTime=$_POST['home'];
$ProductTime=$_POST['products'];
$ProcessTime=$_POST['process'];
$DevTime=$_POST['dev'];
$ContactTime=$_POST['contact'];


$xmlObject = <<<XML
<?xml version="1.0" encoding="UTF-8"?> 
<item>
    <section>
        <name>Home</name>
        <time>$HomeTime</time>
    </section>  
    <section>
        <name>Product</name>
        <time>$ProductTime</time>
    </section>
    <section>
        <name>Process</name>
        <time>$ProcessTime</time>
    </section>  
    <section>
        <name>Development</name>
        <time>$DevTime</time>
    </section>
    <section>
        <name>Contact</name>
        <time>$ContactTime</time>
    </section>
    </item>
XML;



    $sql = "INSERT INTO user_time (ID, Sections) VALUES ('$id', '$xmlObject')";

    if ($conn->query($sql) === TRUE) {
        echo "New record created successfully";
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }

回答1:


First fetch existing XML data from db using the session id it exists, and store to variable $xmlFromDB and proceed with following steps. Otherwise insert to db.

if(session id exisits in db){
    //Fetch the table row or corresponding session id.
    //Extract times from XML using this code.

    $arr = [];
    $times = new SimpleXMLElement($xmlFromDB);
    foreach($times->section as $sec){
        $arr[$sec->name.""] = $sec->time;
    }
    extract($arr);

    //This will add previous value to new value.
    $HomeTime += $Home;
    $ProductTime += $Product;
    $ProcessTime += $Process;
    $DevTime += $Development;
    $ContactTime += $Contact;
}

//Now generate xml using your method.

$xmlObject = <<<XML
    <?xml version="1.0" encoding="UTF-8"?> 
    <item>
        <section>
            <name>Home</name>
            <time>$HomeTime</time>
        </section>  
        <section>
            <name>Product</name>
            <time>$ProductTime</time>
        </section>
        <section>
            <name>Process</name>
            <time>$ProcessTime</time>
        </section>  
        <section>
            <name>Development</name>
            <time>$DevTime</time>
        </section>
        <section>
            <name>Contact</name>
            <time>$ContactTime</time>
        </section>
        </item>
XML;

if(session id exists){
    //Then update the same row using the UPDATE  query.
    $sql = "UPDATE user_time SET Sections = '$xmlObject' where = '$id'";
}else{
    $sql = "INSERT INTO user_time (ID, Sections) VALUES ('$id', '$xmlObject')";
}

if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
} 


来源:https://stackoverflow.com/questions/53493896/php-query-for-updating-table-with-same-session-id

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!