1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

help with form INSERT INTO

Discussion in 'PHP' started by cgo85, Apr 9, 2008.

  1. #1
    I'm trying to insert data from a form into my DB. For some reason, I can't seem to figure this out. Here is the code I have:

    <html>
    <head>
    </head>
    <center>
    <form method="post" action="script.php">
    <input type="hidden" name="id" value="null">
    <table>
    <tr><td align="left">cityurl</td>
    <td><input type="text" name="cityurl"></td>
    </tr>
    <tr><td align="left">abbr</td>
    <td><input type="text" name="abbr" size="20"></td>
    </tr>
    <tr><td align="left">neigh</td>
    <td><input type="text" name="neigh" size="20"></td>
    </tr>
    <tr><td colspan="2">
    <p align="center">
    <input type="submit" value="Enter record">
    </td>
    </tr>
    </table>
    </form>
    </center>
    </html>
    Code (markup):
    AND this is the php page:


    <?
    include 'config.php';
    $table = "neighborhood";
    
    include 'opendb.php';
    
    $sqlquery = "INSERT INTO $table VALUES('$id','$cityurl','$abbr','$neigh') WHERE cityurl='$cityurl' AND abbr='$abbr'";
    
    $results = mysql_query($sqlquery);
    
    include 'closedb.php';
    
    print "<html><body><center>";
    print "<p>You have just entered this record<p>";
    print "City : $cityurl<br>";
    print "State : $abbr<br>";
    print "Neigh :$neigh";
    print "</body></html>";
    ?>
    PHP:
     
    cgo85, Apr 9, 2008 IP
  2. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Do you have globals enabled like that? Is it only the INSERT query that is not working (does it print out what you entered properly under the "You have just entered this record" paragraph)?

    If not, you'll have to change it to use $_GET['cityurl'], etc, rather than $cityurl. You should of course make it safe for your query though before trying to insert it..

    $cityurl = addslashes($_GET['cityurl']);


    EDIT: Nevermind. I just realized that you know how to do the $_GET stuff by finding your other post and I moved the scroll bar over to the right and found your problem. You don't put a WHERE clause for INSERTS. It's inserting data, not grabbing it, so how could the WHERE possibly work?

    $sqlquery = "INSERT INTO $table VALUES('$id','$cityurl','$abbr','$neigh')";

    Just remember what I said about making your data safe first though by running addslashes() (stripslashes() would be the opposite).
     
    zerxer, Apr 9, 2008 IP
  3. cgo85

    cgo85 Peon

    Messages:
    380
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #3
    You were asking if this posts any of the info on the 2nd page... the answer is no. Even with this altered code above. I somewhat understand what you're saying, however, i already have fields named 'cityurl' and 'abbr'... I want the info from the form to enter the corresponding 'neigh' info when i enter it. For example, If I enter Belltown for 'neigh', Seattle for 'cityurl', and WA for 'state', I need it to find all instances where Seattle WA is in my db and add the 'neigh' information into the 'neighborhood' field that's created.

    How can I designate which field to put the 'neigh' info into?

    <?
    
      $cityurl = addslashes($_GET['cityurl']);
      $abbr = addslashes($_GET['abbr']);
      $neigh = addslashes($_GET['neigh']);
      $id = addslashes($_GET['id']);
      
    include 'config.php';
    $table = "demographic";
    
    include 'opendb.php';
    
    $sqlquery = "INSERT INTO $table VALUES('$neigh') WHERE cityurl='$cityurl' AND abbr='$abbr'";
    
    $results = mysql_query($sqlquery);
    
    include 'closedb.php';
    
    print "<html><body><center>";
    print "<p>You have just entered this record<p>";
    print "City : $cityurl<br>";
    print "State : $abbr<br>";
    print "Neigh :$neigh";
    print "</body></html>";
    ?>
    PHP:


    I really appreciate you help!
     
    cgo85, Apr 9, 2008 IP
  4. srobona

    srobona Active Member

    Messages:
    577
    Likes Received:
    57
    Best Answers:
    0
    Trophy Points:
    88
    #4
    use $_POST instead of $_GET in the variable. Because you are using 'method = post' in the form. Hope it will work. :)
     
    srobona, Apr 9, 2008 IP
    cgo85 likes this.
  5. cgo85

    cgo85 Peon

    Messages:
    380
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #5
    srobona, that seems to be passing the info to the next page but it's still not loading it to the DB. I think the main issue now is: How can I designate which field to put the 'neigh' info into? If i want the value for 'neigh' to go into field (neighborhood) in table (demographic) how would i do that?
     
    cgo85, Apr 9, 2008 IP
  6. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Oops, I feel stupid now for telling you to use $_GET and not $_POST. It's 3 am. :(


    Anyways, you can do this:

    INSERT INTO {$table} SET neighborhood='{$neigh}'


    after you've defined $neigh, of course


    If you want to set more, you would just add a comma after the last single quote and put another thing like cityurl='{$cityurl}' and just keep going, separating them with commas.
     
    zerxer, Apr 10, 2008 IP
    srobona and cgo85 like this.
  7. cgo85

    cgo85 Peon

    Messages:
    380
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #7
    OMG i figured it out! Thanks everyone... I did this:

    <?

    $cityurl = addslashes($_POST['cityurl']);
    $abbr = addslashes($_POST['abbr']);
    $neigh = addslashes($_POST['neigh']);
    $id = addslashes($_POST['id']);

    include 'config.php';
    $table = "demographic";

    include 'opendb.php';

    $sqlquery = "UPDATE $table SET neighborhood='$neigh' WHERE cityurl='$cityurl' AND abbr='$abbr'";

    $results = mysql_query($sqlquery);

    include 'closedb.php';

    print "<html><body><center>";
    print "<p>You have just entered this record<p>";
    print "City : $cityurl<br>";
    print "State : $abbr<br>";
    print "Neigh :$neigh";
    print "</body></html>";
    ?>
     
    cgo85, Apr 10, 2008 IP
    srobona likes this.
  8. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #8
    That will only update an existing record already in the database... you didn't want to insert a new record?
     
    zerxer, Apr 10, 2008 IP
  9. cgo85

    cgo85 Peon

    Messages:
    380
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #9
    It seems to be working fine with the update. Plus, I kind of like using that because if I make a mistake it's easy to fix.

    Thanks zerxer!
     
    cgo85, Apr 10, 2008 IP