1

I am inserting a car mileage and car color into a database, on the same page I am displaying that cars ID and the cars owner.

So I have two input fields where the user types the mileage and color and beneath that I am just displaying that cars ID and the cars owner.

The insert works fine, I can insert the mileage and color but it leaves two columns where the cars ID and cars owner (which are displayed on the page where I enter mileage/color) should be...

Does anyone know of a way to put them into the DB too as im not actually typing them, simply echoing them there. Code is shown below, thanks.

    Mileage:<input type="text" name="mileage"><br />
    Color:<input type="text" name="Color"><br />
    carID:<?php echo $cID; ?>
    carOwner: <?php echo $cOwner; ?>

The insert page is just a simple sql statement which only has the mileage/color values . I have tried adding carID and carOwner as extras but it throws errors.......any ideas?

3 Answers3

0

Crude but easiest method and will match the style of the other data:

Show the carID and carOwner but prevent them from being edited:

Mileage:<input type="text" name="mileage"><br />
Color:<input type="text" name="Color"><br />
carID:<input type="text" name="cID" value="<?php echo $cID; ?>" readonly="readonly"><br />
carOwner:<input type="text" name="cOwner" value="<?php echo $cOwner; ?>" readonly="readonly"><br />

Forgot to add, the data will be posted just the same as "mileage" or "Color" for you to process however you like.

JBES
  • 1,512
  • 11
  • 18
  • that is still displaying a textbox, i forgot to mention the $cID and $cOwner is coming from a PHP statement above and outside this form... – user3334417 Feb 21 '14 at 01:01
0

This is how to make Your input type hidden:

Mileage:<input type="text" name="mileage"><br />
Color:<input type="text" name="Color"><br />
carID:<?php echo $cID; ?><input type="hidden" name="cid" value="<?php echo $cID; ?>" /><br />
carOwner: <?php echo $cOwner; ?><input type="hidden" name="cowner" value="<?php echo $cOwner; ?>" /><br />

You should both echo $cID and $cOwner and include them as hidden values. Then on the page where You've sent this form You can get the values cid and cowner the same as mileage, Color to insert into the database. For post and get methods this would be respectively:

$_POST['mileage']
$_POST['Color']
$_POST['cid']
$_POST['cowner']

or

$_GET['mileage']
$_GET['Color']
$_GET['cid']
$_GET['cowner']

Here is a not so safe way to insert those values into the database:

INSERT INTO table_name (column_name1,column_name2,column_name3,column_name4) VALUES (`$_POST['mileage']`,$_POST`['Color']`,`$_POST['cid']`,`$_POST['cowner']`);

The safer way would be to use:

$st = $db->prepare('INSERT INTO table_name (...fields...) VALUES (?,?,?,?)');
$st->execute($values);

where $values is an array of values You want to insert.

You can form the $values array from $_POST like that:

$values[] = $_POST['mileage'];
$values[] = $_POST['Color'];
...

Reference: PDO::prepare, PDO with INSERT INTO through prepared statements.

Not so bad tutorial is located here. It will help You to get the idea, but it has some little errors, that are fixed in the comments of that post.

Community
  • 1
  • 1
Dmytro Dzyubak
  • 1,562
  • 2
  • 22
  • 35
0

Try using hidden inputs. They act just like a normal input tag but the user never sees them.

<input type="hidden" name="cID" value="<?php echo $cID; ?>">
<input type="hidden" name="cOwner" value="<?php echo $cOwner; ?>">
Mileage:<input type="text" name="mileage"><br />
Color:<input type="text" name="Color"><br />
carID:<?php echo $cID; ?>
carOwner: <?php echo $cOwner; ?>

Now you will receive 4 fields in your $_POST or $_GET whichever you are using

$_POST['cID']
$_POST['cOwner']
$_POST['mileage']
$_POST['Color']
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149