SitePoint Sponsor

User Tag List

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

    can i fire the same query multiple time?

    hello,

    I am fetching id from one page to next page. here i want to use 4 to 5 text boxes.each one will have the same add/remove button so i just want to ask that, can i fire the same insertion code for 4 times? or is there any another way?

    while doing this when i apply query for second time my session value is not keeping(?) so can anyone tell me what can i do?

    this is my code
    1)first page
    PHP Code:
    $result mysql_query($query,$link) or die('Query failed: ' mysql_error());

    $_SESSION['id'] = mysql_insert_id($link); 
    2)second page
    <?php

    session_start();
    if($_SERVER['REQUEST_METHOD'] == "GET"){
    echo $SESSION['id'];
    }else if($_SERVER['REQUEST_METHOD'] == "POST")
    {

    $friends = $_POST['friends'];
    $myid = $_SESSION['id'];
    $myname =$_SESSION['myname'];
    }




    if($_POST['submit'] == "Add")
    {
    $link = mysql_connect('===', '==', '===')
    or die('Could not connect: ' . mysql_error());
    echo 'Connected successfully';
    mysql_select_db('===') or die('Could not select database');

    echo $query = "INSERT INTO friends(friends,myid) values ('".$friends."','".$myid."')";

    $result = mysql_query($query,$link) or die('Query failed: ' . mysql_error());

    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";
    }
    }
    ?>
    <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="<?=$myname;?> " readonly />
    <input type="hidden" name="myid" value="<?=$_SESSION['id'];?>" />

    </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>
    <div class="row">
    <label for="friend">2</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>

    any help will be appreciated.
    thanks in advance.

  2. #2
    SitePoint Guru deepM's Avatar
    Join Date
    Dec 2007
    Location
    India
    Posts
    705
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    function AddItem($myid,$friends)
    {

    echo 
    $query "INSERT INTO friends(friends,myid) values ('".$friends."','".$myid."')";
    }

    if(
    $_POST['submit'] == "Add")
    {
    AddItem($myid,$friends);

    I have made this fuction with hoping that i can fire the query multiple time. values are getting added but the session value is stored only once.afterward its showing empty field.
    can anyone tell me how can i keep $myid forever for this page?

  3. #3
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,047
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    Tou shouldn't need to issue the query multiple times unless the fields change.

    INSERT INTO friends (friends,myid) values (?,?),(?,?),(?,?),(?,?),(?,?)....

    replace the placeholders with the actual data and build this with a loop.

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

    thanks for your reply.

    lets take an example, we can comment on blog many times as much as we want once we logged in . I have to work on session and the log in forms. once user logged in, he/she can post multiple comment or do the operation twice or more times.

    so just working on that. any more suggestion please tell me.

  5. #5
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,047
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    Are you talking hypothetically because that doesn't seem like the correct approach? A user shouldn't be able to post multiple comments all at once. I get what your saying by they can post any number, but not all at once - right?

  6. #6
    SitePoint Guru deepM's Avatar
    Join Date
    Dec 2007
    Location
    India
    Posts
    705
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    they can post any number, but not all at once - right
    not at once but still on the same page and the insertion query will be same.


    textbox add remove

    textbox add remove
    i just want to do something like this and add it in same table(id textbox myid)
    where my problem was when i used to fire query for first time, it was adding into database. but for the second time it was adding but myid value was not getting added.

    so i want to work on that. its like log in form where the id keeps same till we wont log out.


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
  •