简体   繁体   中英

Insert multiple values in MySql Column

I have information from a form that i would like to enter into a MySQL DB, The normal insert works great for me but I would like to combine two text fields into one Column in the database.

Below is the current code I use to insert values to MySQL

$make = $_POST['make'];
$model = $_POST['model'];

UPDATE gs.gs_objects SET
    vin = '".$vin."',
    plate_number = '".$engine."',
    model = '".$make."' '".$model."', //I am not sure if this is correct as it keeps giving me error!!
    installer = '".$installer."'
WHERE imei = '$imei'

I am trying to add the make and model into on Column Ford Ranger as it is in two separate fields in my form

You have unwanted single quotes here :

model = '".$make."' '".$model."'

Should be written as :

model = '".$make." ".$model."'

Query :

$query = 
    "UPDATE gs.gs_objects SET
        vin = '".$vin."',
        plate_number = '".$engine."',
        model = '".$make." ".$model."', 
        installer = '".$installer."'
    WHERE imei = '$imei'";

NB : anyone on SO will strongly suggest to use prepared statements and parameterized queries , to protect your code from SQL injection and make your queries more readable and maintainable. Such typo is far much easier to detect when using parameterized queries.

The best is to have two different columns (it gives more option if you want to select a specific information). But if you want to keep all the information in one column, you can do it in a variable to avoid concatenation in a query.

$make = $_POST['make'];
$model = $_POST['model'];

$complete_model = $make.' '.$model;

UPDATE gs.gs_objects SET
                vin = '".$vin."',
                plate_number = '".$engine."',
                model = '".$complete_model."',
                installer = '".$installer."'
                WHERE imei = '".$imei."'

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