如何存储由用户服务器端(mySQL)创建的<li> -item

时间:2017-02-21 21:10:10

标签: php mysql database

我有一个简单的<ul>,你可以在其中添加一个带有输入字段的列表项:

$('#plannerform').submit(function(e) {
        var val = $(this).find('#plannername').val();
        $('ul.plannerlist:visible').append('<li>' + val + '</li>');
        e.preventDefault();
});

我的问题是:是否有可能以某种方式将此创建的列表保存在Web服务器上的mySQL数据库中?我没有太多的PHP或其他语言的服务器端存储经验。如果有可能,你能告诉我任何链接,它解释如何?我已经花了一些时间进行搜索,但我找不到任何东西,因为我根本不知道要搜索什么。

1 个答案:

答案 0 :(得分:0)

这是一个简单的PHP脚本,我写这个脚本连接到我的MySQL数据库,并检索显示给定列中的每个结果。

只需更换: 如果本地计算机上安装了MySQL,则可能是您的本地IP的主机名。 你的MySQL用户名,如果你还没有创建另一个用户,可能是root用户。 密码,如果您没有创建密码,则可以为空。 具有数据库名称的数据库。

<?php 

//Connect to DB 
$conn = new mysqli("Hostname","Username","Password","Database"); 

//If the connection has errors 
if ($conn->connect_error){ 
   //Display the error 
   die("Connection failed because: " . $conn->connect_error); 
} 

//Otherwise the connection is good so lets create a sql query 
$sql = "SELECT * FROM Database"; 

//Get the results of the query 
$result = $conn->query($sql); 

//If the results have rows 
if($result->num_rows > 0){ 

    //While there are more rows in the results 
    while($row = $result->fetch_assoc()) { 

    //Display in HTML paragraph form: Column1 = DataInColumn1 
    echo '<p> Column1 = ' . $row["Column1"] . ' </p>'; 

  }//End While
}//End If

//Otherwise the query had no results 
else{ 
  echo '<p>No results found!</p>'; 
} 
?>