Matthieu McLaren
Matthieu McLaren

Reputation: 224

Issue inserting data into a database

I run this function and the value of the post form doesn't insert into the DB. I'm not getting any error messages but I checked and saw that $this->user_id is populating. Am I missing something else?

function senddata () {
    $con = mysql_connect("localhost","root","XXXXX");
    if (!$con)
        {
    die('Could not connect: ' . mysql_error());
        }
        mysql_select_db("user", $con);
        $sql="INSERT INTO profile (collegemajor) VALUE('$_POST[major]') WHERE userid=$this->user_id";

    mysql_close($con);

    }

Upvotes: 0

Views: 46

Answers (3)

Ben D
Ben D

Reputation: 14479

This should be an update function, it seems, rather than an insert function (if you're trying to update a row that already exists, which is seems you are based on the "Where" clause).

And, as others have mentioned, you never ran mysql_query() or similar function.

Upvotes: 0

Michael Sazonov
Michael Sazonov

Reputation: 1533

First of all, I don't see the mysql_query() - the function to execute the query.

It seems like the problem is here:

$sql="INSERT INTO profile (collegemajor) VALUE('$_POST[major]') WHERE userid=$this->user_id";

Use $sql="INSERT INTO profile (collegemajor) VALUE('$_POST[major]'); instead.

The code:

    function senddata () {
        $con = mysql_connect("localhost","root","XXXXX");
        if (!$con)
            {
        die('Could not connect: ' . mysql_error());
            }
            mysql_select_db("user", $con);
            $sql="INSERT INTO profile (collegemajor) VALUE('$_POST[major]');";
        mysql_query( $sql , $con );

        mysql_close($con);

        } 

Upvotes: 0

piotrekkr
piotrekkr

Reputation: 3181

  1. INSERT statement don't have WHERE clausule,
  2. you need to send query!!!

Upvotes: 1

Related Questions