PHP

QUESTION

CALCULATE SUM FROM Given STRING.

$a="aaaa_123@###_bb@b_456_ccc_7891";

SOL :


echo "Simple";
$a="aaaa_123@###_bb@b_456_ccc_7891";
$arr=str_split($a);
print_r($arr);
echo "
";

echo "
";
echo "by using function";
echo "
";
echo array_sum($arr);

?>

2. swap two variables without using a third variable.


$a = 5;
$b = 6;
list($a, $b) = array($b, $a);
print $a . ',' . $b;

3. Output of

$var a
echo a;

Answer .Parse error: syntax error, unexpected ‘a’ (T_STRING)

4. TAKE USER INFO AND STORE IN DB.(TABLE FORM)

ID,NAME,FIRST,LAST,LAST,ADDRESS,PHOTO

AND EDIT RECORD,DELETE RECORD.


CSS

@charset "utf-8";
/* CSS Document */

*
{
padding:0;
margin:0;
}
body
{
background:#fff;
font-family:Georgia, "Times New Roman", Times, serif;
text-align:center;
}
#header
{
background:#00a2d1;
width:100%;
height:50px;
color:#fff;
font-size:36px;
font-family:Verdana, Geneva, sans-serif;
}
#body
{
margin-top:100px;
}
#body table
{
margin:0 auto;
position:relative;
bottom:50px;
}
table td,th
{
padding:20px;
border: solid #9fa8b0 1px;
border-collapse:collapse;
}
#footer
{
text-align:center;
position:absolute;
left:0;
right:0;
margin:0 auto;
bottom:50px;
}

INDEX.PHP

<?php include_once 'dbconfig.php'; ?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
	<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<div id="header">
<label>DETAILS</label></div>
<div id="body">
<form action="upload.php" method="post" enctype="multipart/form-data">
ID <input type="text" name="ID" required> 

FirstNAme <input type="text" name="first" required> 

LastName <input type="text" name="last" required> 

Email <input type="text" name="email" required> 

Address<input type="text" name="addr" required> 

<input type="file" name="file" />
<button type="submit" name="btn-upload">Submit</button>
</form>

<?php if(isset($_GET['success'])) { ?>
<label>File ADDED Successfully... <a href="view.php">click here to view file.</a></label>
<?php } else if(isset($_GET['fail'])) { ?>
<label>Problem While File Uploading !</label>
<?php } else { ?>

<?php } ?></div>
<a href="view.php">click here to view file.</a>
</body>
</html>

dbconfig.php

<?php $dbhost = "localhost"; $dbuser = "root"; $dbpass = ""; $dbname = "test"; mysql_connect($dbhost,$dbuser,$dbpass) or die('cannot connect to the server');  mysql_select_db($dbname) or die('database selection problem'); ?>

upload.php

<?php include_once 'dbconfig.php'; if(isset($_POST['btn-upload'])) {  $ID=$_POST['ID']; $first=$_POST['first']; $last=$_POST['last']; $email=$_POST['email']; $addr=$_POST['addr']; $file = rand(1000,100000)."-".$_FILES['file']['name']; $file_loc = $_FILES['file']['tmp_name']; $folder="uploads/"; // new file size in KB $new_size = $file_size/1024;  // new file size in KB // make file name in lower case $new_file_name = strtolower($file); // make file name in lower case $final_file=str_replace(' ','-',$new_file_name); if(move_uploaded_file($file_loc,$folder.$final_file)) { $sql="INSERT INTO mahesh(id,first,last,email,Addr,pic) VALUES('$ID','$first','$last','$email','$addr','$final_file')"; mysql_query($sql); ?>
<script>
alert('successfully ADDED');
window.location.href='index.php?success';
</script>
<?php } else { ?>
<script>
alert('error while uploading file');
window.location.href='index.php?fail';
</script>
<?php } } ?>

VIEW.PHP

<?php include_once 'dbconfig.php'; ?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
	<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<div id="header"></div>
