Porblem with SQL statement in my php.

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

    Porblem with SQL statement in my php.

    Hello (Again)

    I am trying to insert recordes into two tables with the following code. But
    field UserId in table tOrders
    and
    field OrderID in table tOrderItem
    are not updating only the default value 0 is being saved into the records. I
    have been pondering this one all day. I expect as I am new to php I am
    missing something obvious but I cannot see it. All the other fields are
    being created OK and the $_SESSION[username] is OK as it can be echoed on
    the page. I suspect it must be something to do with the variables in the SQL
    statements.

    *************** *************** *************** *************** **********
    <?php

    session_start() ;
    include('iddsof 2.php');
    include("login. php");


    $OrderDate = date('d/m/Y');
    echo $_SESSION[username];

    $UserID = mysql_query("SE LECT id FROM users WHERE username
    ='".$_SESSION[username]."'");
    $OrderID = mysql_query("SE LECT OrderID FROM tOrders WHERE UserID =
    '$UserID'");

    $insertOrder = "INSERT INTO tOrders (UserId,OrderDa te,OrderCost) VALUES
    ('$id','$OrderD ate',10)";
    $add_order = @mysql_query($i nsertOrder) or die('Query failed: ' .
    mysql_error());


    $insertOrderDet ails = "INSERT INTO tOrderItem (OrderID,Questi onNo) VALUES
    ('$OrderID',10) ";
    $add_order_deta ils = @mysql_query($i nsertOrderDetai ls) or die('Query failed:
    ' . mysql_error());

    ?>
    <h1>Order Status</h1>
    <p>Congratulati ons <b><?php echo $_SESSION[username] ?></b>, your order has
    been completed.<br>
    Please check that the questions have transfered to your local database from
    within Pupil Tester.<br><br> <br><br>
    <a href=\logout.ph p\> Logout</a> or return to <a href=\Questions .php\>
    Questions Database</a></p>

    <?php
    mysql_close($co nn);
    ?>
    *************** *************** *************** *************** ***********


  • Gordon Burditt

    #2
    Re: Porblem with SQL statement in my php.

    >I am trying to insert recordes into two tables with the following code. But[color=blue]
    >field UserId in table tOrders
    >and
    >field OrderID in table tOrderItem
    >are not updating only the default value 0 is being saved into the records. I
    >have been pondering this one all day. I expect as I am new to php I am
    >missing something obvious but I cannot see it.[/color]

    The return value of mysql_query() is *NOT* a value of what you
    selected. It's a result set. Fetch a row (you might have gotten
    more than one row) from the result set (e.g. with $r =
    mysql_fetch_arr ay($OrderID).) Then look for the column (e.g.
    $r['OrderID'] you asked for, and THAT'S the value you want to put
    in the query.

    Also, when you're debugging something, don't put @ in front of
    function calls. Also, print out the SQL being passed to mysql_query.

    You never set $id (unless it's hidden in your includes).
    [color=blue]
    >All the other fields are
    >being created OK and the $_SESSION[username] is OK as it can be echoed on
    >the page. I suspect it must be something to do with the variables in the SQL
    >statements.
    >
    >************** *************** *************** *************** ***********
    ><?php
    >
    >session_start( );
    >include('iddso f2.php');
    >include("login .php");
    >
    >
    >$OrderDate = date('d/m/Y');
    >echo $_SESSION[username];
    >
    >$UserID = mysql_query("SE LECT id FROM users WHERE username
    >='".$_SESSIO N[username]."'");
    >$OrderID = mysql_query("SE LECT OrderID FROM tOrders WHERE UserID =
    >'$UserID'");
    >
    >$insertOrder = "INSERT INTO tOrders (UserId,OrderDa te,OrderCost) VALUES
    >('$id','$Order Date',10)";
    >$add_order = @mysql_query($i nsertOrder) or die('Query failed: ' .
    >mysql_error()) ;
    >
    >
    >$insertOrderDe tails = "INSERT INTO tOrderItem (OrderID,Questi onNo) VALUES
    >('$OrderID',10 )";
    >$add_order_det ails = @mysql_query($i nsertOrderDetai ls) or die('Query failed:
    >' . mysql_error());
    >
    >?>
    ><h1>Order Status</h1>
    ><p>Congratulat ions <b><?php echo $_SESSION[username] ?></b>, your order has
    >been completed.<br>
    >Please check that the questions have transfered to your local database from
    >within Pupil Tester.<br><br> <br><br>
    ><a href=\logout.ph p\> Logout</a> or return to <a href=\Questions .php\>
    >Questions Database</a></p>
    >
    ><?php
    >mysql_close($c onn);
    >?>
    >************** *************** *************** *************** ************[/color]

    Gordon L. Burditt

    Comment

    • Ian Davies

      #3
      Re: Porblem with SQL statement in my php.

      Gordon
      Your sugestions worked
      Thank u
      Ian

      "Gordon Burditt" <gordonb.ythas@ burditt.org> wrote in message
      news:11llh3s2g0 ll595@corp.supe rnews.com...[color=blue][color=green]
      > >I am trying to insert recordes into two tables with the following code.[/color][/color]
      But[color=blue][color=green]
      > >field UserId in table tOrders
      > >and
      > >field OrderID in table tOrderItem
      > >are not updating only the default value 0 is being saved into the[/color][/color]
      records. I[color=blue][color=green]
      > >have been pondering this one all day. I expect as I am new to php I am
      > >missing something obvious but I cannot see it.[/color]
      >
      > The return value of mysql_query() is *NOT* a value of what you
      > selected. It's a result set. Fetch a row (you might have gotten
      > more than one row) from the result set (e.g. with $r =
      > mysql_fetch_arr ay($OrderID).) Then look for the column (e.g.
      > $r['OrderID'] you asked for, and THAT'S the value you want to put
      > in the query.
      >
      > Also, when you're debugging something, don't put @ in front of
      > function calls. Also, print out the SQL being passed to mysql_query.
      >
      > You never set $id (unless it's hidden in your includes).
      >[color=green]
      > >All the other fields are
      > >being created OK and the $_SESSION[username] is OK as it can be echoed on
      > >the page. I suspect it must be something to do with the variables in the[/color][/color]
      SQL[color=blue][color=green]
      > >statements.
      > >
      > >************** *************** *************** *************** ***********
      > ><?php
      > >
      > >session_start( );
      > >include('iddso f2.php');
      > >include("login .php");
      > >
      > >
      > >$OrderDate = date('d/m/Y');
      > >echo $_SESSION[username];
      > >
      > >$UserID = mysql_query("SE LECT id FROM users WHERE username
      > >='".$_SESSIO N[username]."'");
      > >$OrderID = mysql_query("SE LECT OrderID FROM tOrders WHERE UserID =
      > >'$UserID'");
      > >
      > >$insertOrder = "INSERT INTO tOrders (UserId,OrderDa te,OrderCost) VALUES
      > >('$id','$Order Date',10)";
      > >$add_order = @mysql_query($i nsertOrder) or die('Query failed: ' .
      > >mysql_error()) ;
      > >
      > >
      > >$insertOrderDe tails = "INSERT INTO tOrderItem (OrderID,Questi onNo) VALUES
      > >('$OrderID',10 )";
      > >$add_order_det ails = @mysql_query($i nsertOrderDetai ls) or die('Query[/color][/color]
      failed:[color=blue][color=green]
      > >' . mysql_error());
      > >
      > >?>
      > ><h1>Order Status</h1>
      > ><p>Congratulat ions <b><?php echo $_SESSION[username] ?></b>, your order[/color][/color]
      has[color=blue][color=green]
      > >been completed.<br>
      > >Please check that the questions have transfered to your local database[/color][/color]
      from[color=blue][color=green]
      > >within Pupil Tester.<br><br> <br><br>
      > ><a href=\logout.ph p\> Logout</a> or return to <a href=\Questions .php\>
      > >Questions Database</a></p>
      > >
      > ><?php
      > >mysql_close($c onn);
      > >?>
      > >************** *************** *************** *************** ************[/color]
      >
      > Gordon L. Burditt[/color]


      Comment

      Working...