0

I'm trying to create an application form with PHP. I've set up the HTML markup, and created the PHP processor. To no avail, unfortunately. I am not getting any errors, but the database is not updating either. Would be very greatful if you could have a look at my code.

HTML Markup:

<form method="post" action="<?php echo base_url();?>index.php/new_post" id="application-form">
                        <table class="application">
                            <tbody>
                                <tr>
                                    <td class="left">Question 1</td>
                                    <td><input type="text" size="30" name="question1" id="question1"/></td>
                                </tr>
                                <tr>
                                    <td class="left">Question 2</td>
                                    <td><input type="text" size="30" name="question2" id="question2"/></td>
                                </tr>
                                <tr>
                                    <td class="left">Question 3</td>
                                    <td><input type="text" size="30" name="question3" id="question3"/></td>
                                </tr>
                                <tr>
                                    <td class="left">Question 4</td>
                                    <td><input type="text" size="30" name="question4" id="question4"/></td>
                                </tr>
                                <tr>
                                    <td class="left">Question 5</td>
                                    <td><textarea rows="4" cols="30" name="question5" id="question5"></textarea></td>
                                </tr>
                                <tr>
                                    <td class="left">Question 6</td>
                                    <td><textarea rows="4" cols="30" name="question6" id="question7"></textarea></td>
                                </tr>
                                <tr>
                                    <td class="left">Question 7</td>
                                    <td><input type="text" size="30" name="question7" id="question7"/></td>
                                </tr>
                                <tr>
                                    <td class="left">Question 8</td>
                                    <td><textarea rows="4" cols="30" name="question8" id="question8"></textarea></td>
                                </tr>
                                <tr>
                                    <td class="left">Question 9</td>
                                    <td><textarea rows="4" cols="30" name="question9" id="question9"></textarea></td>
                                </tr>
                                <tr>
                                    <td class="left">Question 10</td>
                                    <td><textarea rows="4" cols="30" name="question10" id="question10"></textarea></td>
                                </tr>
                                <tr>
                                    <td class="left">Question 11</td>
                                    <td><textarea rows="4" cols="30" name="question11" id="question11"></textarea></td>
                                </tr>
                                <tr>
                                    <td class="left">Question 12</td>
                                    <td><input type="text" size="30" name="question12" id="question12"/></td>
                                </tr>
                                <tr>
                                    <td class="left">Question 13</td>
                                    <td><textarea rows="4" cols="30" name="question13" id="question13"></textarea></td>
                                </tr>
                                <tr>
                                    <td class="left">Question 14</td>
                                    <td><textarea rows="4" cols="30" name="question14" id="question14"></textarea></td>
                                </tr>
                                <tr>
                                    <td class="left">Question 15</td>
                                    <td><input type="text" size="30" name="question15" id="question15"/></td>
                                </tr>                           
                            </tbody>
                        </table>
                        <button type="submit" class="btn btn-block btn-danger martop15">Send Application</button>
                    </form>

PHP Processor:

<?php 
    if(!empty($_POST)){
        $question1 = $_POST['question1']; 
        $question2 = $_POST['question2']; 
        $question3 = $_POST['question3']; 
        $question4 = $_POST['question4']; 
        $question5 = $_POST['question5']; 
        $question6 = $_POST['question6']; 
        $question7 = $_POST['question7']; 
        $question8 = $_POST['question8']; 
        $question9 = $_POST['question9']; 
        $question10 = $_POST['question10']; 
        $question11 = $_POST['question11']; 
        $question12 = $_POST['question12']; 
        $question13 = $_POST['question13']; 
        $question14 = $_POST['question14']; 
        $question15 = $_POST['question15']; 

    }

        $mysqli = new mysqli('localhost', 'root', '', 'database');

        $mysqli->query("INSERT INTO `applications`(`question1`, `question2`, `question3`, `question4`, `question5`, `question6`, `quesion7`, `question8`, `question9`, `question10`, `question11`, `question12`, `question13`, `question14`, `question15`) VALUES (`$question1`, `$question2`, `$question3`, `$question4`, `$question5`, `$question6`, `$question7`, `$question8`, `$question9`, `$question10`, `$question11`, `$question12`, `$question13`, `$question14`, `$question15`)");
