PHP无法在MySQL数据库上插入值

时间:2017-07-27 17:20:06

标签: php mysql netbeans

我有问题使我的PHP代码在MySQL数据库中进行更改。 这是我的代码: **明星是用户名和密码

<?php
$con=mysqli_connect("192.168.1.98","*****","*****","test");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
echo "Connected successfully";

//Query
mysqli_query($con,"INSERT INTO users (email, password, name) 
VALUES ('Glenn','Quagmire',33)");
?>

我能够与MySQL服务器通信,通过检查连接条件,当我输入错误的passwd时,它会给我错误,并在我连接时提供成功。

我也可以使用MySQL Workbench修改我的数据库,但是我不能用PHP代码来修改它。有没有办法将值放在数据库中。\

  1. 我的PHP代码错了吗?
  2. 我的Netbeans无法连接到MySQL吗?
  3. 在登录和条件检查中PHP和MySQL之间是否存在问题?
  4. 非常感谢。

2 个答案:

答案 0 :(得分:0)

Not sure about your problem but try this it will help you..!
<?php
$server   = "192.168.1.98";
$database = "test";
$username = "root";
$password = "password";

$mysqlConnection = mysqli_connect($server,$username,$password,$database);
if (!$mysqlConnection)
{
  echo "Please try later.";
}
else
{
 mysql_select_db($database, $mysqlConnection);
 mysqli_query($mysqlConnection ,"INSERT INTO users (email,password,Age) 
 VALUES ('Glenn','Quagmire',33)");
}
?>

答案 1 :(得分:0)

在不知道mysqli_connect_error()返回的情况下,我们无法给出适当的答案。

顺便说一句,我建议您使用PDO连接到MySQL。以下是基于您提供的数据的代码:

<?php

try {
    $PDO = new PDO("mysql:host=127.0.0.1;port=3306;dbname=yourdatabase", 'youruser', 'yourpassword');
    echo 'Connected successfully!';
} catch (PDOException $e) {
    echo 'Failed to connect to MySQL: ' . $e->getMessage();
    die();
}

//Query
$sql = "INSERT INTO users (email, password, name) VALUES (:email, :password, :name)";
$params = array('email' => 'Glenn', 'password' => 'Quagmire', 'name' => 33);

$query = $PDO->prepare($sql);
$run = $query->execute($params);
if (!$run) {
    $errorInfo = $query->errorInfo();
    $errorMessage = $query->$errorInfo[2];
    echo $errorMessage;
} else {
    echo 'Insert executed.';
}

?>