1

I need some help

Is there a way to make this in PDO? https://stackoverflow.com/a/1899508/6208408 Yes I know I could change to mysql but I use a mssql server and can't use mysql. I tried some things but I'm not as good with PDO as mysql... It's hard to find some good examples of inserting array's into database with PDO. So quickly said I have a PDO based code connected to a mssql webserver.

best regards joep

I tried this before:

//id
$com_id = $_POST['com_id'];
//array

$mon_barcode = $_POST['mon_barcode'];                   
$mon_merk = $_POST['mon_merk'];
$mon_type = $_POST['mon_type'];
$mon_inch = $_POST['mon_inch'];
$mon_a_date = $_POST['mon_a_date'];
$mon_a_prijs = $_POST['mon_a_prijs'];

$data = array_merge($mon_barcode, $mon_merk, $mon_type, $mon_inch, $mon_a_date, $mon_a_prijs);
try{
    $sql = "INSERT INTO IA_Monitor (Com_ID, Barcode, Merk, Type, Inch, Aanschaf_dat, Aanschaf_waarde) VALUES (?,?,?,?,?,?,?)";
    $insertData = array();


    foreach($_POST['mon_barcode'] as $i => $barcode)                        
    {
        $insertData[] = $barcode;
    }

    if (!empty($insertData))
    {
        implode(', ', $insertData);
        $stmt = $conn->prepare($sql);
        $stmt->execute($insertData);

    }
}catch(PDOException $e){
    echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
1
  • @Cemal correct, I edited it Commented Feb 21, 2018 at 14:04

1 Answer 1

0

The code below should fix your problems.

$db_username='';
$db_password='';

$conn = new \PDO("sqlsrv:Server=localhost,1521;Database=testdb", $db_username, $db_password,[]);
//above added per @YourCommonSense's request to provide a complete example to a code fragment

if (isset($_POST['com_id'])) { //was com_id posted?
    //id
    $com_id = $_POST['com_id'];
    //array
    $mon_barcode = $_POST['mon_barcode'];
    $mon_merk = $_POST['mon_merk'];
    $mon_type = $_POST['mon_type'];
    $mon_inch = $_POST['mon_inch'];
    $mon_a_date = $_POST['mon_a_date'];
    $mon_a_prijs = $_POST['mon_a_prijs'];

    $sql = "INSERT INTO IA_Monitor (Com_ID, Barcode, Merk, Type, Inch, Aanschaf_dat, Aanschaf_waarde) VALUES (?,?,?,?,?,?,?)";
    try {
        $stmt = $conn->prepare($sql);
        foreach ($mon_barcode as $i => $barcode) {
            $stmt->execute([$com_id, $barcode, $mon_merk[$i], $mon_type[$i], $mon_inch[$i], $mon_a_date[$i], $mon_a_prijs[$i]]);
        }
    } catch (\PDOException $e) {
        echo $sql . "<br>" . $e->getMessage();
    }
}
$conn = null;
Sign up to request clarification or add additional context in comments.

20 Comments

it prints me this on my screen prepare($sql); foreach ($mon_barcode as $i => $barcode) { $insertData = array_merge([$com_id,$barcode],$data); $stmt->execute($insertData); } } catch (PDOException $e) { echo $sql . " " . $e->getMessage(); } $conn = null; } ?>
that is like half of the code , so after $conn it starts prepare, probably something wrong while i made my connection
This code does what you ask, can this be done in PDO https://stackoverflow.com/a/1899508/6208408
Can you upload a screenshot of your output?
@YourCommonSense May I ask what I have missed?
|

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.