0

The SQL, this is a lot of code, so perhaps this is not the only issue.

SQL

public function save(): int
    {
        $pdo = Database::connect();  // note the static method call

        $sql = "INSERT INTO `boat` VALUES(null,:name, :reg_num, :length, :image, :owner_id)";
        $statement = $pdo->prepare($sql);
        //$statement->bindParam(':id', $getId, PDO::PARAM_INT);
        $statement->bindParam(':name', $this->name, PDO::PARAM_STR);
        $statement->bindParam(':reg_num', $this->regNumber, PDO::PARAM_STR);
        $statement->bindParam(':length', $this->length, PDO::PARAM_INT);
        $statement->bindParam(':image', $this->image, PDO::PARAM_STR);
        $statement->bindParam(':owner_id', $this->ownerId, PDO::PARAM_INT);

        if ($statement->execute()) {
            return 1;
        } else {
            return 0;
        };

The HTML and PHP, the database is not processing the id and owner_id fields, I end up with a 0 in both of these columns and an error occurs.

Page 1

<!DOCTYPE html>
    <html>
    <head>
        <title>Upload</title>
        <h1>New boat</h1>
    </head>
    <body>
        <form action="add_boat_save.php" method="post">
            Choose owner:<br>
            <?php

            function __autoload($class_name) {
                include $class_name . '.php';
            }

            // MAKE NEW owner object
            $owner = new owner(0, "", 0, 0, "", 0);

            $owners = Owner::findAll();

            echo "<select name='owner_id'>";
                foreach ($owners as $owner) {

                    echo "<option value='" . $owner->getId() . "''>" . $owner->getFirstName() . " ". $owner->getLastName() ."</option>";
                }
                echo "</select>";

            ?>
            <br>
            Boat name:<br>
            <input type="text" name="name"><br>
            Boat reg_num:<br>
            <input type="text" name="reg_num"><br>
            Boat length:<br>
            <input type="text" name="length"><br>
            image of Boat:<br>
            <select name="image" id="image"><br>
                <option value="" selected="selected"></option>
            <?php

            $dir = "../extracted/images";//your path
            $dh  = opendir($dir);
            while (false !== ($filename = readdir($dh))) {
            $files[] = htmlentities($filename);
            }
            sort($files);
            foreach($files as $filename) {
            echo "<option value='" . $filename . "'>".$filename."</option>";}

            ?>
            </select>
            <br>
            <input type="submit" name="submit" value="Submit"/>
            <?php
            echo "<br><br>";
            echo "Click here to return";
            echo "<p><a href='index.php'>[ BOAT DATABASE ]</a></p>";
            echo "Click here to add an owner";
            echo "<p><a href='add_owner.php'>[ ADD OWNER HERE ]</a></p>";
            ?>
        </form>
        <?php

        $dir_path = "icons/boat/";
        $extensions_array = array('jpg','png','jpeg');

        if(is_dir($dir_path))
        {
            $files = scandir($dir_path);

            for($i = 0; $i < count($files); $i++)
            {
                if($files[$i] !='.' && $files[$i] !='..')
                {

                    // get file extension
                    $file = pathinfo($files[$i]);
                    $extension = $file['extension'];

                    // check file extension
                    if(in_array($extension, $extensions_array))
                    {
                        // show image
                        echo "<img src='$dir_path$files[$i]' height=\"360\" .
                     IMG STYLE=\"position:absolute; TOP:70px; LEFT:280px; \" SRC=\"getImage\">";
                    }
                }
            }
        }

        ?>
    </body>
</html>

Page 2

<?php
/**
 * Created by PhpStorm.
 * User: Josh
 * Date: 3/4/2017
 * Time: 4:47 PM
 */

// Declare an autoloader
function __autoload($class_name) {
    include $class_name . '.php';
}

echo "<h1>Boat saved to database</h1>";

var_dump($length);
var_dump($ownerId);

// MAKE NEW boat object

//$id = intval ($_POST['id']);
$name = filter_var($_POST['name'],FILTER_SANITIZE_FULL_SPECIAL_CHARS);
$regNumber = filter_var ($_POST['reg_num'],FILTER_SANITIZE_FULL_SPECIAL_CHARS);
$length = filter_var(intval($_POST['length'],FILTER_SANITIZE_NUMBER_INT));
$image = filter_var($_POST['image'],FILTER_SANITIZE_FULL_SPECIAL_CHARS);
$ownerId = filter_var(intval($_POST['owner_id'],FILTER_SANITIZE_NUMBER_INT));

$save_boat = new Boat(null, "$name", "$regNumber", $length, "$image", $ownerId);
?>

<?php

$dir_path = "icons/saved/";
$extensions_array = array('jpg','png','jpeg');

if(is_dir($dir_path))
{
    $files = scandir($dir_path);

    for($i = 0; $i < count($files); $i++)
    {
        if($files[$i] !='.' && $files[$i] !='..')
        {

            // get file extension
            $file = pathinfo($files[$i]);
            $extension = $file['extension'];

            // check file extension
            if(in_array($extension, $extensions_array))
            {
                // show image
                echo "<img src='$dir_path$files[$i]' height=\"200\" .
                     IMG STYLE=\"position:absolute; TOP:92px; LEFT:280px; \" SRC=\"getImage\">";
            }
        }
    }
}

// INSERT - TO BE IMPLEMENTED !!
echo "Inserting now<br>";
$save_boat->save();
echo "<br>";
echo "Click here to add a boat";
echo "<p><a href='add_boat.php'>[ ADD BOAT HERE ]</a></p>";
echo "Click here to add an owner";
echo "<p><a href='add_owner.php'>[ ADD BOAT HERE ]</a></p>";
echo "Click here to return";
echo "<p><a href='index.php'>[ BOAT DATABASE ]</a></p>";
mplungjan
  • 169,008
  • 28
  • 173
  • 236
J.H
  • 1
  • Welcome to SO. Please post a [mcve] instead of reams of code. PS: Do not call anything `name="submit"` if you ever want to programatically submit a form – mplungjan Mar 11 '17 at 06:52
  • Your database structure would probably help here. Make sure your ID field is auto-increment and is the table's primary key. Look into `DESCRIBE table;` to see your table structure, if you don't have access to phpMyAdmin – Marc-Antoine Parent Mar 11 '17 at 06:56

0 Answers0