Quick two Code questions..

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • IchBin

    Quick two Code questions..

    Sorry I am new to PHP. First question, I hate to ask this but is there
    an or statement in PHP that I can use in an IF clause? I can't seem to
    find the format. Like say '|' or 'or'.

    Question: I am trying to do a SQL table insert. OK, no big deal. Well I
    am having a problem with this code. I build a data structure
    $fields_values and pass it to a function insertIntoDB() to issue the
    insert.

    The problem is in the actual INSERT Statement. The values for the column
    data do not have "" around the Col values. So SQL flags the fields as
    wrong. Example:

    INSERT INTO `Log` (ipAddress,acti on,groupPageNam e,namePageName, system)
    VALUES (127.0.0.1,brow se,Main,HomePag e,Testing new code)

    Can some one point me in the right direction on wrapping " around the
    data values so it will insert into the table.

    $table = 'myTable';

    $fields_values = array
    (
    'ipAddress' =$_SERVER['REMOTE_ADDR'],
    'action' =$action,
    'groupPageName' =FmtPageName('$ Group', $pagename),
    'namePageName' =FmtPageName('$ Name', $pagename);,
    'system' =$system
    );

    insertIntoDB($t able, $fields_values) ;

    function insertIntoDB($t able, $fields_values)
    {
    $fields = implode(array_k eys($fields_val ues), ',');
    $values = implode(array_v alues($fields_v alues), ',');
    $sqlStatement = 'INSERT INTO `'.$table.'` ('.$fields.') VALUES
    ('.$values.')';
    $res = mysql_query($sq lStatement)OR die(mysql_error ());
    return true;
    }

    --
    Thanks in Advance...
    IchBin, Pocono Lake, Pa, USA http://weconsultants.phpnet.us
    'If there is one, Knowledge is the "Fountain of Youth"'
    -William E. Taylor, Regular Guy (1952-)
  • Bob Stearns

    #2
    Re: Quick two Code questions..

    IchBin wrote:
    Sorry I am new to PHP. First question, I hate to ask this but is there
    an or statement in PHP that I can use in an IF clause? I can't seem to
    find the format. Like say '|' or 'or'.
    >
    Question: I am trying to do a SQL table insert. OK, no big deal. Well I
    am having a problem with this code. I build a data structure
    $fields_values and pass it to a function insertIntoDB() to issue the
    insert.
    >
    The problem is in the actual INSERT Statement. The values for the column
    data do not have "" around the Col values. So SQL flags the fields as
    wrong. Example:
    >
    INSERT INTO `Log` (ipAddress,acti on,groupPageNam e,namePageName, system)
    VALUES (127.0.0.1,brow se,Main,HomePag e,Testing new code)
    >
    Can some one point me in the right direction on wrapping " around the
    data values so it will insert into the table.
    >
    $table = 'myTable';
    >
    $fields_values = array
    (
    'ipAddress' =$_SERVER['REMOTE_ADDR'],
    'action' =$action,
    'groupPageName' =FmtPageName('$ Group', $pagename),
    'namePageName' =FmtPageName('$ Name', $pagename);,
    'system' =$system
    );
    >
    insertIntoDB($t able, $fields_values) ;
    >
    function insertIntoDB($t able, $fields_values)
    {
    $fields = implode(array_k eys($fields_val ues), ',');
    $values = implode(array_v alues($fields_v alues), ',');
    $sqlStatement = 'INSERT INTO `'.$table.'` ('.$fields.') VALUES
    ('.$values.')';
    $res = mysql_query($sq lStatement)OR die(mysql_error ());
    return true;
    }
    >
    Logical or is copied from C et al. It is '||'. '|' is bit wise or of two
    integers. Reference


    The statement INSERT should look like the following:

    INSERT INTO `Log` (ipAddress,acti on,groupPageNam e,namePageName, system)
    VALUES ('127.0.0.1','b rowse','Main',' HomePage','Test ing new code')

    If, as it appears, all your db columns are either CHAR, VARCHAR, or DATE
    then this is easily done by:

    $values = "'" . implode(array_v alues($fields_v alues), "','") . "'";

    If they are of mixed numeric and the aforementioned types, then your
    fields_values assignment statement must be modified as follows for each
    CHAR etc. column:

    'action' ="'$action'" ,

    Comment

    • IchBin

      #3
      Re: Quick two Code questions..

      Bob Stearns wrote:
      IchBin wrote:
      >
      >Sorry I am new to PHP. First question, I hate to ask this but is there
      >an or statement in PHP that I can use in an IF clause? I can't seem to
      >find the format. Like say '|' or 'or'.
      >>
      >Question: I am trying to do a SQL table insert. OK, no big deal. Well
      >I am having a problem with this code. I build a data structure
      >$fields_valu es and pass it to a function insertIntoDB() to issue the
      >insert.
      >>
      >The problem is in the actual INSERT Statement. The values for the
      >column data do not have "" around the Col values. So SQL flags the
      >fields as wrong. Example:
      >>
      >INSERT INTO `Log` (ipAddress,acti on,groupPageNam e,namePageName, system)
      >VALUES (127.0.0.1,brow se,Main,HomePag e,Testing new code)
      >>
      >Can some one point me in the right direction on wrapping " around the
      >data values so it will insert into the table.
      >>
      >$table = 'myTable';
      >>
      >$fields_valu es = array
      >(
      >'ipAddress' =$_SERVER['REMOTE_ADDR'],
      >'action' =$action,
      >'groupPageName ' =FmtPageName('$ Group', $pagename),
      >'namePageNam e' =FmtPageName('$ Name', $pagename);,
      >'system' =$system
      >);
      >>
      >insertIntoDB($ table, $fields_values) ;
      >>
      >function insertIntoDB($t able, $fields_values)
      >{
      > $fields = implode(array_k eys($fields_val ues), ',');
      > $values = implode(array_v alues($fields_v alues), ',');
      > $sqlStatement = 'INSERT INTO `'.$table.'` ('.$fields.') VALUES
      >('.$values.')' ;
      > $res = mysql_query($sq lStatement)OR die(mysql_error ());
      > return true;
      >}
      >>
      Logical or is copied from C et al. It is '||'. '|' is bit wise or of two
      integers. Reference

      >
      The statement INSERT should look like the following:
      >
      INSERT INTO `Log` (ipAddress,acti on,groupPageNam e,namePageName, system)
      VALUES ('127.0.0.1','b rowse','Main',' HomePage','Test ing new code')
      >
      If, as it appears, all your db columns are either CHAR, VARCHAR, or DATE
      then this is easily done by:
      >
      $values = "'" . implode(array_v alues($fields_v alues), "','") . "'";
      >
      If they are of mixed numeric and the aforementioned types, then your
      fields_values assignment statement must be modified as follows for each
      CHAR etc. column:
      >
      'action' ="'$action'" ,
      Thanks you Bob for your help, all works now.

      --
      Thanks in Advance...
      IchBin, Pocono Lake, Pa, USA http://weconsultants.phpnet.us
      'If there is one, Knowledge is the "Fountain of Youth"'
      -William E. Taylor, Regular Guy (1952-)

      Comment

      Working...