SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Guru deepM's Avatar
    Join Date
    Dec 2007
    Location
    India
    Posts
    705
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    insert value and pass value at the same time

    Hello,

    I have two pages. on the first page i want to insert data(table myname(id,myname) into database.simultaneously want to pass id of this page to next page for the insertion for the another table like(myfriend,myid(id of myname)

    i have to do both the process at one time so bit confusing. so can anyone tell me here how can i do that?

    here is my code:
    1st page- addfriend.php
    PHP Code:
    <?

    var_dump
    ($_POST);
    if(
    $_SERVER['REQUEST_METHOD'] == "GET"){
      
    $myname   =    "";
       }else if(
    $_SERVER['REQUEST_METHOD'] == "POST")
       {
           
    $myname                     $_POST['myname'];


               if(
    $_POST['submit'] == "submit"){

               
    $myname                 $_POST['myname'];
               }
     }

    $link mysql_connect('XX''XX''XX')
        or die(
    'Could not connect: ' mysql_error());
    echo 
    'Connected successfully';
    mysql_select_db('XX') or die('Could not select database');

    // Performing SQL query
    echo "connectivity is done";
    echo 
    $query "INSERT INTO myname(myname)values ('".$myname."')";
    $result mysql_query($query) or die('Query failed: ' mysql_error());

    // Printing results in HTML
    if(mysql_affected_rows() > 0){

    echo 
    "this is result"mysql_affected_rows();


         }else{

           echo 
    "this is no result";

         }
    // Closing connection

    ?>
    HTML Code:
    <html>
    <head>
    <body>
    <form action="<?=$PHP_SELF;?>"" method="post" name="productFrm">
    <div class="row">
    
    <label for="name">MyName</label>
    
    <input type="text" name="myname" size="25" value="" />
    
    </div>
    
    <h4>My friends</h4>
    
    
    
    <div class="row">
    
    <label for="friend1">1</label>
    
    <input type="text" name="friend1" size="25" />
    
    </div>
    
    <div class="row">
    
    <label for="friend2">2</label>
    
    <input type="text" name="friend2" size="25" />
    
    </div>
    </div><div class="row">
    
    
    <span class="col6"><input type="submit" name="next" value="next" class="button_style" /></span>
    
    <span class="col4"><input type="reset" value="Reset" class="button_style" /></span>
    </div>
    </form>
    </body>
    </html>
    2)addfriend_preview.php

    PHP Code:
    <?

    var_dump
    ($_POST);
    if(
    $_POST['submit'] == "submit")

    {

    $link mysql_connect('10.0.0.4''root''gk123')
        or die(
    'Could not connect: ' mysql_error());
    echo 
    'Connected successfully';
    mysql_select_db('deepali') or die('Could not select database');

    // Performing SQL query
    echo "connectivity is done";
    echo 
    $query "INSERT INTO friends(friend,myid) values ('".$friend."','".$muid."')";
    $result mysql_query($query) or die('Query failed: ' mysql_error());

    // Printing results in HTML
    if(mysql_affected_rows() > 0){

    echo 
    "this is result"mysql_affected_rows();


         }else{

           echo 
    "this is no result";

         }
    // Closing connection
    mysql_close($link);
                }else{
                echo 
    "values are not submiited";
                }
                  
    // return $this;.
    ?>
    HTML Code:
    <html>
    
    <head>
    
    <body>
    <form action="<?=$PHP_SELF;?>" method="post" name="productFrm">
    <div class="row">
    	<label for="name">MyName</label>
    	<input type="text" name="myname" size="25" value="<?=$_POST['myname'];?> " readonly />
    </div>
    <!-- <h4>My friends</h4>	  -->
    
    
    <br/>
    
    <div class="row">
    	<label for="friend1">1</label>
    	<input type="text" name="friend1" value="<?=$_POST['friend1'];?>" size="25" /> &nbsp; &nbsp;<span class="col4"><input type="submit" name="submit" value="Add" class="button_style" onClick="javascript:return fmvalidate();" /></span> &nbsp; &nbsp;<span class="col4"><input type="submit" name="submit" value="Remove" class="button_style" onClick="javascript:return fmvalidate();" /></span>
    </div>
    <br/>
    <div class="row">
    	<label for="friend2">2</label>
    	<input type="text" name="friend1" value="<?=$_POST['friend2'];?>" size="25" />&nbsp; &nbsp;<input type="submit" name="submit" value="Add" class="button_style" onClick="javascript:return fmvalidate();" /></span> &nbsp; &nbsp;<span class="col4"><input type="submit" name="submit" value="Remove" class="button_style" onClick="javascript:return fmvalidate();" /></span>
    </div>
    <br/><div class="row">
    
    <span class="col4"><input type="submit" name="submit" value="submit" class="button_style" onClick="javascript:return fmvalidate();" /></span>
    
    <span class="col4"><input type="reset" value="Reset" class="button_style" /></span>
    </div>
    </form>
    </body>
    </html>
    thanks in advance.

  2. #2
    SitePoint Addict
    Join Date
    Jul 2008
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could use a session variable.

    addfriend.php
    PHP Code:
    session_start();


    // Performing SQL query
    echo "connectivity is done";
    echo 
    $query "INSERT INTO myname(myname)values ('".$myname."')";
    $result mysql_query($query,$link) or die('Query failed: ' mysql_error());

    $_SESSION['myid'] = mysql_insert_id($link); 
    addfriend_preview.php
    PHP Code:
    session_start();


    // Performing SQL query
    echo "connectivity is done";
    echo 
    $query "INSERT INTO friends(friend,myid) values ('".$friend."','".$_SESSION['myid']."')";
    $result mysql_query($query,$link) or die('Query failed: ' mysql_error());

    #if variable is no longer needed empty and destroy
    $_SESSION = array();
    session_destroy(); 

  3. #3
    SitePoint Guru deepM's Avatar
    Join Date
    Dec 2007
    Location
    India
    Posts
    705
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks spiderling,

    i want to ask you, what action i can keep here form action="addfriend_preview.php" or form action="<?=$PHP_SELF;?>" ?

    because its not working yet.

  4. #4
    SitePoint Addict
    Join Date
    Jul 2008
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If your HTML code is part of the PHP file, you can use PHP_SELF as the action. If not, then the form action needs to be the PHP file processing the data for that form.

    But you have another issue. Once the addfriend.php has completed processing, there is nothing taking the user to the HTML form for addfriend_preview.php.

  5. #5
    SitePoint Guru deepM's Avatar
    Join Date
    Dec 2007
    Location
    India
    Posts
    705
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    spiderling, value is got inserted once in the second table(friends).but now showing the error
    Query failed: Out of range value adjusted for column 'myid' at row 1now my code is
    addfriend.php
    Code:
    <?
    
    session_start();
    
    if($_SERVER['REQUEST_METHOD'] == "POST")
       {
           $myname                 	= $_POST['myname'];
    
    
    }
    if($_POST['submit'] == "next")
    {
           $myname                 	= $_POST['myname'];
    echo hi;
    $link = mysql_connect('xx', 'xx', 'xxx')
        or die('Could not connect: ' . mysql_error());
    echo 'Connected successfully';
    mysql_select_db('xxx') or die('Could not select database');
    
    // Performing SQL query
    echo "connectivity is done";
    session_start();
    echo $query = "INSERT INTO myname(myname)values ('".$myname."')";
    
    $result = mysql_query($query,$link) or die('Query failed: ' . mysql_error());
    
    $_SESSION['id'] = mysql_insert_id($link);
    
    
    // Printing results in HTML
    if(mysql_affected_rows() > 0){
    
    echo "this is result". mysql_affected_rows();
    
    
         }else{
    
           echo "this is no result";
    
         }
    // Closing connection
    }
    
    ?>
    
    <html>
    <head>
    <body>
    <form action="addfriend_preview.php" method="post" name="productFrm">
    <div class="row">
    
    <label for="name">MyName</label>
    
    <input type="text" name="myname" size="25" value="" /><label class="errorText"><?=$mynameErr;?></label>
    
    </div>
    
    </div><div class="row">
    
    
    <span class="col6"><input type="submit" name="next" value="next" class="button_style" /></span>
    
    <span class="col4"><input type="reset" value="Reset" class="button_style" /></span>
    
    </div>
    
    </form>
    </body>
    </html>
    addfriend_preview.php

    Code:
    <?php
    
    session_start();
    
    
    var_dump($_POST);
    
    if($_SERVER['REQUEST_METHOD'] == "GET"){
       $myname                  = "";
    
    
       }else if($_SERVER['REQUEST_METHOD'] == "POST")
       {
    
               $friends     = $_POST['friends'];
      }
    if($_POST['submit'] == "Add")
    
    {
    
    $link = mysql_connect('xx', 'xx', 'xxx')
        or die('Could not connect: ' . mysql_error());
    echo 'Connected successfully';
    mysql_select_db('xxx') or die('Could not select database');
    
    // Performing SQL query
    echo "connectivity is done";
    
    echo $query = "INSERT INTO friends(friends,myid) values ('".$friends."','".$_SESSION['id']."')";
    
    $result = mysql_query($query,$link) or die('Query failed: ' . mysql_error());
    
    
    
    #if variable is no longer needed empty and destroy
    
    $_SESSION = array();
    
    session_destroy();
    
    // Printing results in HTML
    if(mysql_affected_rows() > 0){
    
    echo "this is result". mysql_affected_rows();
    
    
         }else{
    
           echo "this is no result";
    
         }
    // Closing connection
    mysql_close($link);
    	        }else{
    	        echo "values are not submiited";
    	        }
    	          // return $this;.
    ?>
    
    <html>
    
    <head>
    
    <body>
    <form action="<?=$PHP_SELF;?>" method="post" name="productFrm">
    <div class="row">
    	<label for="name">MyName</label>
    	<input type="text" name="myname" size="25" value="<?=$_SESSION['myname'];?> " readonly />
    </div>
    <!-- <h4>My friends</h4>	  -->
    
    
    <br/>
    
    <div class="row">
    	<label for="friend">1</label>
    	<input type="text" name="friends" value= ""  size="25" />&nbsp; &nbsp;<input type="submit" name="submit" value="Add" class="button_style" onClick="javascript:return fmvalidate();" /></span> &nbsp; &nbsp;<span class="col4"><input type="submit" name="submit" value="Remove" class="button_style" onClick="javascript:return fmvalidate();" /></span>
    </div>
    
    
    
    </form>
    
    
    
    </body>
    
    </html>

  6. #6
    SitePoint Addict
    Join Date
    Jul 2008
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The $_SESSION['id'] is not being given a value. When you go to addfriend.php the form comes up, and the PHP is not processed. Therefore no value is given to the session variable. See this forms tutorial.

    You need to:
    1. Create an HTML document with the form HTML for addfriend.
    2. Have the above form action submit to a PHP file, which has the addfriend PHP code and the HTML form code for addfriend_preview.
    3. Have the form action from #2 above submit to a PHP file which has the addfriend_preview PHP code.

  7. #7
    SitePoint Guru deepM's Avatar
    Join Date
    Dec 2007
    Location
    India
    Posts
    705
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The $_SESSION['id'] is not being given a value
    as i said, values are inserted once. where i got this session id inserted there.
    id myname
    1 abc
    id friends myid
    1 bbb 1
    so i think i am getting session id. but getting this error.Query failed: Out of range value adjusted for column 'myid' at row 1

    is it because the way you have mention that id is not passing but then how come i get first value than

  8. #8
    SitePoint Guru deepM's Avatar
    Join Date
    Dec 2007
    Location
    India
    Posts
    705
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The $_SESSION['id'] is not being given a value. When you go to addfriend.php the form comes up, and the PHP is not processed. Therefore no value is given to the session variable.
    how can i do that? i have tried but the session value is not passing next page.

    addfriend.php, ihave this
    $_SESSION['id'] = mysql_insert_id($link);
    then what i have to written on the next page so i ll get this session value?

  9. #9
    SitePoint Addict
    Join Date
    Jul 2008
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    deepson2,

    I've explained how to structure your form submission process below. It is a matter of taking the code you currently have and structuring it better so that the process flows and the variable is carried from one page to the other. I would suggest you read these tutorials and the other PHP related tutorials as a starting point to build your knowledge and understanding.
    PHP Forms and User Input
    PHP Sessions

    You need to:
    1. Create an HTML page with the form HTML for addfriend.
    2. Have the above form action submit to a PHP file, which has the addfriend PHP code and the HTML form code for addfriend_preview.
    3. Have the form action from #2 above submit to a PHP file which has the addfriend_preview PHP code.
    1. (addfriend.html)
    HTML Code:
    <html>
    <head>
    <body>
    <form action="addfriend.php" method="post" name="productFrm">
    <div class="row">
    <label for="myname">MyName</label>
    <input type="text" name="myname" size="25" value="" />
    </div>
    </div><div class="row">
    <span class="col6"><input type="submit" name="nextbtn" value="next" class="button_style" /></span>
    <span class="col4"><input type="reset" value="Reset" class="button_style" /></span>
    </div>
    </form>
    </body>
    </html>
    2.(addfriend.php)
    PHP Code:
    <?php
    session_start
    ();

    if(
    $_POST['nextbtn'] == "next")
    {
      
    $myname  $_POST['myname'];

    echo 
    hi;
    $link mysql_connect('xx''xx''xxx')
        or die(
    'Could not connect: ' mysql_error());
    echo 
    'Connected successfully';
    mysql_select_db('xxx') or die('Could not select database');

    // Performing SQL query
    echo "connectivity is done";

    $query "INSERT INTO myname (myname) VALUES ('$myname')";
    $result mysql_query($query,$link) or die('Query failed: ' mysql_error());
    $_SESSION['id'] = mysql_insert_id($link);

    // Printing results in HTML
    if(mysql_affected_rows() > 0){
    echo 
    "this is result"mysql_affected_rows();
         }else{
           echo 
    "this is no result";
         }
    // Closing connection
    mysql_close($link);
    }
    ?>
    <html>
    <head>
    <body>
    <form action="addfriend_preview.php" method="post" name="productFrm">
    <div class="row">
        <label for="name">MyName</label>
        <?php echo $myname;?>
    </div>
    <!-- <h4>My friends</h4>      -->
    <br/>
    <div class="row">
        <label for="friends">1</label>
        <input type="text" name="friends" value= ""  size="25" />&nbsp; &nbsp;<input type="submit" name="submit" value="Add" class="button_style" onClick="javascript:return fmvalidate();" /></span> &nbsp; &nbsp;<span class="col4"><input type="submit" name="submit" value="Remove" class="button_style" /></span>
    </div>
    </form>
    </body>
    </html>
    3.(addfriend_preview.php)
    PHP Code:
    <?php

    session_start
    ();
    if(
    $_SERVER['REQUEST_METHOD'] == "POST")
       {

    $friends $_POST['friends'];

    $link mysql_connect('xx''xx''xxx')
        or die(
    'Could not connect: ' mysql_error());
    echo 
    'Connected successfully';
    mysql_select_db('xxx') or die('Could not select database');

    // Performing SQL query
    echo "connectivity is done";

    $query "INSERT INTO friends (friends,myid) VALUES ('$friends','".$_SESSION['id']."')";
    $result mysql_query($query,$link) or die('Query failed: ' mysql_error());

    #if variable is no longer needed empty and destroy
    $_SESSION = array();
    session_destroy();

    // Printing results in HTML
         
    if(mysql_affected_rows() > 0){
           echo 
    "this is result"mysql_affected_rows();
         }else{
           echo 
    "this is no result";
         }
    // Closing connection
    mysql_close($link);
    }else{
     echo 
    "values are not submiited";
    }
    ?>

  10. #10
    SitePoint Guru deepM's Avatar
    Join Date
    Dec 2007
    Location
    India
    Posts
    705
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    good morning spiderling,

    Yesterday i have tried everything including the code which you have mention today. It works for once and then my session id didn't stay at my page. so i discussed this with my senior also. as you suggested i have to work on session.and have to create first page like log in form.so once myname is taken it should keep it for all pages. right?

    thanks for your much appriciated help. i will try your code also. i just have to learn how login form works.once i do that i can do this also.

    if any suggestion please tell me.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •