Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

Sunday, January 16, 2011

PHP MYSQL Tutorial 2: Insert, Update, Delete records in a Table.

This is second of PHP MYSQL basics tutorials. If you have not read the first one, please do it here :
http://codingbasics.blogspot.com/2011/01/php-mysql-tutorial-1-install-php-mysql.html
where each and every step is very detailed explained with code.

Insert rows into table


Insert.php


<html> 
<body> 
<? 
if ($UserName) 

mysql_connect (localhost, username, password) or die ("Problem connecting to DataBase"); 
$query = "insert into cust values ('$cid','$UserName','$LastName','$Details')"; 
$result = mysql_db_query("eshop", $query); 


echo "Data inserted. new table:<br><p></p>"; 
$query = "SELECT * FROM cust"; 
$result = mysql_db_query("eshop", $query); 


if ($result) { 
echo "<table width=90% align=center border=1><tr> 
<td align=center bgcolor=#00FFFF>idx</td> 
<td align=center bgcolor=#00FFFF>User Name</td> 
<td align=center bgcolor=#00FFFF>Last Name</td> 
<td align=center bgcolor=#00FFFF>Free Text</td> 
</tr>"; 


while ($r = mysql_fetch_array($result)) { 
$cid = $r["cid"]; 
$user = $r["UserName"]; 
$last = $r["LastName"]; 
$det = $r["Details"]; 


echo "<tr> 
<td>$idx</td> 
<td>$user</td> 
<td>$last</td> 
<td>$det</td> 
</tr>"; 


} // end of while loop 
echo "</table>"; 
} else { 
echo "No data."; 
} // end of if ($result) 
} else { 
echo "No UserName Entered. Please go back and reenter UserName"; 
} // end of if ($UserName) 
echo "<p></p>"; 
include ('sitelinks.txt'); 
?> 
</body> 
</html>

Read more for update and Delete records from the table.

Thursday, January 13, 2011

PHP MYSQL - Tutorial 1 : Install PHP, MYSQL , connect and view the contents of a table

Most of you already experienced by now, its not that easy to install apache, php and mysql
and get started with PHP quickly. you may face more problems especially on windows.
For this, there is wonderful free tool called XAMPP.

XAMPP is an easy to install Apache distribution containing MySQL, PHP and Perl
THis is available for Win,Linux, Solaris, Mac OS at : http://www.apachefriends.org/en/xampp.html

When I used Xampp, the only issue I faced was "apache was not started" , but it was due to a the port 80
was already in by other service in my system.In that case, change the port in httpd.conf
or stop the service( if you do not need it now like tomcat etc.,)

Okay, let me assume that PHP is installed.
This tutorial series is all about connecting to MYSQL Database , and performing varios database operations

First step obviously is to create a table.
You can install SQLYOG( excellent MySQL client) to do the qureying on the MYSQL Database Server installed by Xampp.
or since this is a basic tutorial , you can even type the commands in mysql command line.

Lets get into code now :

Sunday, January 9, 2011

Submit Form with JQuery , PHP, HTML,MYSQL

Let us understand with a simple example of how to submit a form to a database with JQuery, PHP, HTML, MySql.

This is a direct working code with explanation given whereever required.


<div class="container">  
<form id="submit" method="post">  
<fieldset>  
              <legend>Enter Employee Information</legend>  
  
              <label for="fname">Employee First Name:</label>  
           <input id="fname" class="text" name="fname" size="20" type="text">  
  
              <label for="lname">Employee Last Name:</label>  
              <input id="lname" class="text" name="lname" size="20" type="text">  
  
            <button class="button positive"> <img src="../images/icons/somepic.png" alt=""> 
                 Add Employee </button>  
  </fieldset>  
</form>  
<div class="success" style="display:none;">Employee has been added.</div>  
</div>  


// It is relatively easy to make ajax calls with jquery. we need to pass type( GET or POST),
// URL ( of the destination), data,  success( what to be done post successful execution).

$(document).ready(function(){  
    $("form#submit").submit(function() {  
    // we want to store the values from the form input box, then send via ajax below

    var fname     = $('#fname').attr('value'); 

// We are storing it in the variable called “fname” and we are taking it from the input
       //  field with the id of “fname” and the “.attr(‘value’) tells jQuery to take the value from
       // the attribute of the value from the input field

    var lname     = $('#lname').attr('value');  
        $.ajax({  
            type: "POST",  
            url: "ajaxcalls.php",  
            data: "fname="+ fname +"& lname="+ lname,  
            success: function(){  
                $('form#submit').hide(function(){$('div.success').fadeIn();});  
  
            }  
        });  
    return false;  
    });  
});  



Add this in ajaxcalls.php File:



<?php  
  
    include ("../../inc/config.inc.php");  
  
    // Employee INFORMATION  
    $fname        = htmlspecialchars(trim($_POST['fname']));  
    $lname        = htmlspecialchars(trim($_POST['lname']));  
  
    $addClient  = "INSERT INTO Employees (fname,lname) VALUES ('$fname','$lname')";  
    mysql_query($addClient) or die(mysql_error());  
  
?>  


Hope you like it.

captcha code with PHP

This is php script used to generate the captcha message.


somcaptcha.php:
---------------


<?php
session_start();
$ranStr = md5(microtime());
$ranStr = substr($ranStr, 0, 6);
$_SESSION['cap_code'] = $ranStr;
$newImage = imagecreatefromjpeg("somecapimg.jpg");
$txtColor = imagecolorallocate($newImage, 0, 0, 0);
imagestring($newImage, 5, 5, 5, $ranStr, $txtColor);
header("Content-type: image/jpeg");
imagejpeg($newImage);
?>

Now we need to validate the generated capatcha code with information submitted in user.



<?php
session_start();
$cap = 'notEq'; // storing the value in a php variable to jquery variable to show alert for quick debugging.
if ($_SERVER['REQUEST_METHOD'] == 'POST') 
{
if ($_POST['captcha'] == $_SESSION['cap_code']) 
{
// Captcha verification is Correct. PRoceed further
$cap = 'Eq';
else 
{
// Captcha verification is wrong. Take other action
$cap = '';
}
}
?>




<html>
<body>
<form action="" method="post">
<label>Name:</label><br/>
<input type="text" name="name" id="name"/>
<label>Message:</label><br/>
<textarea name="msg" id="msg"></textarea>
<label>Enter the contents of image</label>
<input type="text" name="captcha" id="captcha" />
<img src='somecaptcha.php' />
<input type="submit" value="Submit" id="submit"/>
</form>
<div class="cap_status"></div>
</body>
</html>


you can add more validations at client side .

subversion video