Laravel 5 belongsToMany() by Using Pivot Table Example

Relationship using pivot table in Laravel 5? A method, belongsToMany(), is a part of Eloquent ORM in Laravel 5. It connects between tables and it has many to many relationship. In order to use it, you must have two models three tables.

Continue reading Laravel 5 belongsToMany() by Using Pivot Table Example

How to Put Date from MySQL to Select HTML Tag with PHP

You may want to create editable data using PHP and HTML. You want to take your date type value to your <select> HTML tag. It is not difficult, but yes we need to be patient to code it.

First, you should call a query to your MySQL such as:

<?php
$sqlquery=mysql_query("select * from income where transaction_id='".$_GET['transaction']."'") or die(mysql_error());
$row = mysql_fetch_array($sqlquery);
?>

Second, you have to set the type of your date in your table to DATE type. Don’t set it to VARCHAR or INTEGER. Let your MySQL database rules what it’s programmed to be. You just need to explode, separate, and put it into different <select> tag, as follow:

Continue reading How to Put Date from MySQL to Select HTML Tag with PHP

How to Store Date from Select HTML Tag to MySQL

Storing a date from <select> HTML tag to MySQL database is not that easy. I spent three hours to find out how. First time I tried to, I was thinking that my script was not effective. But, after reading some tutorial I found my own way to do it. Here it is.

MySQL script

Put this script at the top of your PHP file because it needs to store data first and then execute the script after.

<?php
if(isset($_POST['submit'])):
  $thedate=$_POST['dateyear']."-".$_POST['datemonth']."-".$_POST['dateday'];
  mysql_query("insert into income values('','".$thedate."')");
endif;
?>

Continue reading How to Store Date from Select HTML Tag to MySQL

WordPress “Error establishing a database connection” Checklist

If it is your first time to install WordPress, you can get annoying Error establishing a database connection. There must be some reasons why this annoying error message appears. Here are the checklist that may help you out:

  • Check that your database is correct. When you create a database manually in cPanel. You must be enter the name such as “newsite”, “mysite”, or else. But, when it’s done. It becomes yourusername_databasename. If your username in your webhosting is “becca” and your database name “newsite”, your correct database name is “becca_newsite” NOT “newsite”.
  • Don’t forget to add user to your MySQL database! This can be hideous, so please treat it carefully.
  • Set the right database password when installing WordPress. Where can you get it? It is when you create MySQL user. If you are using fantastico, the password will automatically generated.
  • Make sure the database is online by seeing server statusĀ at the left bottom corner in cPanel.

If you are sure you have completed the checklist and still error, you should contact your webhosting provider.

How to Order MySQL Data Ascending and Descending Based on Field

The syntax is easy to remember.

SELECT * FROM <table> ORDER BY <field> <ASC/DESC>

Example:

  1. mysql_query("SELECT * FROM groceries ORDER BY shopname ASC")
  2. mysql_query("SELECT * FROM groceries ORDER BY shopaddress DESC")
  3. mysql_query("SELECT * FROM students ORDER BY department ASC")

If you don’t want to order it ascending or descending just remove ASC or DESC. It will be automatically ordered by the first data/record.