1

I've been searching for some answers but can't figure out what's happening.

The SQL connection is working perfectly, I've checked it. So I have this POST method.

  if(isset($_POST['new_data'])) {
    $new_nm = mysqli_real_escape_string($db, $_REQUEST['new_name']);
    $new_pstn = mysqli_real_escape_string($db, $_REQUEST['new_position']);
    ...
        $db->query("INSERT INTO data (name, position, description, twitter, email, image) VALUES('$new_nm', '$new_pstn',
            '$new_dscrpt', '$new_twt', '$new_mail', '$new_img')");
    }

And here's the form from where I'm getting the data:

<form role="form" method="post">
    <label>Name</label>
        <input name="new_name" class="form-control" />
    <label>Position</label>
        <input name="new_position" class="form-control" />
    ...
    <button type="submit" name="new_data">Submit!</button>
</form> 

When I click in the button I don't get the data inserted. What am I doing wrong? I have another POST method with a different name which is working (that one makes an UPDATE).

Thanks a lot.

8
  • 1
    Could you provide structure of your table? Commented Jun 18, 2017 at 4:08
  • Why don't you try adding: echo "INSERT INTO data (name, position, description, twitter, email, image) VALUES('$new_nm', '$new_pstn', '$new_dscrpt', '$new_twt', '$new_mail', '$new_img')"; to see if you get a properly formatted result with the variables filled in. That usually helps my troubleshoot. Commented Jun 18, 2017 at 4:11
  • @Rulisp id ENUM('') NOT NULL PRIMARY KEY, name text NOT NULL, position text NOT NULL, description text NOT NULL, twitter text, email varchar(60), image varchar(300) NOT NULL Commented Jun 18, 2017 at 4:11
  • @Keith just did that and the introduced values are ok! they just not get inserted :( Commented Jun 18, 2017 at 4:15
  • 1
    @kadota I'm not sure, but I think that problem is in your id field. Convert it to int and may be add it AUTO_INCREMENT option Commented Jun 18, 2017 at 4:20

1 Answer 1

2

Well, I'll try to answer this question.

According to table structure, problem is in id field, which should be int with AUTO_INCREMENT.

Also, I want to make some suggestion for your input

if(isset($_POST['new_data'])) 
{
//Always check your input and sanitize it with htmlspecialchars() or htmlentities()
    $new_nm = htmlspecialchars(isset($_REQUEST['new_name']) ? $_REQUEST['new_name'] : "");
    $new_nm = mysqli_real_escape_string($db, $new_nm);

    ...
    $db->query("INSERT INTO data (name, position, description, twitter, email, image) VALUES('$new_nm', '$new_pstn',
            '$new_dscrpt', '$new_twt', '$new_mail', '$new_img')");
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.