Home > Blockchain >  Fetch on multiple tables select option with multiple values insert (joins fetch insert)
Fetch on multiple tables select option with multiple values insert (joins fetch insert)

Time:07-07

I have two tables for cars details, one for type of fuel and another for the cars. The fields and table names are as follows:

table name: |**viaturas**| 
fields: idviatura, matricula, marca, km, idtipocomb 

**combustivel** |    
fields: idcombustivel, tipocomb.

What I want to do is display on a form a select option that brings the select options of matricula, and then when I submit the form, it passes to insert.php automaticly the tipocomb value of that matricula. I tried with the following codes: in

form.php

<?php
$con = mysqli_connect("localhost","root","","xxxx");
$sql = "SELECT * FROM viaturas 
            INNER JOIN combustivel ON viaturas.idcomb = combustivel.idcomb";  
$result = mysqli_query($con, $sql);  
?>
    <select name="matricula">
<?php 
while ($matricula = mysqli_fetch_array($result,MYSQLI_ASSOC)):; 
?>
        <option value="<?php echo $matricula["matricula"];?>|<?php echo $matricula["tipocomb"];?>"><?php echo $matricula["matricula"];?>  
        </option>
<?php
endwhile; 
?>

Edited and on insert.php I tried many types of things but never worked I need to know how to pass the value tipocomb and send it to insert PHP and have the correct syntax, so that when I submit the form, in insert PHP it $_POSTS it correctly. the insert.php looks like this:


if(isset($_POST['submit']))
{
    include('database_connection.php');
    list($matricula, $tipocomb) = explode("|", $_POST["matricula"]);
    $query = "
    INSERT INTO tabledata (matricula, tipocomb) 
    VALUES(:matricula, :tipocomb)
    ";
    $statement->bindValue(':matricula', $matricula);
    $statement->bindValue(':tipocomb', $tipocomb);
    $statement = $connect->prepare($query);
    $statement->execute(
        array(
            ':matricula'        =>  $matricula,
            ':tipocomb'     =>   $tipocomb,
                )
    );
    $result = $statement->fetchAll();

    if(isset($result))
    {
        echo 'done';
    }

}

?>

it's still not working

CodePudding user response:

In whatever PHP file that handles your form upload...

Using PHP list and explode to capture the pipe | deliminated values separately:

list($matricula, $tipocomb) = explode("|", $_POST["matricula"]);
echo "$matricula $tipocomb";

// Given an example $_POST["matricula"] value of "car|fuel" this echos the string:
// "car fuel"

Alternately you can leave the result of explode in an array:

$matricula_arr = explode("|", $_POST["matricula"]);
echo "{$matricula_arr[0]} {$matricula_arr[1]}";

// Given an example $_POST["matricula"] value of "car|fuel" this also echos the string:
// "car fuel"

insert.php

// db  prep
// data validation

list($matricula, $tipocomb) = explode("|", $_POST["matricula"]);
$sth = $dbh->prepare('INSERT INTO tabledata (matricula, tipocomb) VALUES (:matricula, :tipocomb)');
$sth->execute(array(
  ":matricula" => $matricula,
  ":tipocomb" => $tipocomb
));

...or:

$stmt = $mysqli->prepare("INSERT INTO tbl (matricula, tipocomb) VALUES (?, ?)");
list($matricula, $tipocomb) = explode("|", $_POST["matricula"]);
$stmt->bind_param("ss", $matricula, $tipocomb);
$stmt->execute();

...or:

$stmt = $mysqli->prepare("INSERT INTO tabledata VALUES (?, ?)");
$stmt->bind_param('ss', $matricula, $tipocomb);
list($matricula, $tipocomb) = explode("|", $_POST["matricula"]);
$stmt->execute();

...or:

list($matricula, $tipocomb) = explode("|", $_POST["matricula"]);
$sth = $dbh->prepare('INSERT INTO tabledata (matricula, tipocomb) VALUES (:matricula, :tipocomb)');
$sth->bindValue(':matricula', $matricula);
$sth->bindValue(':tipocomb', $tipocomb);
$sth->execute();
  •  Tags:  
  • php
  • Related