简体   繁体   中英

How do I make a query with variables submitted by the user, and save it?

I'm a database noob.
I'm building a database with a bunch of properties.
I want to make it so that the property owner (the user) can submit his/her property to my property table, by only calling a function/stored routine/stored procedure/prepared statement (I googled them, but having a hard time figuring out which I'm looking for).

Here is my insert query code:

INSERT INTO property
(property_owner_id, property_type_id, address, zip_code, area_m2, price_€)
VALUES
(/* Owner ID */,/* Property Type ID */,/* Address */,/* Zip Code */,/* Area */,/* Price */);

So I would want to create something like a submit function that I could store. Then, the property owner would only need to do something like:

submit(his id, his property's type, address, zip code, area, price)

I'm searching for answers and I see stuff with "@", other times I don't.
I'm also don't quite yet understand the whole "function/stored routine/stored procedure/prepared statement" thing.
It also seems to change from language to language.
In some examples, I need to set the variable/parameter beforehand. In others I don't.

I just started learning SQL. I know how a database works in general.
I know how to create tables, update them, insert data, create primary and foreign keys.
I know how to query data using join, left join, cross join, etc. and the where, having, order by, group by, etc. commands.
But as of right now, I don't really understand how to make a reusable block of code where I can just call its name, and insert the variables/parameters only.

If it helps, I'm using MySQL Workbench 6.2.5 with UwAmp.

Thanks!

You probably already read about the stored procedures but maybe this link is helpful.

To your question: You want a procedure that can accept a parameter when it is called.

CREATE PROCEDURE addtoproperty(
    IN ownerid INT,
    IN propertytype INT,
    IN adress VARCHAR,
    IN zipcode INT,
    IN area VARCHAR,
    IN price DECIMAL(15,2)      
)
BEGIN
    INSERT INTO property
        (property_owner_id, property_type_id, address, zip_code, area_m2, price_€)
    VALUES
        (ownerid,propertytype, adress, zipcode, area, price);
END;

And you would use this procedure with following statement. You need to replace the text with the text with the actual values this time. So ownerid would be something like 12345.

CALL addtoproperty(ownerid,propertytype, adress, zipcode, area, price);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM