Debugging PHP with Visual Studio Code

Debugging PHP with Visual Studio Code





Debugging is one of the most important thing of our application development. To make quality software, debugging is a vital part of development.In this post, I am going to show how to debug PHP with visual studio code.

About Visual Studio Code:

Visual studio code, developed by microsoft is an open source code editor features integrated debugging for almost all programming language.

Installation of VS Code:

To get started first visit https://code.visualstudio.com to download visual studio code. Once VS code installed open it and then you need to install PHP debug extension.

Installation of Xdebug PHP Extension in VS Code:

To install PHP Extension just move to the extension in sidebar icons of VS code or you can press CTRL+SHIFT+X to explore extension. After that type PHP debug in search extension and install it.

After successfully installed Extension you need to setup user setting. Just  Go to setting or press CTRL+, you will be move to the setting and change executable path as i am showing below.

Installation of Xdebug:

To configure xdebug in local server you need to go https://xdebug.org/wizard.php and paste here your phpinfo(). After that you will get instructions what to do next.To get phpinfo, go to the browser and type localhost you will be move to the dasboard and here you can find the phpinfo menu just click it and copy all the content by pressing CTRL+A.

INSTRUCTIONS

Debugging Setup in VS COde:

Next we need to setup debugging mode in vs code. Just open folder containing PHP Code file and choose the debug mode from the left hand side.

Set Breakpoint:

To debug, need to add breakpoint in code. To add breakpoint click on the left of the line where you want to add breakpoint as i am showing showing below.

Breakpoint are indicated by red dot. When execution start on browser, it will stop at breakpoint where we can check the state of the code and variables.

Start Debugging:

To start debugging just click on the start debugging option. For the first time it will create configuration file, no need to change for our puposes .If everything is setup correctly then VS code will be in debbuging mode. Run your page and you will get your debugging environment in VS code with Xdebug.


Thank you for reading this post. we hope you like this Post, Please feel free to comment below, your suggestion. If you face any issue with this code let us know. We’d love to help!

Insert, Edit, Update, Delete Data using Php

In order to insert, edit, update Data in Php, We need to follow certain steps:-

  1. Check the connection to database.
  2. Create an html Form
  3. Insert data to database by filling form
  4. Edit, Update, Delete Data.

CHECK THE CONNECTION TO DATABASE.

In order to check connection to database, we need to know SERVER NAME, USER, PASSWORD, DATABASE .

db.php

<?php
ob_start();
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', '');
define('DB_DATABASE', 'techalltype');
$connection = mysql_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD) or die(mysql_error());
$database = mysql_select_db(DB_DATABASE) or die(mysql_error());
?>

NOTE:- While working on local computer server name is localhost, username is root,and password is blank (by default).

CREATE AN HTML FORM:-

In this step we simply use html to create a form.

index.php

<form action="insert.php" method="post">
<p>
  <label> USER NAME : </label>
  <input type="text" name="username" required placeholder="Enter your name" />
</p>
<p>
  <label> EMAIL ID : </label>
  <input type="email" name="usermail" required placeholder="Enter your Email ID"/>
</p>
<p>
  <label> MOBILE NUMBER : </label>
  <input type="text" name="usermobile" required placeholder="Enter your mobile number"  />
</p>
<p>
  <input type="submit" name="send" value="Submit" />
</p>
</form>

Here action=”insert.php”  means when we click on submit button the page will redirect to insert.php page. Method=”post” means when form will submit form will use post data.(Post data is referred most times because post method can carry maximum data from one page to another).

Insert.php

<?php
 ob_start();
  include("db.php");  // check the connection
  if(isset($_POST['send'])!="") // if send button is clicked
  {
  $username=mysql_real_escape_string($_POST['username']);  // mysql_real_escape_string use to escapes special characters in a string
  $usermail=mysql_real_escape_string($_POST['usermail']);
  $usermobile=mysql_real_escape_string($_POST['usermobile']);
  $update=mysql_query("INSERT INTO register(username,emailid,mobileno,created)VALUES
									  ('$username','$usermail','$usermobile',now())"); // insert statement to add data to database
  
  if($update)
  {
	  $msg="Successfully Updated!!";
	  echo "<script type='text/javascript'>alert('$msg');</script>";// If data inserted to database alert message display
	  header('Location:index.php');// If data inserted move to index.php page 
  }
  else
  {
	 $errormsg="Something went wrong, Try again"; 
	  echo "<script type='text/javascript'>alert('$errormsg');</script>"; // if data not inserted up line message display
	  header('Location:index.php'); If data not inserted move to index.php page 
  }
  }
 ob_end_flush();
?>

DISPLAY DATA FROM DATABASE:-

To view data from database, we need to create a page

view.php

<?php
 include('db.php');
  $select=mysql_query("SELECT * FROM register order by id desc"); // select * means display all records 
  $i=1;
  while($userrow=mysql_fetch_array($select))
  
  {
  $id=$userrow['id'];
  $username=$userrow['username'];
  $usermail=$userrow['emailid'];
  $usermobile=$userrow['mobileno'];
  $created=$userrow['created']
?>

 <p> USER NAME : <span><?php echo $username; ?></span>
    <a href="delete.php?id=<?php echo $id; ?>" 
    onclick="return confirm('Are you sure you wish to delete this Record?');">
    		<span class="delete" title="Delete"> delete </span></a>
  </p>
  <br />
  <p> EMAIL ID : <span><?php echo $usermail; ?></span>
    <a href="edit.php?id=<?php echo $id; ?>"><span class="edit" title="Edit"> edit </span></a>
  </p>
  <br />
  <p> MOBILE NO : <span><?php echo $usermobile; ?></span>
  </p>
<br>
<?php } ?>

EDIT DATA-

In order to edit data from database we need to click on edit button available on view.php.

edit.php

<?php 
ob_start();
include('db.php');
if(isset($_GET['id']))
{
  $id=$_GET['id'];
  if(isset($_POST['update']))
  {
  $eusername=$_POST['eusername'];
  $eusermail=$_POST['eusermail'];
  $emobile=$_POST['eusermobile'];
  $updated=mysql_query("UPDATE register SET 
		username='$eusername', emailid='$eusermail', mobileno='$emobile' WHERE id='$id'")or die();
  if($updated)
  {
  $msg="Successfully Updated!!";
  header('Location:index.php');
  }
}
}
ob_end_flush();
?>


<?php 
  if(isset($_GET['id']))
  {
  $id=$_GET['id'];
  $getselect=mysql_query("SELECT * FROM register WHERE id='$id'");
  while($profile=mysql_fetch_array($getselect))
  {
    $username=$profile['username'];
    $usermail=$profile['emailid'];
    $usermobile=$profile['mobileno'];
?>

<form action="" method="post">
    <p>
      <label> USER NAME : </label>
      <input type="text" name="eusername" required placeholder="Enter your name" 
      value="<?php echo $username; ?>" id="inputid" />  // calling data from database in textbox and by submitting the form updated data insert in database
    </p>
    <p>
      <label> EMAIL ID : </label>
      <input type="email" name="eusermail" required placeholder="Enter your Email" 
      value="<?php echo $usermail; ?>" id="inputid" />
    </p>
    <p>
      <label> MOBILE NUMBER : </label>
      <input type="text" name="eusermobile" required placeholder="Enter your mobile number" 
      value="<?php echo $usermobile; ?>" id="inputid" />
    </p>
    <p>
      <input type="submit" name="update" value="Update" id="inputid" />
    </p>
  </form>

<?php } } ?>

DELETE DATA-

In order to delete data from database using form we make page

delete.php

<?php
  ob_start();
  include("db.php");
  if(isset($_GET['id'])!="")
  {
  $delete=$_GET['id'];
  $delete=mysql_query("DELETE FROM register WHERE id='$delete'");
  if($delete)
  {
	  header("Location:index.php");
  }
  else
  {
	  echo mysql_error();
  }
  }
  ob_end_flush();
?>

REGISTER.SQL

CREATE TABLE IF NOT EXISTS `register` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(500) NOT NULL,
  `emailid` varchar(500) NOT NULL,
  `mobileno` varchar(500) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;