0

I have a simple <ul> where you can add a list item with an input field:

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

My question is: Is it possible in a way to save this created list on a mySQL database on a webserver? I haven't got much experience in PHP or other languages for server sided storage. And if it's possible, can you tell me any links where it's explained how? I spent some time already with searching, but I didn't find anything because I simply don't know what to search after.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Tobias Glaus
  • 3,008
  • 3
  • 18
  • 37
  • didn't you post something similar earlier? http://stackoverflow.com/q/42342246/1415724 – Funk Forty Niner Feb 21 '17 at 21:12
  • Yes. After that I spent some time with searching but I didn't find anything similar to my issue. – Tobias Glaus Feb 21 '17 at 21:13
  • Well Tobias, I have to admit that the answers given in there weren't much help to you and weren't all that good, IMHO. Had you known ahead of time, you could have tagged it as "mysql" and "database", least you would have more than likely been given a few pointers/links as to where to go and start with. – Funk Forty Niner Feb 21 '17 at 21:19
  • Thanks for your advice. I added tags to both of my questions now. – Tobias Glaus Feb 21 '17 at 21:22
  • You're welcome Tobias. To tell you the truth, I'm not your guy for the JS stuff, I'm a 99% server-side coder. Best I can offer are these Q&A's I found on Google leading back to Stack http://stackoverflow.com/q/20769364/1415724 --- http://stackoverflow.com/q/33986206/1415724 --- I hope that helps. Maybe someone else will come along and offer another helping hand. – Funk Forty Niner Feb 21 '17 at 21:28
  • @Fred-ii- Isn't server sided code right what i need? ;) – Tobias Glaus Feb 21 '17 at 21:30
  • Yes, but since you posted jquery code, is what I based my search on. If you want pure serverside, then you'll need to use a form with named inputs with either a POST/GET method for the form and insert in db from there. Here are a few more links you can look at http://php.net/manual/en/tutorial.forms.php --- http://php.net/manual/en/function.mysqli-connect.php --- http://php.net/manual/en/mysqli.quickstart.prepared-statements.php --- https://dev.mysql.com/doc/refman/5.7/en/creating-tables.html --- https://dev.mysql.com/doc/refman/5.7/en/insert.html - There isn't much else I can for you here. – Funk Forty Niner Feb 21 '17 at 21:34
  • I got a form on my page. There is a text input and a submit button. When I click the submit button the JS function gets executed. – Tobias Glaus Feb 21 '17 at 21:38
  • you want to select or insert? that answer below uses `select` – Funk Forty Niner Feb 21 '17 at 22:03
  • Saving the list to a database, i guess that's insert, but connecting to the database (like in the answer below) isn't that bad is it – Tobias Glaus Feb 21 '17 at 22:08

1 Answers1

0

Here is a simple php script that I wrote to connect to my MySQL DB, and retrieve along with display each result in a given column.

Just replace: Hostname with probably your local IP if MySQL is installed on your local machine. Your MySQL username, probably root if you haven't created another user. The password, which could be blank if you didn't create one. Database with the name of your Database.

<?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>'; 
} 
?>
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Peavey2787
  • 128
  • 1
  • 10
  • Thanks for your answer! I created a mySQL on my hosting. There I got a name, username, password and host, so that should work :) This is just for connecting right? And after that the code for saving the list should get somewhere in there right? – Tobias Glaus Feb 21 '17 at 21:47
  • Ya, I was just trying to show you an easy example to connect and see whats in the DB. Once you got that working, just change the SQL query to be an INSERT query and then you can save your lists. – Peavey2787 Feb 21 '17 at 21:50
  • this only select's. The guy wants to insert. – Funk Forty Niner Feb 21 '17 at 21:50
  • I got that script on my FTP now. How do I check if it works or not? – Tobias Glaus Feb 21 '17 at 21:55
  • You should just be able to navigate to that file. www.yourwebsite.com/thisphpfile.php and it should load. – Peavey2787 Feb 21 '17 at 21:56
  • Do you have a table created with data in it, and for $row["Column1"] did you change it to the real column name? – Peavey2787 Feb 21 '17 at 22:04
  • No, I haven't created any tabled yet. And what do you mean with "the real column name"? – Tobias Glaus Feb 21 '17 at 22:10
  • First you need to create a table to hold the data. So you're going to need to know what to save and what to name everything so its easy to call it in your code. w3schools has some pretty short and sweet examples for SQL statements. – Peavey2787 Feb 21 '17 at 22:13
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/136292/discussion-between-tobias-glaus-and-peavey2787). – Tobias Glaus Feb 21 '17 at 22:24