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

Recommended Answers

All 4 Replies

The loop is not executing the insert query, because the statement is executed at line 13. So only the last will be inserted. The insert can fail for different reasons:

  • $id does not increment through the loops, i.e. with: $i++;
  • when you execute the form again, $id will start from zero and if column ref is a primary key, it will generate an error for duplicate entry

It is better to use auto_increment on MySQL side and remove the column from the insert statement. The loop, by itself, seems fine, but the error refers to line 24, here there are only 14 lines: do you have other loops in the add script?

Hi

The id has a trigger so that is ok.

Sorry I copied wrong full code

<?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();
if($queryResult === false) {
    echo "<p>Query failed: ".$dbConn->error."</p>\n</body>\n</html>";
    exit;
}

else {
    include "database_conn.php";
    echo "<p>Added  $name with link of $link</p>";
    echo "<p>The description was $desc</p>";
    foreach ($Lo_ID as $location) {
        $sql = "SELECT Lo_Name 
                    FROM location
                    WHERE Lo_ID = $location";
        $queryResult = $dbConn->query($sql);
        $rowObj = $queryResult->fetch_object();
        $lo_name = $rowObj->Lo_Name;
        echo "Location is $lo_name";
    }
    $dbConn ->close();

    include 'database_conn.php';
    $sql = "SELECT Typename 
                    FROM type_tab
                    WHERE TypeID = $ty_id";
    $queryResult = $dbConn->query($sql);
    $rowObj = $queryResult->fetch_object();
    $type = $rowObj->Typename;
    echo "Type of $type";
    $dbConn->close();
}

Querie (line 13) is out of foreach (lines 9-11)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.