-2

Im new in MySql and PHP and im trying to make a CRUD but everytime i try to insert data into table called "studenti" i get the error that i didnt select a database but i selected a database with mysqli_select_db($con, "d_base");

Somebody please help me cuz i dont understand why its not workin'

Here is the code;

$id = $_POST['ID'];
$nota = $_POST['Nota'];
$emri = $_POST['Emri'];
$mbiemri = $_POST['Mbiemri'];



$servername = "localhost";
$dbname = "d_base";

// 1.Create connection
$con = mysqli_connect("localhost","d_base");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

  if (!mysqli_query($con,"INSERT INTO studenti (id, nota, emri, mbiemri) VALUES ('$id', '$nota','$emri','$mbiemri')"))
  {
  echo("Error description: " . mysqli_error($con));
  }

// Perform queries 
mysqli_select_db($con, "d_base");
mysqli_query($con,"INSERT INTO studenti (id, nota, emri, mbiemri) VALUES ('$id', '$nota','$emri','$mbiemri')");
mysqli_close($con);
  • You select it *after* your first query – John Conde Apr 04 '17 at 18:02
  • 2
    Your code is vulnerable to SQL injection attacks. You should use [mysqli](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) or [PDO](http://php.net/manual/en/pdo.prepared-statements.php) prepared statements as described in [this post](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php). – Alex Howansky Apr 04 '17 at 18:02
  • It has to be selected up after you connect, not below – clearshot66 Apr 04 '17 at 18:02
  • i selected after the connection but i still get the same error – Bishudkishu Apr 04 '17 at 18:03
  • Is your database user also d_base? The format for mysqli_connect is `mysqli_connect("db_host", "db_user", "db_password", "db_name");` – aynber Apr 04 '17 at 18:04
  • @AlexHowansky he is using mysqli ;) just saying.. But I agree to what you said.. – Shakti Phartiyal Apr 04 '17 at 18:07
  • @ShaktiPhartiyal My comment was not simply "use mysqli" but "use mysqli prepared statements". Note the link targets. – Alex Howansky Apr 04 '17 at 18:13
  • yeah; real nice sql injection there. You're not planning on taking this to a live website, *are you?* If you want to keep your website/db intact, then do what @AlexHowansky suggested and read those documents till you know them next to "by heart". – Funk Forty Niner Apr 04 '17 at 18:16
  • First you need to connect to your database then you can use querys. `$con = mysqli_connect("host", "database username", "database password", "database name");` and now you have connection and selected database. – Mario Apr 04 '17 at 18:16
  • also; what are the POST's origins? Someone gave you an answer below, yet you state it still doesn't work. Ever thought that those may be failing here, or what's being passed through them? Maybe something isn't or mysql is complaining about something but you're not checking for the errors at all. Edit: Oh, you are, you just didn't tell us what those were. – Funk Forty Niner Apr 04 '17 at 18:17
  • `(id, nota, emri, mbiemri VALUES` that's a syntax error right there. – Funk Forty Niner Apr 04 '17 at 18:18
  • @Fred-ii- no im not gonna publish this it's just for learning, thanks for the syntax error i corrected it, but still i get the error – Bishudkishu Apr 04 '17 at 18:24
  • Bishud Please edit the question if you have corrected the answer as suggested by @Fred-ii- – Shakti Phartiyal Apr 04 '17 at 18:30

1 Answers1

0

Before all that if you are a begginer go straight on PDO or use mysqli with prepared statements its safer.

Here is example how your php and html form must look like and work.

First you must check if submit button is pressed, if its pressed read values form form $_POST variables.

Second thing you must escape injection to your mysql by using function mysqli_real_escape_string().

After that try to insert query and check for error, if there is no error query will be inserted successfully.

PHP code

<?php

// set error report ; 1 = on | 0 = off
error_reporting(1);

$db_host = "localhost"; // host
$db_user = "root";      // database username
$db_pass = "";          // database password
$db_name = "d_base";    // database name

// 1.Create connection
$con = mysqli_connect($db_host, $db_user, $db_pass, $db_name);

// Check connection
if (mysqli_connect_errno())
{
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

// if form is submited
if (isset($_POST['submit']))
{
    // escape post variables
    $id = mysqli_real_escape_string($con, $_POST['ID']);
    $nota = mysqli_real_escape_string($con, $_POST['Nota']);
    $emri = mysqli_real_escape_string($con, $_POST['Emri']);
    $mbiemri = mysqli_real_escape_string($con, $_POST['Mbiemri']);

    // make query
    $query = mysqli_query($con, "INSERT INTO studenti (id, nota, emri, mbiemri VALUES ('$id', '$nota', '$emri', '$mbiemri')")

    // check for query
    if (!$query)
    {
        echo "Error description: " . mysqli_error($con);
    }
    else
    {
        echo "Query inserted.";
    }

    // close connenction
    mysqli_close($con);
}


?>

<form action="" method="post">
   <input type="text" name="ID" placeholder="Id"><br />
   <input type="text" name="Nota" placeholder="Nota"><br />
   <input type="text" name="Emri" placeholder="Emri"><br />
   <input type="text" name="Mbiemri" placeholder="Mbiemri"><br />
   <input type="submit" name="submit" value="Submit form">
</form>
Mario
  • 518
  • 2
  • 19
  • thank u for the explanation , i just started a few days ago with MySql and PHP i'v tried this but now i get another error Failed to connect to MySQL: Access denied for user 'd_base'@'localhost' (using password: YES) – Bishudkishu Apr 04 '17 at 19:07
  • @Bishud probably you are using wrong data for connection to database. You can check yours database user and password in phpmyadmin under **user accounts** tab, here you can see on this image http://image.prntscr.com/image/361edc80d14b4194aa35f91919375508.jpeg – Mario Apr 04 '17 at 19:23
  • This is the same thing that I see when I open the user accounts – Bishudkishu Apr 04 '17 at 21:43
  • @Bishud what is name of yours database and table / column, can you make a image ? – Mario Apr 04 '17 at 21:48
  • @Bishud then it must work i will update my code. Put all in 1 script and call it test.php, then run in browser http://localhost/test.php and try it – Mario Apr 04 '17 at 22:16
  • This is confusing because i still get the same error. ---- Failed to connect to MySQL: Access denied for user 'd_base'@'localhost' (using password: YES) – Bishudkishu Apr 04 '17 at 22:37
  • Try to reinstall xampp or wamp – Mario Apr 04 '17 at 23:01
  • thank u for your help, i just created a new user and its working now. – Bishudkishu Apr 05 '17 at 14:24
  • @Bishud no problem, i'm glad that you solved problem. – Mario Apr 05 '17 at 14:32