How to make same button for insert and update
How to make same button for insert and update
config.php
<?php
$conn = mysqli_connect('localhost', 'root', '', 'parth');
?>
records.php
<?php
include('config.php');
?>
<?php
if(isset($_POST['submit'])) {
$name = $_POST['name'];
$email = $_POST['mail'];
$gen = $_POST['gender'];
$age = $_POST['age'];
//$hob =implode(",",$_POST['hobbies']);
$hob = implode(",",$_POST['hobbies']);
$pass = $_POST['pass'];
$cpass = $_POST['cpass'];
$query = mysqli_query($conn, "INSERT INTO form(name, email, gender, age, hobbies, pass, cpass)VALUES('".$name."', '".$email."', '".$gen."', '".$age."', '".$hob."', '".$pass."', '".$cpass."') ");
if($query)
{
echo "Insert";
}
else
{
echo "Fail";
}
}
if(isset($_POST['uid'])) {
$id = $_POST['uid'];
$name = $_POST['name'];
$email = $_POST['mail'];
$gen = $_POST['gender'];
$age = $_POST['age'];
//$hob =implode(",",$_POST['hobbies']);
//$hob = implode(",",$_POST['hobbies']);
$hob = implode(",",$_POST['hobbies']);
$pass = $_POST['pass'];
$cpass = $_POST['cpass'];
$query = mysqli_query($conn, "UPDATE form SET name = '".$name."', email = '".$email."', gender = '".$gen."', age = '".$age."', hobbies = '".$hob."', pass = '".$pass."', cpass = '".$cpass."' WHERE my_id = '".$id."' ");
if($query)
{
echo "Update";
}
else
{
echo "Fail";
}
}
$id = $_GET['id'];
$query = mysqli_query($conn, "DELETE FROM form WHERE my_id = '".$id."'");
header("Location:view.php");
?>
update.php
<?php
include('config.php');
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
$('#update').click(function(){
var u = $('#uid').val();
var nm = $('#name').val();
var em = $('#mail').val();
// var gen = $('#gender').val();
var gen = $("input[name='gender']:checked").val();
var ag = $('#age').val();
//var hb = $('#hob').val();
var hb = ;
$.each($("input[name='hobbies']:checked"), function(){
hb.push($(this).val());
});
// alert("My favourite sports are: " + hb.join(", "));
var p = $('#pass').val();
var cp = $('#cpass').val();
$('.error').remove();
var reg = /^([A-Za-z0-9_-.])+@([A-Za-z0-9_-.])+.([A-Za-z]{2,4})$/;
var validEmail = reg.test(em);
if(u.length < 1){
$('#uid').after('<span class="error">Update Id is required</span>');
return false;
}
else if(nm.length < 1){
$('#name').after('<span class="error">This field is required</span>');
return false;
}
else if(em.length < 1){
$('#mail').after('<span class="error">This field is required</span>');
return false;
}
else if(!validEmail){
$('#mail').after('<span class="error">Enter a valid email</span>');
return false;
}
if(!gen){
$('.gender').after('<span class="error">This field is required</span>');
return false;
}
else if(ag.length == ""){
$('#age').after('<span class="error">This field is required</span>');
return false;
}
else if(hb.length < 1){
$('.hob').after('<span class="error">This field is required</span>');
return false;
}
else if(p.length < 1){
$('#pass').after('<span class="error">This field is required</span>');
return false;
}
else if(cp.length < 1){
$('#cpass').after('<span class="error">This field is required</span>');
return false;
}
else if (p != cp) {
$('#cpass').after('<span class="error">Password not match.</span>');
return false;
} else
console.log("Starting ajax");
$.ajax({
url: "./records.php",
type: "post",
data: {
uid:u,
name:nm,
mail:em,
gender:gen,
age:ag,
hobbies:hb,
pass:p,
cpass:cp
},
success:function(data) {
alert('done');
}
});
});
});
</script>
</head>
<body>
<?php
if(isset($_GET['id'])) {
$id = $_GET['id'];
$query = mysqli_query($conn, "SELECT * FROM form WHERE my_id = '".$id."' ");
$q = mysqli_fetch_array($query);
}
?>
<a href="view.php">View</a>
<form>
<table>
<tr>
<td>Name</td>
<td><input type="text" name="name" id="name" value="<?= $q['name']; ?>"></td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" name="mail" id="mail" value="<?= $q['email']; ?>"></td>
</tr>
<tr>
<td>Gender</td>
<td>Male<input type="radio" name="gender" id="gender" class="gender" value="male" <?php if($q['gender']=="male"){ echo "checked";}?>>
Female<input type="radio" name="gender" id="gender1" class="gender" value="female" <?php if($q['gender']=="female"){ echo "checked";}?>></td>
</tr>
<tr>
<td>Age </td>
<td>
<select name="age" id= "age" value="<?= $q['age']; ?>">
<option value="<?= $q['age']; ?>"><?= $q['age']; ?></option>
<?php
for ($i=1; $i<=100; $i++)
{
?>
<option><?php echo $i;?></option>
<?php
}
?>
</select>
</td>
</tr>
<tr>
<td>Hobbies</td>
<td>
<?php $hobby=explode(",",$q['hobbies']); ?>
<input type="checkbox" name="hobbies" value="chess"<?php if(in_array("chess",$hobby)) { ?> checked="checked" <?php } ?> class="hob" id="hob">Chess
<input type="checkbox" name="hobbies" value="cricket" <?php if(in_array("cricket",$hobby)) { ?> checked="checked" <?php } ?> class="hob" id="hob1">Cricket
<input type="checkbox" name="hobbies" value="football" <?php if(in_array("football",$hobby)) { ?> checked="checked" <?php } ?> class="hob" id="hob2">Football
<input type="checkbox" name="hobbies" value="hockey" <?php if(in_array("hockey",$hobby)) { ?> checked="checked" <?php } ?> class="hob" id="hob3">Hockey
</td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="pass" id="pass" value="<?= $q['pass']; ?>"></td>
</tr>
<tr>
<td>Confirm Password</td>
<td><input type="password" name="cpass" id="cpass" value="<?= $q['cpass']; ?>"></td>
</tr>
<tr>
<td><input type="hidden" name="uid" id="uid" value="<?= $id ?>"></td>
<!-- <td><input type="submit" name="update" id="update" value="update"></td> -->
<td><button type="button" name="update" id="update">Update</button></td>
<!-- <?php
// if(isset($_GET['id']) > 1)
{
?>
<input type = "submit" class = "btn btn-primary" style="width:49%" value = "Save" name = "submit">
<?php
// } else {
?>
<input type = "submit" class = "btn btn-primary" style="width:49%" value = "Update" name = "submit">
<?php
}
?> -->
</tr>
</table>
</form>
</body>
</html>
index.php
<?php
include('config.php');
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
$('#submit').click(function(){
var s = $('#submit').val();
var nm = $('#name').val();
var em = $('#mail').val();
// var gen = $('#gender').val();
var gen = $("input[name='gender']:checked").val();
var ag = $('#age').val();
//var hb = $('#hob').val();
var hb = ;
$.each($("input[name='hobbies']:checked"), function(){
hb.push($(this).val());
});
// alert("My favourite sports are: " + hb.join(", "));
var p = $('#pass').val();
var cp = $('#cpass').val();
$('.error').remove();
var reg = /^([A-Za-z0-9_-.])+@([A-Za-z0-9_-.])+.([A-Za-z]{2,4})$/;
var validEmail = reg.test(em);
if(nm.length < 1){
$('#name').after('<span class="error">This field is required</span>');
return false;
}
else if(em.length < 1){
$('#mail').after('<span class="error">This field is required</span>');
return false;
}
else if(!validEmail){
$('#mail').after('<span class="error">Enter a valid email</span>');
return false;
}
if(!gen){
$('.gender').after('<span class="error">This field is required</span>');
return false;
}
else if(ag.length == ""){
$('#age').after('<span class="error">This field is required</span>');
return false;
}
else if(hb.length < 1){
$('.hob').after('<span class="error">This field is required</span>');
return false;
}
else if(p.length < 1){
$('#pass').after('<span class="error">This field is required</span>');
return false;
}
else if(cp.length < 1){
$('#cpass').after('<span class="error">This field is required</span>');
return false;
}
else if (p != cp) {
$('#cpass').after('<span class="error">Password not match.</span>');
return false;
} else
console.log("Starting ajax");
$.ajax({
url: "./records.php",
type: "post",
data: {
submit:s,
name:nm,
mail:em,
gender:gen,
age:ag,
hobbies:hb,
pass:p,
cpass:cp
},
success:function(data) {
alert('done');
}
});
});
});
</script>
</head>
<body>
<a href="view.php">View</a>
<?php
if(isset($_GET['id'])) {
$id = $_GET['id'];
$query = mysqli_query($conn, "SELECT FROM form WHERE my_id = '".$id."' ");
$q = mysqli_fetch_array($query);
}
?>
<form>
<table>
<tr>
<td>Name</td>
<td><input type="text" name="name" id="name"></td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" name="mail" id="mail"></td>
</tr>
<tr>
<td>Gender</td>
<td>Male<input type="radio" name="gender" class="gender" id="gender" value="male">
Female<input type="radio" name="gender" class="gender" id="gender1" value="female"></td>
</tr>
<tr>
<td>Age </td>
<td>
<select name="age" id= "age">
<option value="">selet age</option>
<?php
for ($i=1; $i<=100; $i++)
{
?>
<option><?php echo $i;?></option>
<?php
}
?>
</select>
</td>
</tr>
<tr>
<td>Hobbies</td>
<td>
<input type="checkbox" name="hobbies" class="hob" value="chess" id="hob">Chess
<input type="checkbox" name="hobbies" class="hob" value="cricket" id="hob1">Cricket
<input type="checkbox" name="hobbies" class="hob" value="football" id="hob2">Football
<input type="checkbox" name="hobbies" class="hob" value="hockey" id="hob3">Hockey
</td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="pass" id="pass"></td>
</tr>
<tr>
<td>Confirm Password</td>
<td><input type="password" name="cpass" id="cpass"></td>
</tr>
<tr>
<td></td>
<!-- <td><input type="submit" name="submit" id="submit"></td> -->
<td><button type="button" name="submit" id="submit">Submit</button></td>
<!-- <?php
// if(isset($_GET['id']) > 1)
{
?>
<input type = "submit" class = "btn btn-primary" style="width:49%" value = "Save" name = "submit">
<?php
// } else {
?>
<input type = "submit" class = "btn btn-primary" style="width:49%" value = "Update" name = "submit">
<?php
}
?> -->
</tr>
</table>
</form>
</body>
</html>
add.html
<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
$('#submit').click(function(){
var s = $('#submit').val();
var nm = $('#name').val();
var ml = $('#mail').val();
var ps = $('#pass').val();
$('.error').remove();
var reg = /^([A-Za-z0-9_-.])+@([A-Za-z0-9_-.])+.([A-Za-z]{2,4})$/;
var validEmail = reg.test(ml);
if(nm.length < 1) {
$('#name').after('<span class="error">This field is required</span>');
}
else if(ml.length < 1) {
$('#mail').after('<span class="error">This field is required</span>');
}
else if(!validEmail) {
$('#mail').after('<span class="error">Enter a valid email</span>');
}
else if(ps.length < 1) {
$('#pass').after('<span class="error">This field is required</span>');
} else {
$.ajax({
url: "./index.php",
type:"post",
data: {
submit:s,
name:nm,
email:ml,
pass:ps
},
success:function(data) {
alert('done');
}
});
}
});
});
</script>
</head>
<body>
<a href="view.php">view</a>
<form>
<table>
<tr>
<td>Name</td>
<td><input type="text" name="name" id="name"></td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" name="mail" id="mail"></td>
</tr>
<tr>
<td>Password</td>
<td><input type="text" name="pass" id="pass"></td>
</tr>
<tr>
<td></td>
<td><button type="button" name="submit" id="submit" class="btn">Submit</button></td>
</tr>
</table>
</form>
</body>
</html>
view.php
<?php
include('config.php');
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
$('.del').click(function(){
var d = $(this).data('id');
if(confirm('Are you sure you want to delete ?')) {
if(d== '') {
alert('This user cant be deleted');
return false;
} else {
$.ajax({
url: "./records.php?id="+d,
type: "post",
data: {
del:d
},
success:function(data) {
$('body').html(data);
}
});
}
}
});
});
</script>
</head>
<body>
<a href="index.php">Add</a>
<?php
$query = mysqli_query($conn, "SELECT * FROM form");
?>
<table>
<tr>
<td>Name</td>
<td>Email</td>
<td>Gender</td>
<td>Age</td>
<td>Hobbies</td>
<td>Password</td>
<td>Confitm Password</td>
<td>Action</td>
</tr>
<?php
while($q = mysqli_fetch_array($query)) {
?>
<tr>
<td><?= $q['name'] ?></td>
<td><?= $q['email'] ?></td>
<td><?= $q['gender'] ?></td>
<td><?= $q['age'] ?></td>
<td><?= $q['hobbies'] ?></td>
<td><?= $q['pass'] ?></td>
<td><?= $q['cpass'] ?></td>
<td><a href="update.php?id=<?= $q['my_id']; ?>">Update</a> | <a href="#!" class="del" data-id="<?= $q['my_id']; ?>">Delete</a></td>
</tr>
<?php
}
?>
</table>
</body>
</html>
How to make same button for insert and update and make single form with php jquery ajax so solve the problem so its make single button for insert and update and also give me idea how to make it in one form with one button and perform insert and update operation so How to make same button for insert and update and make single form with php jquery ajax
Your code is always inserting a new record, regardless if
uid
exists or not. All you need to do is if it exists, do an update. If not, do an insert.– Magnus Eriksson
11 mins ago
uid
Don't post code as a comment. Edit your question instead.
– Magnus Eriksson
8 mins ago
You also need to seriously narrow down the code in your question. Only post the relevant parts, not your whole application.
– Magnus Eriksson
6 mins ago
Are you even reading our comments or are you simply ignoring them? You need to fix your question... Please read: How to create a Minimal, Complete, and Verifiable example and also How do I ask a good question?
– Magnus Eriksson
5 mins ago
1 Answer
1
<?php
if(isset($_GET['id']) > 1)
{
?>
<button type = "button" value = "Save" name = "submit">Insert</button>
<?php
} else {
?>
<button type = "button" value = "Update" name = "submit">Update</button>
<?php
}
?>
This is not an answer. Wrong place to post your updated code. Edit your question again.
– mickmackusa
2 mins ago
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Warning: You are wide open to SQL Injections and should really use parameterized Prepared Statements instead of manually building your queries like that. Specially since you're not escaping the user inputs at all!
– Magnus Eriksson
14 mins ago