hoi,
ik ben eens iets nieuws aan het proberen, ik zou data willen invoeren in mijn mysql database. ik heb bv 2 tabellen, de id van tabel1 wil ik ook gebruiken in tabel 2 en zou tijdens de post moeten meegestuurd worden en dat krijg ik niet in orde.
momenteel heb ik deze tabel structuur:
db = deviceDB
devices
devices_id
devices_type_id
devices_date
devices_serial
devices_type
devices_type_id
devices_type_name
devices_type_model
de form voor de insert te doen:
nu had ik tussen die form graag een select box waar ik de value voor de post van device_type_id zou meesturen van wat er geselcteerd word in een select field zoals dit:
Maar hier krijg ik dan de error op dat device_type_id niet null mag zijn
"Notice: Undefined index: devices_type_id in"
Hoe kan ik er voor zorgen dat die id toch word meegenomen?
ik ben eens iets nieuws aan het proberen, ik zou data willen invoeren in mijn mysql database. ik heb bv 2 tabellen, de id van tabel1 wil ik ook gebruiken in tabel 2 en zou tijdens de post moeten meegestuurd worden en dat krijg ik niet in orde.
momenteel heb ik deze tabel structuur:
db = deviceDB
devices
devices_id
devices_type_id
devices_date
devices_serial
devices_type
devices_type_id
devices_type_name
devices_type_model
de form voor de insert te doen:
PHP:
<?php
if (isset($_POST['submit']))
{
require "../config.php";
require "../common.php";
try
{
$connection = new PDO($dsn, $username, $password, $options);
$nieuwe_device = array(
"devices_serial" => $_POST['devices_serial'],
"devices_date" => $_POST['devices_date']
);
$sql = sprintf(
"INSERT INTO %s (%s) values (%s)",
"devices",
implode(", ", array_keys($nieuwe_device)),
":" . implode(", :", array_keys($nieuwe_device))
);
$statement = $connection->prepare($sql);
$statement->execute($nieuwe_device);
}
catch(PDOException $error)
{
echo $sql . "<br>" . $error->getMessage();
}
}
?>
<?php require "templates/header.php"; ?>
<?php
if (isset($_POST['submit']) && $statement)
{ ?>
<blockquote><?php echo $_POST['devices_serial']; ?> is succesvol toegevoegd.</blockquote>
<?php
} ?>
<h2>Nieuwe Devices</h2>
<form method="post">
<label for="devices_serial">Serienummer</label>
<input type="text" name="devices_serial" id="devices_serial">
<label for="devices_date">Datum</label>
<input type="text" name="devices_date" id="devices_date">
<input type="submit" name="submit" value="Submit">
</form>
<a href="index.php">Home</a>
<?php require "templates/footer.php"; ?>
nu had ik tussen die form graag een select box waar ik de value voor de post van device_type_id zou meesturen van wat er geselcteerd word in een select field zoals dit:
PHP:
<?php
/**
* Use an HTML form to create a new entry in the
* users table.
*
*/
if (isset($_POST['submit']))
{
require "../config.php";
require "../common.php";
try
{
$connection = new PDO($dsn, $username, $password, $options);
$nieuwe_device = array(
"devices_serial" => $_POST['devices_serial'],
"devices_date" => $_POST['devices_date'],
"devices_type_id" => $_POST['devices_type_id'],
);
$sql = sprintf(
"INSERT INTO %s (%s) values (%s)",
"devices",
implode(", ", array_keys($nieuwe_device)),
":" . implode(", :", array_keys($nieuwe_device))
);
$statement = $connection->prepare($sql);
$statement->execute($nieuwe_device);
}
catch(PDOException $error)
{
echo $sql . "<br>" . $error->getMessage();
}
}
?>
<?php require "templates/header.php"; ?>
<?php
if (isset($_POST['submit']) && $statement)
{ ?>
<blockquote><?php echo $_POST['devices_serial']; ?> is succesvol toegevoegd.</blockquote>
<?php
} ?>
<h2>Nieuwe Devices</h2>
<form method="post">
<label for="devices_serial">Serienummer</label>
<input type="text" name="devices_serial" id="devices_serial">
<label for="devices_date">Datum</label>
<input type="text" name="devices_date" id="devices_date">
<?php
//Connect to our MySQL database using the PDO extension.
$pdo = new PDO('mysql:host=localhost;dbname=mydb', 'root', 'mypassword' '');
//Our select statement. This will retrieve the data that we want.
$sql = "SELECT devices_type_id, devices_type_name, devices_type_model FROM devices_type";
//Prepare the select statement.
$stmt = $pdo->prepare($sql);
//Execute the statement.
$stmt->execute();
//Retrieve the rows using fetchAll.
$users = $stmt->fetchAll();
?>
<select>
<?php foreach($users as $user): ?>
<option value="<?= $user['devices_type_id']; ?>"><?= $user['devices_type_name']; ?><?= $user['devices_type_model']; ?></option>
<?php endforeach; ?>
</select>
<input type="submit" name="submit" value="Submit">
</form>
<a href="index.php">Home</a>
<?php require "templates/footer.php"; ?>
Maar hier krijg ik dan de error op dat device_type_id niet null mag zijn
"Notice: Undefined index: devices_type_id in"
Hoe kan ik er voor zorgen dat die id toch word meegenomen?