0

So the website I have at the minute isn't inserting the data correctly. I do have id and sub_date (date it got submitted) but these are automatic I would of thought so don't need to create variables for it and stuff? Correct me if im wrong?

PHP Code:

<?php
if (isset($_POST['addBtn']))
{
    $m_titleAdd = $_POST['m_titleAdd'];
    $authorAdd = $_POST['authorAdd'];
    $statusAdd = $_POST['statusAdd'];
    $tutorialAdd = $_POST['tutorialAdd'];
    if (empty($errors))
    {
        $SQLinsert = $odb -> prepare("INSERT INTO `methods` VALUES(NULL, :id, :m_title, :sub_date, :status, :author, :tutorial)");
        $SQLinsert -> execute(array(':id' => null, ':m_title' => $m_titleAdd, ':sub_date' => null, ':status' => $statusAdd, ':author' => $authorAdd, ':tutorial' => $tutorialAdd));
        echo '<div class="nNote nSuccess hideit"><p><strong>SUCCESS: </strong>Method has been added</p></div>';
    }
    else
    {
        echo '<div class="nNote nFailure hideit"><p><strong>ERROR:</strong><br />';
        foreach($errors as $error)
        {
            echo '-'.$error.'<br />';
        }
        echo '</div>';
    }
}
?>

HTML Code:

<form action="" method="POST">
        <fieldset>
                <div class="formRow">
                    <label>Method Title:</label>
                    <div class="formRight"><input type="text" name="m_titleAdd" class="form-control round" style="width:300px;" /></div>
                    <div class="clear"></div>
                </div>
                <div class="formRow">
                    <label>Tutorial:</label>
                    <div class="formRight"><textarea style="resize:none;" rows="3" cols="15" name="tutorialAdd" class="autoGrow form-control round"></textarea></div>
                    <div class="clear"></div>
                </div>
                <div class="formRow">
                    <label>Author:</label>
                    <div class="formRight"><input type="text" name="authorAdd" class="form-control round" style="width:300px;" /></div>
                    <div class="clear"></div>
                </div>
                <div class="control-group">
                <label class="control-label">Type:</label>
                <div class="controls">
                <select class="btn btn-default dropdown-toggle" name="status" tabindex="1">
                <option value="Working" />Working
                <option value="Plausible" />Plausible
                <option value="Not Working" />Not Working
                </select>
                </div>
                </div><br>
                <div class="formRow">
                    <div style="width:100px;"><input type="submit" value="Add" name="addBtn" class="btn btn-primary btn-block btn-round" /></div>
                     <div class="clear"></div>
                </div><br>
        </fieldset>
    </form>
Clay
  • 4,700
  • 3
  • 33
  • 49
char
  • 13
  • 2
  • Insert `null` twice? What for? – u_mulder Jan 16 '16 at 07:49
  • @u_mulder Where did I insert it twice? – char Jan 16 '16 at 07:51
  • `VALUES(NULL, :id` and `':id' => null` – u_mulder Jan 16 '16 at 07:52
  • @u_mulder Removed first NULL so its VALUES(:id, :m_title, etc.. problem still remains. Can you see why its not working? Thanks – char Jan 16 '16 at 08:00
  • what are the columns for your table? Assuming you're using PDO, you can use this to get any error information - http://php.net/manual/en/pdo.errorinfo.php – Clay Jan 16 '16 at 08:12
  • @Chris Banks I am sorry to be a pain in the arse but I am a very fresh beginner at PHP and not too sure how I would implement the error information? Also, do you means database table? – char Jan 16 '16 at 08:17
  • yes, post all the columns that are in your `methods` table. Below where you call `execute()` add this line: `var_dump($SQLInsert->errorInfo());` – Clay Jan 16 '16 at 08:19
  • @Chris Banks http://imgur.com/MQJhWxH excuse the fact that im on mobile at the minute – char Jan 16 '16 at 08:23
  • Still not 100% sure where to put error info as there are already code in the () of execute. – char Jan 16 '16 at 08:25
  • At the line below `$SQLInsert->execute(....);` put this line `var_dump($SQLInsert->errorInfo());` – Clay Jan 16 '16 at 08:31
  • @Chris Banks it just said there is a fatal error on the line with errorInfo()? – char Jan 16 '16 at 08:35
  • in my comment I had `$SQLInsert` (uppercase I), it's supposed to be a lowercase i, can you try with the lowercase i. – Clay Jan 16 '16 at 08:40
  • @Chris Banks array(3) { [0]=> string(5) "23000" [1]=> int(1048) [2]=> string(30) "Column 'status' cannot be null" } – char Jan 16 '16 at 08:44

1 Answers1

0

$_POST['statusAdd'] is undefined and in your form it is actually named status. So just change the following.

Change this:

$statusAdd = $_POST['statusAdd'];

To this:

$statusAdd = $_POST['status'];

Then for your insert query make sure you change this, you don't need to specify null for the id in this case:

VALUES(NULL, :id, etc

To this:

VALUES(:id, etc
Clay
  • 4,700
  • 3
  • 33
  • 49