Jump to content

Need reference to auto insert Integers


co.ador

Recommended Posts

I am wondering how can I insert integers in the database without manually input it in the form.

 

I am looking to fill the product_id and customer_id table. I would do it but only by sequence and '' quotes at the VALUE part of an INSERT statement. Also condition the insert of product_id and customer_id according to the id in the SESSION variable.

 

<?php 
session_start();

if (isset($_SESSION['id'])) {
    $userid = $_SESSION['id'];
    $username = $_SESSION['username'];
   	

$fname = $_POST['firstname'];
$lname = $_POST['lastname']; 
$telephone = $_POST['telephone'];
        $city = $_POST['city'];
$state = $_POST['state'];
$itemname = $_POST['product_name'];
$price = $_POST['price'];
$details = $_POST['details'];
        $category = $_POST['category'];
$subcategory = $_POST['subcategory'];
        $product_id = $_POST['product_id'];
$customer_id = $_POST['customer_id'];
        $date_sold = $_POST['date_sold'];



      $sqlinsert = "INSERT INTO customers
                            (id,firstname,lastname,telephone,city,state,product_name,price,details)            
                            VALUES('','$fname','$lname', '$telephone','$city','$state','$itemname','$price','$details')";

      $sqlinsert2 = "INSERT INTO products
                            (id,product_name,price,details,category,subcategory)            
                            VALUES('','$itemname','$price','$details','$category','$subcategory')";

$sqlinsert3 = "INSERT INTO sales
                               (id,product_id, customer_id, date_sold)            
                               VALUES('','$product_id','$customer_id','$date_sold')";


$enterquery = mysql_query($sqlinsert) or die(mysql_error()); 
$enterquery2 = mysql_query($sqlinsert2) or die(mysql_error()); 
$enterquery3 = mysql_query($sqlinsert3) or die(mysql_error()); 
}
?>

 

The above insert its respective fields in each table but how is it possible to INSERT it according to the SESSION['id]; so that it INSERT according to the costumer id plus that it generates and INSERT automatically in the product_id and customer_id related to the product_id and customer_id fields in products and customer table? without manually doing it.

 

any references will be appriciated because the above code seems like the product_id and customer_id won't INSERT in relations to the customer and product table.

 

One more thing the product_id and customer_id won't be pass from the form to this file but rather a value will be INSERT it in their field according to the id in customer and products table.

Link to comment
https://forums.phpfreaks.com/topic/233170-need-reference-to-auto-insert-integers/
Share on other sites

Yes, but in special the product_id and customer_id table.  The rest will be entered by them through a form. but product_id and customer_id how are they entered?

 

customers table

 

id

customers_id

name

last name

 

 

 

products table

 

id

product_name

product_id

 

 

sales table

 

id

product_id

customer_id

date_entered

 

 

the above are the tables and its fields.

I still need more information, sorry, it's late here and I may be reading things wrong, hah! My question is, what do you want the values of customer_id and product_id to be? If product_id is already set VIA post, you would insert it normally like everything else.

 

Anyways, for customer_id, how would you manage to make that specially for that user? You could do it via IP, but it's not recommended. Do you have register & login system?

 

if you want to use the id from a row that you just inserted, you can call it back using  mysql_insert_id()

 

<?php 
session_start();

if (isset($_SESSION['id'])) {
    $userid = $_SESSION['id'];
    $username = $_SESSION['username'];
   	

$fname = $_POST['firstname'];
$lname = $_POST['lastname']; 
$telephone = $_POST['telephone'];
        $city = $_POST['city'];
$state = $_POST['state'];
$itemname = $_POST['product_name'];
$price = $_POST['price'];
$details = $_POST['details'];
        $category = $_POST['category'];
$subcategory = $_POST['subcategory'];
        $product_id = $_POST['product_id'];
$customer_id = $_POST['customer_id'];
        $date_sold = $_POST['date_sold'];



      $sqlinsert = "INSERT INTO customers
                            (id,firstname,lastname,telephone,city,state,product_name,price,details)            
                            VALUES('','$fname','$lname', '$telephone','$city','$state','$itemname','$price','$details')";
      $enterquery = mysql_query($sqlinsert) or die(mysql_error()); 
      $customer_id= mysql_insert_id();

      $sqlinsert2 = "INSERT INTO products
                            (id,product_name,price,details,category,subcategory)            
                            VALUES('','$itemname','$price','$details','$category','$subcategory')";
      $enterquery2 = mysql_query($sqlinsert2) or die(mysql_error());
      $product_id =  mysql_insert_id();
      	$sqlinsert3 = "INSERT INTO sales
                               (id,product_id, customer_id, date_sold)            
                               VALUES('','$product_id','$customer_id','$date_sold')";




$enterquery3 = mysql_query($sqlinsert3) or die(mysql_error()); 
}
?>

I understand the $customer_id variable as in

$customer_id= mysql_insert_id();
below the insert in costumers table will grab the id of the costumer table right? and  the same for $product_id as in $product_id= mysql_insert_id(); below the INSERT for products?

 

Then you insert those variables in the VALUES for the INSERT into the sales table..

 

 

the mysql_insert_id is a fantastic function becuase it will record the id of the previous INSERT.

 

Great function.

 

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.