简体   繁体   English

将数据插入数据库时​​出现问题

[英]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. 我没有收到任何错误消息,但是我检查了一下,发现$ this-> user_id正在填充。 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);

    }
  1. INSERT statement don't have WHERE clausule, INSERT语句没有WHERE子句,
  2. you need to send query!!! 您需要发送查询!!!

First of all, I don't see the mysql_query() - the function to execute the query. 首先,我看不到mysql_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. 使用$sql="INSERT INTO profile (collegemajor) VALUE('$_POST[major]');代替。

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);

        } 

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). 看来,这应该是一个更新函数,而不是插入函数(如果您要更新已经存在的行,这似乎是基于“ Where”子句的)。

And, as others have mentioned, you never ran mysql_query() or similar function. 而且,正如其他人提到的那样,您永远不会运行mysql_query()或类似函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM