Cum sa introducem valori intr-o baza de date mysql folosind php

Configurare noua (How To)

Situatie

Cum sa introducem valori intr-o baza de date mysql folosind php

Solutie

Pasi de urmat
  • Definim datele de conexiune si realizam conexiunea

 

$servername = “localhost”;
$username = “username”;
$password = “password”;
$dbname = “testdb”;

// realizaeaza conexiunea
$con = new mysqli($servername, $username, $password, $testdb);
// verifica conexiunea
if ($con->connect_error) {
    die(“Conexiune nereusita: ” . $con->connect_error);
}

  • In pasul urmator vom popula baza de date mysql cu valorile dorite:

$sql = “INSERT INTO tabeltest (nume, prenume, email)
VALUES (‘John’, ‘Lennon’, ‘john@lennon.com’)”;
Explicatie :

Nume, prenume, email sunt campurile definite in tabelul nostrum, iar John, Lennon, john@lennon.com sunt valorile ce dorim sa le introducem.

Pentru a afisa evetualele erori, adaugam urmatorul cod:
if ($con->query($sql) === TRUE) {
    echo “Date adaugate cu succes”;
} else {
    echo “Error: ” . $sql . “<br>” . $con->error;
}
$con->close();

Full code:

 

<?php

$servername = “localhost”;
$username = “username”;
$password = “password”;
$dbname = “testdb”;

// realizaeaza conexiunea
$con = new mysqli($servername, $username, $password, $testdb);
// verifica conexiunea
if ($con->connect_error) {
    die(“Conexiune nereusita: ” . $con->connect_error);
}

$sql = “INSERT INTO tabeltest (nume, prenume, email)
VALUES (‘John’, ‘Lennon’, ‘john@lennon.com’)”;

if ($con->query($sql) === TRUE) {
    echo “Date adaugate cu succes”;
} else {
    echo “Error: ” . $sql . “<br>” . $con->error;
}
$con->close();

?>

Tip solutie

Permanent

Voteaza

(7 out of 11 people found this article helpful)

Despre Autor

Leave A Comment?