Mysql
 sql >> Base de données >  >> RDS >> Mysql

L'insert Mysql ne fonctionne pas et ne donne pas d'erreurs

Veuillez essayer de ne pas utiliser mysql_connect utilisez plutôt mysqli_connect ou PDO_MySQL lire ceci

Utilisez également die pour savoir s'il y a des erreurs dans votre code

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}

Sinon (méthode recommandée)-

Style procédural

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

$sql = "INSERT INTO Persons (firstname, lastname, email)
VALUES ('Happy', 'John', '[email protected]le.com')";

if (mysqli_query($conn, $sql)) {
    echo "New Person created successfully";
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}

mysqli_close($conn);

Style orienté objet MySQL

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "INSERT INTO Persons (firstname, lastname, email)
VALUES ('Happy', 'John', '[email protected]')";

if ($conn->query($sql) === TRUE) {
    echo "New Person created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();