How to insert data into mysql-database

Hi,
I want insert data(1,1,1,1) to mysql(open,heat,up,down) by php,here is my php

<?php
$servername = "localhost";
$username = "idxxxxxxx";
$password = "xxxxxxx";
$dbname = "idxxxxxxxx";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
$insert = "INSERT INTO idxxxxxxxx.data(open,heat,up,down)value('1','1','1','1')" or die(mysql_error());
mysql_query($insert);
?>

but it always show

This page does not work
xxxxxxx.000webhostapp.com is currently unable to process this request.
HTTP ERROR 500

how can I modify my php to insert data into mysql-database

Try


<?php
$servername = "localhost";
$username = "xxxx";
$password = "xxxx";
$dbname = "xxxx";

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $sql = "INSERT INTO data (open, heat, up, down)
    VALUES ('1', '1', '1', '1')";
    // use exec() because no results are returned
    $conn->exec($sql);
    echo "New record created successfully";
    }
catch(PDOException $e)
    {
    echo $sql . "<br>" . $e->getMessage();
    }

$conn = null;
?>

1 Like

it can work,thanks

work

1 Like