1

Im having problem make my php code to make changes in MySQL Database. This is my code: ** The star is username and password

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

I am able to communicate with the MySQL Server, With the check connection conditions, When I input wrong passwd it gives me error, and provide success when I connected.

I am also able to modify my database with MySQL Workbench, but I can't do it with PHP Code. Is there a way to Put values in the database.\

  1. Is my PHP Code Wrong?
  2. Is my Netbeans unable to connect to the MySQL?
  3. Is there a problem between PHP and MySQL in the login and condition checking?

Thanks anyway.

  • which type of error give that.? – Mehul Jariwala Jul 27 '17 at 17:25
  • can you send me error number.? – Mehul Jariwala Jul 27 '17 at 17:25
  • The Query, Im unable to add my database straight from the PHP and need the help of MySQL Workbench or Phpmyadmin. So my bet would be wrong query? –  Jul 27 '17 at 17:27
  • here we used phpmyadmin.? – Mehul Jariwala Jul 27 '17 at 17:29
  • I used it as example, now im unable to edit the database with my query given. Any help? –  Jul 27 '17 at 17:31
  • you just need to insert data into database right.? – Mehul Jariwala Jul 27 '17 at 17:32
  • After that insert query, can you add `echo mysqli_error($con);` to see if it might provide a clue? It's strange that you're trying to put 33 to the name, but there might be other columns that may need other values, I'm guessing. – Paul T. Jul 27 '17 at 17:32
  • @ Paul T. I used your `echo mysqli_error($con)` it didn't show anything just connected successfully and btw I used the 33 as name cause its random, I put all the type is varchar(255) in the table. –  Jul 27 '17 at 17:36
  • @MehulJariwala Yes, but I need to do it from php so that I can connected my website from forms. –  Jul 27 '17 at 17:37
  • Note: The object-oriented interface to `mysqli` is significantly less verbose, making code easier to read and audit, and is not easily confused with the obsolete `mysql_query` interface. Before you get too invested in the procedural style it’s worth switching over. Example: `$db = new mysqli(…)` and `$db->prepare("…")` – tadman Jul 27 '17 at 17:37
  • You'll also want to [enable exceptions](https://stackoverflow.com/questions/14578243/turning-query-errors-to-exceptions-in-mysqli) so that you don't overlook any important errors by mistake. You're not testing the result of `mysqli_query` here so anything could be going wrong and you'd have no idea. – tadman Jul 27 '17 at 17:38
  • @JoesLie: Try the exact same query in phpMyAdmin, without the surrounding double quotes, and see what you might get. – Paul T. Jul 27 '17 at 17:48
  • you can check the localhost and then fire query – Mehul Jariwala Jul 27 '17 at 17:58

2 Answers2

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)");
}
?>
Mehul Jariwala
  • 886
  • 1
  • 9
  • 19
0

Without knowing the return of mysqli_connect_error(), we can't give an apropriate answer.

By the way, I suggest you use PDO to connect to MySQL. Here is the code based on the data you provided:

<?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.';
}

?>
Lawrence
  • 309
  • 7
  • 18