0

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

2
  • 1
    If you run such a query, be sure to check for error messages thrown by MySQL - it will inform you quickly whether that works or not Commented Feb 4, 2019 at 14:08
  • 1
    Also you should use prepared Statements Commented Feb 4, 2019 at 14:10

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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."'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.