-2

ok so I can connect and view the database with my php code, however I can not insert data into it.here is the query I tested with phpmyadmin which was able insert new data into my table

INSERT INTO `members` ( `id` , `username` , `email` ) 
VALUES ( 123456789, 'russi', 'baka@dog.com' )

then I tried to put it into my actual php file

<?php
$servername = "localhost";
$username = "root";
$password = "blablabla";
$dbname = "test_database";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO 'members' ('id', 'username', 'email')
VALUES (2339978, 'vladtheimpalor', 'vladtheimaplor@bloody.com')";
$sql = "SELECT id, username, email FROM members";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "id: " . $row["id"]. " - username: " . $row["username"]. " -email:" . $row["email"]. "<br>";
    }
} else {
    echo "0 results";
}
$conn->close();
?>

so select function works but insert does not.

Adrian Cid Almaguer
  • 7,815
  • 13
  • 41
  • 63
alkokarko
  • 85
  • 2
  • 8
  • 1
    Your INSERT query is immediately overwritten by a SELECT query and never executed. –  May 02 '15 at 21:14

5 Answers5

2

You are overriding your $sql variable w/o executing it. Besides that you should not use single quotes for columns, but backticks (see When to use single quotes, double quotes, and backticks in MySQL)

Change

$sql = "INSERT INTO 'members' ('id', 'username', 'email')
VALUES (2339978, 'vladtheimpalor', 'vladtheimaplor@bloody.com')";
$sql = "SELECT id, username, email FROM members";
$result = $conn->query($sql);

to

$sql = "INSERT INTO `members` (`id`, `username`, `email`)
VALUES (2339978, 'vladtheimpalor', 'vladtheimaplor@bloody.com')";
$result = $conn->query($sql);
$sql = "SELECT id, username, email FROM members";
$result = $conn->query($sql);
Community
  • 1
  • 1
MrTux
  • 32,350
  • 30
  • 109
  • 146
1

Change your insert to:

$sql = "INSERT INTO members (id, username, email)
VALUES (2339978, 'vladtheimpalor', 'vladtheimaplor@bloody.com')";

And call your query:

$sql = "INSERT INTO members (id, username, email)
VALUES (2339978, 'vladtheimpalor', 'vladtheimaplor@bloody.com')";
//Here, you never execute your query
$result = $conn->query($sql);

$sql = "SELECT id, username, email FROM members";
$result = $conn->query($sql);
Adrian Cid Almaguer
  • 7,815
  • 13
  • 41
  • 63
1

Of course it did not work !

You never execute your INSERT...

<?php
...
$sql = "INSERT INTO 'members' ('id', 'username', 'email')
VALUES (2339978, 'vladtheimpalor', 'vladtheimaplor@bloody.com')";
$conn->exec($sql);
$sql = "SELECT id, username, email FROM members";
$result = $conn->query($sql);
...

:)

roillion
  • 23
  • 5
  • There is no `exec` method, is there? (https://php.net/manual/de/class.mysqli.php) also the single quotes are wrong. – MrTux May 02 '15 at 21:23
0

remove the single quotes around your column and table names:

$sql = "INSERT INTO members (id, username, email)
VALUES (2339978, 'vladtheimpalor', 'vladtheimaplor@bloody.com')";

single quotes are only used for char fields.

Also you never execute the insert Statement because you overwrite it.

Jens
  • 67,715
  • 15
  • 98
  • 113
0
$sql = "INSERT INTO 'members' ('id', 'username', 'email')
VALUES (2339978, 'vladtheimpalor', 'vladtheimaplor@bloody.com')";
$sql = "SELECT id, username, email FROM members";
//missing the grave accent 
$sql = "INSERT INTO `members` (id, username, email)
VALUES (2339978, 'vladtheimpalor', 'vladtheimaplor@bloody.com')";  

$sql = "SELECT `id`, `username`, `email` FROM `members`";

/* This is the corrrected code */