?>

Thanks in advance.

Shepred
  • 5
  • 2
  • 3
    Remove the ticks `\`` from your values and replace them with quotes `'`. Those are values, not columns. Had you been checking for errors, you'd of been told about it. A quick search & replace will fix that in a jiffy. – Funk Forty Niner Feb 10 '15 at 04:19
  • You're not seeing any errors because you're not looking for them. Add some error checking to your `mysqli_*()` calls. –  Feb 10 '15 at 04:21
  • You should really look into sanitizing your data as well. – KyleMassacre Feb 10 '15 at 04:23
  • 1
    your code is vulnerable to sql injection :/ – NullPoiиteя Feb 10 '15 at 04:27
  • Nota: You also may have mispelled the word "quesion7" for that column, something that wasn't noted anywhere, except the answer I edited to make it correct. – Funk Forty Niner Feb 10 '15 at 18:44

3 Answers3

0

Please use

   $queryResult =  $mysqli->query("INSERT INTO applications(question1, question2, question3, question4, question5, question6,quesion7, question8, question9, question10, question11, question12, question13, question14,question15) VALUES ('$question1', '$question2', '$question3', '$question4', '$question5', '$question6', '$question7', '$question8', '$question9', '$question10','$question11', '$question12', '$question13','$question14', '$question15')");

Remove `` from insert into TableName (here,here)

For error handling

if ($queryResult === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: ";
}
Pratik Joshi
  • 11,485
  • 7
  • 41
  • 73
0

If your statement fails, you can output the error message by doing the following:

if (!$mysqli->query("INSERT INTO `applications`(`question1`, `question2`, `question3`, `question4`, `question5`, `question6`, `quesion7`, `question8`, `question9`, `question10`, `question11`, `question12`, `question13`, `question14`, `question15`) VALUES (`$question1`, `$question2`, `$question3`, `$question4`, `$question5`, `$question6`, `$question7`, `$question8`, `$question9`, `$question10`, `$question11`, `$question12`, `$question13`, `$question14`, `$question15`)")) {
  printf("Error message: %s\n", $mysqli->error);
}

For INSERT statements, $mysqli->query statements will return TRUE if the statement is successful, FALSE otherwise. The above should give you an indication as to what went wrong.


Edit:

You're using the incorrect identifiers for your VALUES, being ticks and should be regular single quotes instead.

Change it to:

if (!$mysqli->query("INSERT INTO `applications` (`question1`, `question2`, `question3`, `question4`, `question5`, `question6`, `quesion7`, `question8`, `question9`, `question10`, `question11`, `question12`, `question13`, `question14`, `question15`) VALUES ('$question1', '$question2', '$question3', '$question4', '$question5', '$question6', '$question7', '$question8', '$question9', '$question10', '$question11', '$question12', '$question13', '$question14', '$question15')")) {
  printf("Error message: %s\n", $mysqli->error);
}

N.B.:

Plus, you may have mispelled your "quesion7" column, which should most likely read as "question7".

It should be modified if that is the case, but wasn't made in the code above.


Important note:

Community
  • 1
  • 1
-1

In your code you added quotes for database field names i think this was problem

$mysqli->query("INSERT INTO applications (question1, question2, question3, question4, question5, question6, quesion7, question8, question9, question10,question11, question12, question13, question14, question15) VALUES (`$question1`, `$question2`, `$question3`, `$question4`, `$question5`, `$question6`, `$question7`, `$question8`, `$question9`, `$question10`, `$question11`, `$question12`, `$question13`, `$question14`, `$question15`)");

try out this. i have doubt in form action u passing segments by "/" seprated is there this support in core php actually we pass only query strings only in some frameworks we use "/"

  • If you try to use `` Tick in values, you get SQL -> INSERT INTO articles (id,a) values(102,test); And Error `Unknown column '102' in 'field list'` . Please modify answer. Where is ` – Pratik Joshi Feb 10 '15 at 04:47