Php MySQL_query and echo conflict

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SHOverine
    New Member
    • Sep 2006
    • 18

    #1

    Php MySQL_query and echo conflict

    Hello All. I have a problem with a form that I am trying to create. What happens is: I have a person enter a UserName and select some values using radio buttons. They submit the form, and the next page is intended to echo their information back. I was able to get the first page to write values to the database, but when I add the second page, the values do not write to the database and I get an error.

    Here is a pared-down version of the code, with comments:

    <!-- FIRST PAGE -->
    <html>
    <head>
    <?php
    include("db.php "); //CONNECTS TO DATABASE
    ?>
    </head>
    <body>
    <form name="form1" method="post" action="myinfor mation.php">

    <p>Name: <input name="UserName" type = "text"></p>

    <?php
    $_SESSION['$UserName'] = $UserName;
    ?>

    <p><input name="v1" value="Value1" type="hidden">
    <input name="Input1" onclick="g1.val ue = v1.value" type="radio">Va lue1</p>

    <!-- IF SELECTED, THIS PUTS THE VALUE ASSOCIATED WITH THE RADIO BUTTON "V1" INTO THE TEXT BOX "G1", WHICH I WANT TO PUT IN THE MYSQL DATABASE -->

    </form>

    <?php dbConnect('my_d atabase');

    if (isset($_POST['submitok'])) {
    $Submit_sql = "INSERT INTO Info (UserName, G1)
    VALUES ('$UserName', '$g1')

    mysql_query($Su bmit_sql) or die(mysql_error ());
    } ?>
    </body>
    </html>

    <!-- IF I HAVE NOTHING IN THE NEXT PAGE, myinformation.p hp, THE VALUES WILL WRITE TO THE DATABASE. HOWEVER, I WANT TO ECHO BACK THE VALUES FOR THE USER SO THEY CAN PRINT THEM. -->

    <!--SECOND PAGE -->

    <html>
    <head>
    <?php
    include("db.php "); //CONNECTS TO DATABASE
    ?>
    </head>
    <body>
    <h1><?php echo $UserName;
    $_SESSION['$UserName'] = $UserName;?>'s Information</h1>

    <p>
    <?php
    dbConnect('my_d atabase');
    $sql_g1 = "SELECT G1 FROM Info WHERE UserName = '$UserName'";
    $result_g1 = mysql_query($sq l_g1);
    $g1 = mysql_result($r esult_g1, 0);
    echo $g1;
    ?>
    </p>
    </body>
    </html>

    <!-- THE INFORMATION DOES NOT WRITE TO THE DATABASE WHEN I HAVE THIS IN MY SECOND PAGE AND I GET THE ERROR "Warning: mysql_result(): Unable to jump to row 0 on MySQL result index 5 in /home/theweekl/public_html/NCAA2007/myinformation.p hp on line xxx", BECAUSE THE DATA IS NOT THERE -->

    Any help is greatly appreciated!

    Cheers,
    Seth
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    We can only help you when you adhere to the Posting Guidelines, shown at the top of this forum! Especially the part about enclosing code within code or php tags! The code posted is absolutely unreadable in this format.

    You have posted code before so you know that.

    moderator

    Comment

    • SHOverine
      New Member
      • Sep 2006
      • 18

      #3
      Sorry about the ill-formatted post.

      Hello All. I have a problem with a form that I am trying to create. What happens is: I have a person enter a UserName and select some values using radio buttons. They submit the form, and the next page is intended to echo their information back. I was able to get the first page to write values to the database, but when I add the second page, the values do not write to the database and I get an mysql_result() error.

      Here is the code:

      First page:
      [php]form name="form1" method="post" action="myinfor mation.php">

      <p>Name: <input name="UserName" type = "text"></p>

      $_SESSION['$UserName'] = $UserName;

      <p><input name="v1" value="Value1" type="hidden">
      <input name="Input1" onclick="g1.val ue = v1.value" type="radio">Va lue1</p>

      </form>

      dbConnect('my_d atabase');

      if (isset($_POST['submitok'])) {
      $Submit_sql = "INSERT INTO Info (UserName, G1)
      VALUES ('$UserName', '$g1')

      mysql_query($Su bmit_sql) or die(mysql_error ());
      }[/php]
      Second page (myinformation. php):
      [php]
      <h1> echo $UserName;
      $_SESSION['$UserName'] = $UserName;'s Information</h1>

      <p>
      dbConnect('my_d atabase');
      $sql_g1 = "SELECT G1 FROM Info WHERE UserName = '$UserName'";
      $result_g1 = mysql_query($sq l_g1);
      $g1 = mysql_result($r esult_g1, 0);
      echo $g1;

      </p>
      [/php]

      Comment

      • ronverdonk
        Recognized Expert Specialist
        • Jul 2006
        • 4259

        #4
        When you pass (POST) your data using submit to the second page, you must extract the POSTed values from the $_POST array. I cannot see any extraction in the second page.

        Also the following statement is unclear to me:
        Code:
        <input name="Input1" onclick="g1.value = v1.value" type="radio">Value1</p>
        What is the 'onclick' doing there? And what are the g1 and v1 values? Did you assign a Javascript DOM variable to these?

        Ronald :cool:

        Comment

        • SHOverine
          New Member
          • Sep 2006
          • 18

          #5
          Ronald,

          As always, thanks for the response. I was making things more complicated than they need be. For one, I needed to have my submit statement on the second page and only echo back the variable - instead of submitting, and then recalling the values to the "my information page". The code is as simple as

          [PHP]
          dbConnect('my_d atabase');
          echo $g1;
          [/PHP]

          As for the onclick stuff - g1 is the text box where the selected value goes (either v1 or v2, which are the values of the radio button "Input1"). If you want to know more, pm or email me.

          Problem has been solved. Thanks again.

          Cheers,
          Seth

          Originally posted by ronverdonk
          When you pass (POST) your data using submit to the second page, you must extract the POSTed values from the $_POST array. I cannot see any extraction in the second page.

          Also the following statement is unclear to me:
          Code:
          <input name="Input1" onclick="g1.value = v1.value" type="radio">Value1</p>
          What is the 'onclick' doing there? And what are the g1 and v1 values? Did you assign a Javascript DOM variable to these?

          Ronald :cool:

          Comment

          • ronverdonk
            Recognized Expert Specialist
            • Jul 2006
            • 4259

            #6
            I am glad you solved your problem. See you next time.

            Ronald :cool:

            Comment

            Working...