<div id="body">
<table width="80%" border="1">
<tr>
<th colspan="4">ADD NEW ENTRY<label><a href="index.php">-----------CLICK HERE</a></label></th>
</tr>
<tr>
<td>ID</td>
<td>First Name</td>
<td>Last Name</td>
<td>Email</td>
<td>Address</td>
<td>Profile Pic</td>
<td colspan=2>Action</td>
</tr>
<?php $sql="SELECT * FROM mahesh"; $result_set=mysql_query($sql); while($row=mysql_fetch_array($result_set)) { ?>
<tr>
<td><?php echo $row['id'] ?></td>
<td><?php echo $row['first'] ?></td>
<td><?php echo $row['last'] ?></td>
<td><?php echo $row['email'] ?></td>
<td><?php echo $row['Addr'] ?></td>
<td><img src="http://localhost:8089/test1/uploads/<?php echo $row['pic'] ?>" Height=100 width=100></td>
<td><a href="edit1.php" target="_blank">EDIT</a></td>
<td><a href="delete.php" target="_blank">DELETE</a></td>
</tr>
<?php } ?></table>
</div>
<a href="uploads/<?php echo $row['pic'] ?>"</a>
</body>
</html>

uploadedit.php

<?php include_once 'dbconfig.php'; if(isset($_POST['btn-upload'])) {  $ID=$_POST['ID']; $first=$_POST['first']; $last=$_POST['last']; $email=$_POST['email']; $addr=$_POST['addr']; $file = rand(1000,100000)."-".$_FILES['file']['name']; $file_loc = $_FILES['file']['tmp_name']; $folder="uploads/"; // new file size in KB $new_size = $file_size/1024;  // new file size in KB // make file name in lower case $new_file_name = strtolower($file); // make file name in lower case $final_file=str_replace(' ','-',$new_file_name); if(move_uploaded_file($file_loc,$folder.$final_file)) { $sql = "UPDATE mahesh SET first='$first' , last='$last', email='$email' , Addr='$addr' , pic='$final_file' where id='$ID' "; mysql_query($sql); ?>
<script>
alert('successfully ');
window.location.href='edit1.php?success';
</script>
<?php } else { ?>
<script>
alert('error while uploading file');
window.location.href='edit1.php?fail';
</script>
<?php } } ?>

DELETE.PHP

<?php if($_SERVER['REQUEST_METHOD']=='POST'){ $id=$_POST['id']; $con = mysqli_connect('localhost','root','','test') or die('Unable To connect'); $sqlss = "delete from mahesh where id='$id' "; if ($con->query($sqlss) === TRUE) {
echo "Record deleted successfully";
} else {
echo "Error deleting record: " . $con->error;
}

}
?>

<html>
<head>
<title>Delete</title>
</head>
<body>
<form action="delete.php" method="post" enctype="multipart/form-data">

ENTER ID TO DELETE<input type="text" name="id" /> 

<button>SUBMIT</button>
</form>

<a href="view.php">click here to view file.</a>
</body>
</html>

5. Query for Highest Salary ?

define('DB_HOST', 'localhost');
define('DB_NAME', 'test');
define('DB_USER','root');
define('DB_PASSWORD','');
$con=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) or die("Failed to connect to MySQL: " . mysql_error());
$db=mysql_select_db(DB_NAME,$con) or die("Failed to connect to MySQL: " . mysql_error());
$query = mysql_query("SELECT max(sal) as sal FROM salary");

$row = mysql_fetch_array($query);

echo $row['sal'];
echo("
");

echo("
");

?>

5. Query for second Highest Salary ?

1. select MAX(Sal) from salary WHERE Sal NOT IN (select MAX(Sal) from salary )
2. SELECT sal FROM salary ORDER BY sal DESC LIMIT 1,1

6. Query for Getting First Three maximum salary?

SELECT sal FROM salary ORDER BY sal DESC LIMIT 0,3 
Advertisements
Posted in PHP

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s