Pregunta

if(isset($_POST['update_page']))
{
    if($_POST['agent_name']=="" and $_POST['company_name']=="" and $_POST['email']=="")
    {
        $response = asort_form_error("All agent detail fields should be filled");
        extract($_POST);
    }
    else
    {
        $sql = "UPDATE ".TBL_AGENT." SET agent_name = '$_POST[agent_name]' , company_name = '$_POST[company_name]' , email = '$_POST[email]' WHERE agent_id = '{$_GET['id']}'";
        mysql_query($sql,$CN);

        $sql_query = "UPDATE ".TBL_SUBSCRIPTION." SET renewal_date = ( UNIX_TIMESTAMP() + ( '$_POST[subscription_renewal]' * 86400 ) ) WHERE agent_id =  '{$_GET['id']}'";
        mysql_query($sql_query,$CN);

        $response = asort_form_ok("Agent Details Successfully Updated");
        header('Location: subscription.php');
    }
    unset($_POST);
}

I'm trying to update the records of my multiple database tables (TBL_AGENT and TBL_SUBSCRIPTION) using same agent_id. Above two queries working fine. I want to write in single line. How to write those two $sql and $sql_query in single line ?

¿Fue útil?

Solución

This should work:

"UPDATE ".TBL_AGENT." 
JOIN  ".TBL_SUBSCRIPTION." ON ".TBL_AGENT.".agent_id = ".TBL_SUBSCRIPTION.".agent_id
SET ".TBL_AGENT.".agent_name = '$_POST[agent_name]' , ".TBL_AGENT.".company_name = '$_POST[company_name]' , ".TBL_AGENT.".email = '$_POST[email]',  ".TBL_SUBSCRIPTION.".renewal_date = ( UNIX_TIMESTAMP() + ( '$_POST[subscription_renewal]' * 86400 ) ) 
WHERE ".TBL_AGENT.".agent_id = '{$_GET['id']}';"

Otros consejos

Use a JOIN

UPDATE tbl_agent AS a
JOIN tbl_subsription AS s ON a.agent_id = s.agent_id
SET a.agent_name = '$_POST[agent_name]',
    s.renewal_date = ( UNIX_TIMESTAMP() + ( '$_POST[subscription_renewal]' * 86400 ) )
WHERE a.agent_d = '{$_GET['id']}'

Try to execute below mysql script manually and then integrate with your code

UPDATE database1.TBL_AGENT a,database2.TBL_SUBSCRIPTION b  SET a.agent_name = <your value> and  a.company_name = <your value> and a.email = <your value> and b.renewal_date = <your value> WHERE a.agent_id = b.agent_id = <your where clause value>
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top