MySQL Question

djcustom

New Member
Messages
17
Reaction score
0
Points
0
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>
 

xPlozion

New Member
Messages
868
Reaction score
1
Points
0
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
 

djcustom

New Member
Messages
17
Reaction score
0
Points
0
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.
 

xPlozion

New Member
Messages
868
Reaction score
1
Points
0
ah, do a query to search against the values entered, and do a order by id desc limit 1
 

sarvar

New Member
Messages
82
Reaction score
0
Points
0
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:
$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.
 

djcustom

New Member
Messages
17
Reaction score
0
Points
0
Everything works Great thanks for all the help
 
Last edited:
Top