+ Reply to Thread
Results 1 to 6 of 6

Thread: MySQL Question

  1. #1
    djcustom is offline x10Hosting Member djcustom is an unknown quantity at this point
    Join Date
    Jul 2008
    Posts
    15

    MySQL Question

    I am still learning MySQL. I am using the code below to submit to a database. The form at the bottom submits to a comment page where where a page id which is linked to the ID of each entry. How would I go about getting the ID of the data entered during submission. Keep in mind multiple people may submit at the same time so it cant be based off of last entry to database. Thanks for any help.
    Code:
    <?php
    
    $business = strip_tags($_POST['business']);
    $owner = strip_tags($_POST['owner']);
    $address = strip_tags($_POST['address']);
    $city = strip_tags($_POST['city']);
    $state = strip_tags($_POST['state']);
    $zip = strip_tags($_POST['zip']);
    $phone = strip_tags($_POST['phone']);
    
    
    
            $db_server = "localhost";
            $db_name = "******";
            $db_user = "******";
            $db_pass = "******";
            $con = mysql_connect($db_server, $db_user, $db_pass) or die(mysql_error());
            mysql_select_db($db_name, $con);
           
            $sql = "INSERT INTO landlords (business, owner, address, city, state, zip, phone)
                    VALUES ('".$business."','".$owner."', '".$address."', '".$city."', '".$state."', '".$zip."', '".$phone."')";
            $result = mysql_query($sql);
            if(!$result){
                return ("<p>SQL Error<br />".$sql."<br />".mysql_error()."</p>");
            }
    ?>
    
    <form name="add_comment" method="post" action="add_comment.php" target="formtarget">
    <input type="hidden" name="business" id="business" value="<?php echo $business;?>" />
    <input type="hidden" name="owner" id="owner" value="<?php echo $owner;?>" />
    <input type="hidden" name="address" id="address" value="<?php echo $address;?>"/>
    <input type="hidden" name="city" id="city" value="<?php echo $city;?>"/>
    <input type="hidden" name="state" id="state" value="<?php echo $state;?>"/>
    <input type="hidden" name="zip" id="zip" value="<?php echo $zip;?>"/>
    <input type="hidden" name="phone" id="phone" value="<?php echo $phone;?>"/>
    <input type="hidden" name="page_id" id="page_id" value="<?php echo $page_id;?>"/>
    <input type="submit" name="submit" id="submit" />
    </form>

  2. #2
    xPlozion's Avatar
    xPlozion is offline x10 Elder xPlozion is an unknown quantity at this point
    Join Date
    Mar 2008
    Location
    Delaware, USA
    Posts
    872

    Re: MySQL Question

    if you want id to be unique, then in the database setup, set the id row to be primary auto_increment ;). you don't need to have that field in when entering data into the db, cause i'll increment itself

  3. #3
    djcustom is offline x10Hosting Member djcustom is an unknown quantity at this point
    Join Date
    Jul 2008
    Posts
    15

    Re: MySQL Question

    I think you misunderstood me. I already have the database set up. My issue is I need to retrieve the ID once it is created.

  4. #4
    xPlozion's Avatar
    xPlozion is offline x10 Elder xPlozion is an unknown quantity at this point
    Join Date
    Mar 2008
    Location
    Delaware, USA
    Posts
    872

    Re: MySQL Question

    ah, do a query to search against the values entered, and do a order by id desc limit 1

  5. #5
    sarvar's Avatar
    sarvar is offline x10Hosting Member sarvar is an unknown quantity at this point
    Join Date
    Sep 2007
    Posts
    82

    Re: MySQL Question

    Quote Originally Posted by xPlozion View Post
    ah, do a query to search against the values entered, and do a order by id desc limit 1
    Let me code what xPlozion told you.

    PHP Code:
    $sql mysql_query("SELECT * FROM `landlords` WHERE `owner` = '".$owner."' AND `phone` = '".$phone."' ORDER BY `id` DESC LIMIT 1");
    $sql_info mysql_fetch_assoc($sql);

    $new_id $sql_info['id']; 
    $new_id is the variable to get ID of the inserted query.
    CLICK HERE to Thank Me.
    (You must be logged in into forums.)

  6. #6
    djcustom is offline x10Hosting Member djcustom is an unknown quantity at this point
    Join Date
    Jul 2008
    Posts
    15

    Re: MySQL Question

    Everything works Great thanks for all the help
    Last edited by djcustom; 01-28-2009 at 12:41 PM.

+ Reply to Thread

Similar Threads

  1. PHP MySQL Question..
    By anuj_web in forum Programming Help
    Replies: 7
    Last Post: 04-26-2008, 05:43 AM
  2. Remote Access to MySQL database
    By donward in forum Free Hosting
    Replies: 4
    Last Post: 04-02-2008, 07:07 PM
  3. Mysql help needed: getting number of items in table column
    By xprtweb in forum Programming Help
    Replies: 1
    Last Post: 02-23-2008, 01:17 AM
  4. 2 questions about MySQL
    By chrisosx in forum Scripts & 3rd Party Apps
    Replies: 0
    Last Post: 07-06-2005, 03:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers