Hi
I have the following form at http://test.genealogyresearchassistance.co.uk/newLinks.php
The code is
<form id="add" action="add.php" method="get">
<label for="name" id="name">Name of site</label>
<input type="text" name="name" required="yes"><br/>
<label for="link" id="link">Link of site</label>
<input type="url" name="link" required="yes"><br/>
<label for="desc" id="desc">A bit about the site</label>
<textarea name="desc" required="yes"></textarea><br/>
<label for="location"> Location:</label>
<?php
include 'database_conn.php';
$sql = "SELECT Lo_ID, Lo_Name
FROM location";
$queryResult = $dbConn->query($sql);
if($queryResult === false) {
echo "<p>Query failed: ".$dbConn->error."</p>\n</body>\n</html>";
exit;
}
else {
while ($rowObj = $queryResult->fetch_object()) {
$Lo_ID = $rowObj->Lo_ID;
$Lo_Name = $rowObj->Lo_Name;
echo "<input type='checkbox' name='location[]' value='$Lo_ID'>$Lo_Name<br/>";
}
}
$dbConn->close();
?>
<label for="type"> Type of site: </label>
<?php
include 'database_conn.php';
$sql = "SELECT TypeID, Typename
FROM type_tab";
$queryResult = $dbConn->query($sql);
if($queryResult === false) {
echo "<p>Query failed: ".$dbConn->error."</p>\n</body>\n</html>";
exit;
}
else {
echo "<select name='type' id='type'>";
while ($rowObj = $queryResult->fetch_object()) {
$typeid = $rowObj->TypeID;
$Typename = $rowObj->Typename;
echo "<option value='$typeid'>$Typename</option>";
}
echo "</select>";
}
$dbConn->close();
?>
<input type="submit" value="send it!">
<input type="reset" value="Reset">
As you can see the user should be able to select more than 1 location, however the add.php will not loop tough the cheack boxes. The add.php code is below
<?php
include 'database_conn.php'; // make db connection
$lo_id = $_REQUEST['location'];
$ty_id = isset($_REQUEST['type']) ? $_REQUEST['type']: null;
$name = isset($_REQUEST['name']) ? $_REQUEST['name']: null;
$desc = isset($_REQUEST['desc']) ? $_REQUEST['desc']: null;
$link = isset($_REQUEST['link']) ? $_REQUEST['link']: null;
$id = 0;
foreach ($lo_id as $location) {
$sql = "INSERT INTO `siteInfo`(`ref`, `Link`, `Typeinfo`, `Location`,
`sitename`, `Disc`) VALUES ('$id', '$link', '$ty_id', '$location', '$name', '$desc')";
}
$queryResult = $dbConn->query($sql);
$dbConn ->close();
What is going wrong, only 1 location is being entered, even when more than 1 are selected.
Error log
[19-Jun-2017 11:38:02 UTC] PHP Warning: Invalid argument supplied for foreach() in /home/genealo1/test.genealogyresearchassistance.co.uk/add.php on line 24
Thanks
P