SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,223
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    problem in inserting

    Hello,
    Whenever I start a new project, I am sometimes perplexed in data processing.

    I am, actually, dumping the old project and start a new project again.
    Dumping and Starting again several times...
    Code:
    myTable
    
    n
    10
    I have "myTable" like the above.
    The table has just one column named "n".
    The table has just one record.
    The record has a numeric value "10" for the column "n".

    Code:
    code
    
    <?php
    $query="select n 
    from myTable";
    $sql=mysql_query($query);
    $row=mysql_fetch_assoc($sql);
    
    $mysql_query=("INSERT INTO myTable ( n ) VALUES ( 20 ) "); 
    
    exit($row['n']);
    
     ?>
    
    result
    
    10
    I have the PHP code above in one of my pages.

    DB connection code is before the code above.


    The browsing result of the code above is "10".
    That means DB connection is correctly working.
    But the data "20" for the column "n" is not inserted.

    What's the problem in my data inserting code above ?
    Why is the data "20" not inserted (in your thinking or guessing) ?
    How can I insert the data "20" to myTable?

    I am in perplexed at the moment.

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,215
    Mentioned
    58 Post(s)
    Tagged
    3 Thread(s)
    you assigned a string consisting of an sql query to a php variable

    you never executed it
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,223
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by r937 View Post
    you assigned a string consisting of an sql query to a php variable
    Would you please rephrase it?

    Quote Originally Posted by r937 View Post
    you never executed it
    How can I execute it? (I used to successfully insert the data with the code above in the old project.)

  4. #4
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,215
    Mentioned
    58 Post(s)
    Tagged
    3 Thread(s)
    here, you assign a value to a variable --
    Code:
    $query="select n 
    from myTable";
    here, you execute it --
    Code:
    $sql=mysql_query($query);
    here, you assign a value to another variable --
    Code:
    $mysql_query=("INSERT INTO myTable ( n ) VALUES ( 20 ) ");
    but then you forgot to execute it
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  5. #5
    SitePoint Enthusiast
    Join Date
    Aug 2011
    Location
    OH, USA
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dotJoon View Post
    Would you please rephrase it?

    How can I execute it? (I used to successfully insert the data with the code above in the old project.)
    something like that
    $result = mysql_query("INSERT INTO myTable ( n ) VALUES ( 20 ) ")

  6. #6
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,223
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Code:
    $query=("INSERT INTO myTable ( n ) VALUES ( 20 ) "); 
    $sql=mysql_query($query);
    With the code above, I've succeeded inserting the data.
    Anyway, the problem is solved.
    I am still wonder that why the data is successfully inserted in the old project without the execution "$sql=mysql_query($query);".


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
  •