PHP not saving data to database

纵饮孤独 提交于 2019-12-01 14:04:45

Try this out: (your present code did not work for me) HTML form and PHP/SQL are all-in-one.

<?php
DEFINE ('DB_USER', 'xxx');
DEFINE ('DB_PASSWORD', 'xxx');  
DEFINE ('DB_HOST', 'xxx');
DEFINE ('DB_NAME', 'xxx');

$link = @mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) 
OR die("could not connect");

if(isset($_POST['submit'])){

    // stored in a variable to shorten
    $value = mysqli_real_escape_string($link,$_POST['input1']);

    // stored in a variable to TEST
    $sql = "INSERT INTO demo (input1) VALUES ('$value')"; 

    if(!mysqli_query($link, $sql)) {
        die('Error: ' . mysqli_error($link));
    }

    else { echo "Success"; }

} // if(isset($_POST['submit']))

    mysqli_close($link);
?>

<form action="" method="post" />
<p>Input 1: <input type="text" name="input1" /></p>
<input type="submit" name="submit" value="Submit" />
</form>

MySQL used port 3306 as the default. Does PHP connect directly to the database? If yes, try making your port match.

Did you add permissions to MySQL to allow your app to connect and interact with the database? You should read about GRANT and permissions.

But the comment by Dagon above is a serious one: exposting a database directly to the Internet should only be done if you're willing to have the data stolen, trashed, or both.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!