Full update query

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

    #1

    Full update query

    Hi Guys,

    I am trying to create an editable table of a MySQL query where every
    field can be updated. My example script is at:
    http://nana46.coconia.net/test4.php however I am currently getting
    Parse errors.

    Am I missing anything obvious in my code below?

    Thanks

    A



    <HTML>
    <TITLE>test 4</title>
    <form method='post' action='test4.p hp'>
    <?php
    function check_mysql()
    {
    if (mysql_errno() > 0)
    {
    die("<BR> MySQL error " . mysql_errno() . ": " .
    mysql_error());
    }

    }

    $db = mysql_connect(" coconia.net", "nana46_nana46" , "hello");
    if (!$db)
    {
    die("Failed to open connection to MySQL server.");

    }

    mysql_select_db ("nana46_nana46 ");
    check_mysql();

    $requete = "SELECT id,lowerval,upp erval,result FROM fig_lookup";
    $resulta = mysql_query($re quete) or die (mysql_error()) ;

    echo '<table border="2" cellpadding="0" cellspacing="0" width="100%">';

    $edit = "<img src='edit.gif' border='0' alt='Edit record'>";

    echo "<table border='2'>";
    echo "<td>id</td>";
    echo "<td>Lowerv al</td>";
    echo "<td>Upperv al</td>";
    echo "<td>Result </td></tr>";

    if($mode == "Update")
    {
    for ($i=0; $i<count($_POST['id']); $i++)
    {

    ////LINE 39////// mysql_query ("UPDATE fig_lookup SET
    lowerval='{'$_P OST['f2'][$i]}',upperval='{' $_POST['f3'][$i]}',result='{'$_ POST['f4'][$i]}'
    WHERE id='{'$_POST['f1'][$i]}'");
    }

    }

    while ($l = mysql_fetch_arr ay($resulta, MYSQL_ASSOC))
    { ?>
    <tr>
    <td><input type="text" name="f1[]" value="<?=$l["id"]?>"></td>
    <td><input type="text" name="f2[]" value="<?=$l["lowerval"]?>"></td>
    <td><input type="text" name="f3[]" value="<?=$l["upperval"]?>"></td>
    <td><input type="text" name="f4[]" value="<?=$l["result"]?>"></td></tr>
    <?
    }

    echo "</tr>";
    echo "</table>";
    ?>
    <input type='submit' name=mode value='Update'>
    </form>
    </HTML>

    Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or
    `T_NUM_STRING' in /home/www/nana46.coconia. net/test4.php on line 39
    (line 39 is mysql_query line - marked in code)

  • Bill Karwin

    #2
    Re: Full update query

    "UKuser" <spidercc21@yah oo.co.uk> wrote in message
    news:1139614141 .985116.93960@g 47g2000cwa.goog legroups.com...[color=blue]
    > ////LINE 39////// mysql_query ("UPDATE fig_lookup SET
    > lowerval='{'$_P OST['f2'][$i]}',upperval='{' $_POST['f3'][$i]}',result='{'$_ POST['f4'][$i]}'
    > WHERE id='{'$_POST['f1'][$i]}'");
    >
    > Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or
    > `T_NUM_STRING' in /home/www/nana46.coconia. net/test4.php on line 39
    > (line 39 is mysql_query line - marked in code)[/color]

    Doesn't PHP have a problem parsing nested quoted strings? That is, your
    array subscript single-quoted strings (like 'f2') aren't being parsed,
    because they're inside the double-quoted string for the UPDATE statement.
    See http://p2p.wrox.com/topic.asp?TOPIC_ID=2468

    Also note that your code is full of SQL injection and XSS vulnerabilities .
    You should save each of those POST parameters to an individual variable and
    validate them, to make sure they contain only simple values, with no
    possibility of extra SQL code or javascript code.

    Regards,
    Bill K.


    Comment

    • UKuser

      #3
      Re: Full update query

      Still not working I'm afraid with Code as below. Would you use the
      check_mysql() command for the injection vulnerabilities ?

      if($mode == "Update")
      {
      for ($i=0; $i<count($_POST['id']); $i++)
      {
      $post1 = $_POST[f2][$i];
      $post2 = $_POST[f3][$i];
      $post3 = $_POST[f4][$i];
      $post0 = $_POST[f1][$i];
      mysql_query ("UPDATE fig_lookup SET
      lowerval='$post 1',upperval='$p ost2',result='$ post3' WHERE id=$post0 ");

      }
      }

      Comment

      • Bill Karwin

        #4
        Re: Full update query

        "UKuser" <spidercc21@yah oo.co.uk> wrote in message
        news:1139657627 .568843.34480@g 47g2000cwa.goog legroups.com...[color=blue]
        > Still not working I'm afraid with Code as below. Would you use the
        > check_mysql() command for the injection vulnerabilities ?[/color]

        I don't know what the check_mysql() function is.

        It's important that you read about what SQL injection means, for instance
        here:

        [color=blue]
        > mysql_query ("UPDATE fig_lookup SET
        > lowerval='$post 1',upperval='$p ost2',result='$ post3' WHERE id=$post0 ");[/color]

        When making SQL strings for execution in a scripting language like this, you
        never know _exactly_ what is being sent to the MySQL parser unless you
        capture that string after interpolating all variables and before executing
        it with mysql_query. It's best to

        Also, you aren't checking the return value of the mysql_query() function.
        This could give you a more informative error message.

        for ($i=0; $i<count($_POST['id']); $i++)
        {
        $post1 = $_POST[f2][$i];
        // validate that $post1 doesn't contain SQL.

        $post2 = $_POST[f3][$i];
        // validate that $post2 doesn't contain SQL.

        $post3 = $_POST[f4][$i];
        // validate that $post3 doesn't contain SQL.

        $post0 = $_POST[f1][$i];
        // validate that $post0 doesn't contain SQL.

        $query = "UPDATE fig_lookup SET
        lowerval='$post 1',upperval='$p ost2',result='$ post3' WHERE id=$post0 ";
        // output $query to make sure it looks right.
        // you might even try copy & paste of that output into the mysql CLI to
        test it.

        $result = mysql_query ($query);
        if (!$result) {
        die('Invalid query: ' . mysql_error());
        }
        }

        Regards,
        Bill K.


        Comment

        • Bill Karwin

          #5
          Re: Full update query

          "Bill Karwin" <bill@karwin.co m> wrote in message
          news:dsll2r02vc h@enews1.newsgu y.com...[color=blue]
          > When making SQL strings for execution in a scripting language like this,
          > you never know _exactly_ what is being sent to the MySQL parser unless you
          > capture that string after interpolating all variables and before executing
          > it with mysql_query. It's best to[/color]

          Er, to elaborate on that last point... <g>

          It's best to create tor SQL statement as a string, then output it either to
          a debugging log or the browser, and examine it for any obvious errors. You
          can even take that finished string and cut & paste it into mysql CLI or
          Query Browser, executing it against a test copy of your database, to see if
          it does what you expect it to do.

          The point is that it's more difficult to debug strings that are a mix of SQL
          and PHP syntax than it is to debug the resulting SQL statement after the
          variables have been interpolated.

          Regards,
          Bill K.


          Comment

          Working...