插入查询不起作用

时间:2016-06-15 19:42:00

标签: php mysql

这是我的插入功能。数据库连接成功并且参数也已成功接收,但未在表中插入值。

 public function insert($owner_fname,$owner_lname,$owner_phone,$owner_email,$owner_age,$owner_city,$owner_address,$owner_password,$owner_confirm_password,$owner_gender)
    {
        $con=new DB();
        $var=$con->connect();
        echo $owner_fname;
        echo $owner_lname;
        echo $owner_phone;
        echo $owner_email;
        echo $owner_age;
        echo $owner_city;
        echo $owner_address;
        echo $owner_password;
        echo $owner_confirm_password;
        echo $owner_gender;
      $res = mysql_query("INSERT INTO owner(o_name,o_last_name,o_phone,o_email,o_age,o_city,o_address,o_password,o_confirm_password,o_gender) VALUES('$owner_fname','$owner_lname','$owner_phone','$owner_email','$owner_age','$owner_city','$owner_address','$owner_password','$owner_confirm_password','$owner_gender')");
      return $res;
    }

2 个答案:

答案 0 :(得分:0)

您应该使用MySqli或Prepared Statements而不是Mysql ,并且您错过了连接参数。使用这样的东西:

SELECT name FROM table_name WHERE first_name = 'Roy' AND roll_number = 5;

其中: 在您的情况下$连接将是$ var 而$ string_query将是' Select * From table'或者' Inserto Into table()values()'

希望我帮助

答案 1 :(得分:0)

public function insert($owner_fname,$owner_lname,$owner_phone,$owner_email,$owner_age,$owner_city,$owner_address,$owner_password,$owner_confirm_password,$owner_gender)
{
    // $con=new DB();
    // $con->connect();
    echo $owner_fname;
    echo $owner_lname;
    echo $owner_phone;
    echo $owner_email;
    echo $owner_age;
    echo $owner_city;
    echo $owner_address;
    echo $owner_password;
    echo $owner_confirm_password;
    echo $owner_gender;
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "orcs";
    $conn = new mysqli($servername, $username, $password, $dbname);
  $sql= "INSERT INTO owner(o_name,o_last_name,o_phone,o_email,o_age,o_city,o_address,o_password,o_confirm_password,o_gender) VALUES('$owner_fname','$owner_lname','$owner_phone','$owner_email','$owner_age','$owner_city','$owner_address','$owner_password','$owner_confirm_password','$owner_gender')";

  if ($conn->query($sql) === TRUE) {
        $last_id = $conn->insert_id;
        echo "New record created successfully.";
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }

    $conn->close();

}

这很好用