0% found this document useful (0 votes)
141 views

08 - Add JSON Data To MySQL PDF

The document contains code for a form to collect a user's name, email, and mobile number. It uses JavaScript to validate the fields are completed and stores the input as a JSON object. The data is sent to a PHP file that inserts the record into a database table. If the table does not exist, it is created. The PHP file then displays the input data and allows the user to view all records in a table with buttons to delete entries.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
141 views

08 - Add JSON Data To MySQL PDF

The document contains code for a form to collect a user's name, email, and mobile number. It uses JavaScript to validate the fields are completed and stores the input as a JSON object. The data is sent to a PHP file that inserts the record into a database table. If the table does not exist, it is created. The PHP file then displays the input data and allows the user to view all records in a table with buttons to delete entries.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Filename : exp8.

html

<html>
<body>
<table>
<tr><td><label for="uname">Name</label></td><td>: <input type="text" name="uname" id="uname" /></td></tr>
<tr><td><label for="email">Email</label></td><td>: <input type="text" name="email" id="email" /></td></tr>
<tr><td><label for="mobile">Mobile</label></td><td>: <input type="text" name="mobile" id="mobile" /></td></tr>
</table>
<input type="button" onclick="save();" value="Save Record" />
<input type="button" onclick="show();" value="Show All Records" />

<script type="text/javascript">
function show()
{
window.location="p8show.php?show=all";
}

function save()
{
var name=document.getElementById("uname").value;
var email=document.getElementById("email").value;
var mobile=document.getElementById("mobile").value;

if(name=="" || email=="" || mobile=="") alert("Please Enter All fileds before proceeding");


else
{
var myJson={"name" : name, "email" : email, "mobile" : mobile};
var data=JSON.stringify(myJson);
window.location ="p8.php?data="+data;
}
}
</script>
</body>
</html>

Page 1
Filename: p8.php
<html>
<body>
<?php
$data= stripslashes($_GET['data']);
$data=json_decode($data);

if($data->name=="") echo "Please enter all fields<br />and continue go <a href='exp8.html'>back</a>";
else
{
echo "<h2>Thank you</h2>";
?>
<table>
<tr><th>Name</th><td><?php echo $data->name; ?></td></tr>
<tr><th>Email</th><td><?php echo $data->email; ?></td></tr>
<tr><th>Mobile</th><td><?php echo $data->mobile; ?></td></tr>
</table>
<?php
//Connect to the MySQL hosted on localhost with user credentials root and nopasss to database web
$con=mysqli_connect("localhost","root","nopass","web") or die("Failed to connect to MySQL: ".mysqli_connect_error());

//Check whether table students exists or not


$query=mysqli_query($con,"SHOW TABLES LIKE 'members';");

if(mysqli_num_rows($query)<=0)
{
echo "Table <strong>members</strong> Not exists !<br />Creating table";
$query="CREATE TABLE members (
id INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
name VARCHAR( 25 ) NOT NULL ,
email VARCHAR( 25 ) NOT NULL ,
mobile VARCHAR( 25 ) NOT NULL
);";
mysqli_query($con,$query);
if(mysqli_error($con)) echo mysqli_error($con);
else echo ".... Successfully created.";
}

Page 2
$query="select * from members where name='".$data->name."' and mobile='".$data->mobile."'";
$query=mysqli_query($con,$query);
if($query && mysqli_num_rows($query)>0)
{
echo "This data already exisits in database.<br />";
}
else
{
$query="INSERT INTO members (name,email,mobile) VALUES ('".$data->name."','".$data->email."','".$data->mobile."');";
mysqli_query($con,$query);
if(mysqli_error($con)) echo "Error Occured: ".mysqli_error($con);
else echo "Total Records inseted =".mysqli_affected_rows($con);
}
}
?>

Filename: p8show.php
<html>
<head>
<meta charset="UTF-8">
<style>
.blueTable {
border: 1px solid #1C6EA4;
width: 100%;
border-collapse: collapse;
}
.blueTable td, blueTable th {
border: 1px solid #AAAAAA;
padding: 3px 2px;
}
.blueTable tr:nth-child(even) { background: #D0E4F5; }

.blueTable thead {
background: linear-gradient(to bottom, #5592bb 0%, #327cad 66%, #1C6EA4 100%);
border-bottom: 2px solid #444444;
}

Page 3
.blueTable thead th {
color: #FFFFFF;
border-left: 2px solid #D0E4F5;
}
.blueTable thead th:first-child { border-left: none; }
</style>
</head>

<body>
<?php
if($_POST['choice']>0)
{
$con=mysqli_connect("localhost","root","nopass","web") or die("Failed to connect to MySQL: ".mysqli_connect_error());
$query="delete from members where id=".$_POST['choice'];
mysqli_query($con,$query);
}

if(isset($_GET['show']))
{
$data=$_GET['show'];
echo "<h2>Members List</h2>";

//Connect to the MySQL hosted on localhost with user credentials root and nopasss to database web
$con=mysqli_connect("localhost","root","nopass","web") or die("Failed to connect to MySQL: ".mysqli_connect_error());

$query="select * from members order by name";


$query=mysqli_query($con,$query);
if($query && mysqli_num_rows($query)>0)
{
?>
<form method="post" id="frmProcess">
<table class="blueTable">
<thead>
<tr><th>Sl No</th><th>Name</th><th>Mobile</th><th>Email</th></tr>
</thead>
<tbody>
<?php

Page 4
$i=1;
while($row=mysqli_fetch_assoc($query))
{
?>
<tr><th><?php echo $i; ?></th>
<td><input type="radio" name="choice" value="<?php echo $row['id']; ?>" id="ID<?php echo $row['id']; ?>" />
<label for="ID<?php echo $row['id']; ?>"><?php echo $row['name']; ?></label></td>
<td><?php echo $row['mobile']; ?></td>
<td><?php echo $row['email']; ?></td>
</tr>
<?php
$i++;
}
}
echo '</tbody></table></form>';
?>
<button onclick="delItem();" name="delete">Delete</button>
</form>
<?php
}
?>
<button onclick="window.history.back();">Back</button>
<script>

function delItem()
{
var name=document.querySelector('input[name="choice"]:checked').closest("td").textContent;
if (confirm("Are you sure to delete ! "+name.trim())) { document.getElementById("frmProcess").submit(); }
}
</script>
</body>
</html>

Page 5

You might also like