-3

index.php

<html>
<head>
    <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();

                console.log("Starting ajax");
                $.ajax({
                    url: "./records.php",
                    type: "post",
                    data: {
                        submit:s,
                        name:nm,
                        mail:em
                    },
                    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);
        }
     ?>
    <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></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 = "hidden" name = "uid" id = "uid">
                <input type = "submit" class = "btn btn-primary" style="width:49%" value = "Update" name = "submit">
                <?php 
                } 
                ?>
            </tr>
        </table>
    </form>
</body>
</html>

How to use same button for insert & update, 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

lucky
  • 308
  • 2
  • 4
  • 17
  • 3
    **Warning:** You are wide open to [SQL Injections](http://php.net/manual/en/security.database.sql-injection.php) and should really use parameterized [Prepared Statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) instead of manually building your queries like that. Specially since you're not escaping the user inputs at all! – M. Eriksson Jul 31 '18 at 05:40
  • 1
    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. – M. Eriksson Jul 31 '18 at 05:42
  • 1) { ?> i apply this code in index.php page where i submit form but still its not done....can u show me how to make it – lucky Jul 31 '18 at 05:44
  • Possible duplicate of [Replace Into Query Syntax](https://stackoverflow.com/questions/19820724/replace-into-query-syntax) – mickmackusa Jul 31 '18 at 05:44
  • @lucky never write question details as comments, please move your commented info into your question via an Edit, then delete your comment. – mickmackusa Jul 31 '18 at 05:45
  • 2
    Don't post code as a comment. Edit your question instead. – M. Eriksson Jul 31 '18 at 05:45
  • so in this code how can i use single button for insert and update – lucky Jul 31 '18 at 05:47
  • 2
    You also need to _seriously_ narrow down the code in your question. Only post the _relevant_ parts, not your whole application. – M. Eriksson Jul 31 '18 at 05:47
  • ok but from seeing my code can u tell me how i apply it – lucky Jul 31 '18 at 05:48
  • 2
    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](http://stackoverflow.com/help/mcve) and also [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask) – M. Eriksson Jul 31 '18 at 05:49
  • yes i applied code in my program but its not worked – lucky Jul 31 '18 at 05:50
  • ok i fix it.... – lucky Jul 31 '18 at 05:50
  • Show your updated code then. FYI `(isset($_GET['id']) > 1)` <-- that ain't right – mickmackusa Jul 31 '18 at 05:50
  • 1) { ?> – lucky Jul 31 '18 at 05:51
  • Well, since you clearly don't care what we write in the comments (specially about narrowing down the code and not posting code in comments), I'm voting to close this question as too broad. This is a rabbit hole I'll avoid. – M. Eriksson Jul 31 '18 at 05:52
  • You don't need to query with $_GET['id'] , this can be done through one single form and one single button . Put a checking in your PHP code and based on that insert or update . – PHP Web Jul 31 '18 at 05:54
  • Another FYI, your regex can be shortened to: `var reg = /^[\w.-]+\@[\w.-]+\.[a-z]{2,4}$/i;` with the same effect. – mickmackusa Jul 31 '18 at 05:56
  • i update my code in index.php page at submit button and also fetched id but its not work – lucky Jul 31 '18 at 05:58
  • You are storing raw (un-encrypted) passwords. This practice should **NEVER** be performed. I hope you aren't using this code in a public application because it is no where near ready. – mickmackusa Jul 31 '18 at 06:07

1 Answers1

1
if(isset($_POST['submit']) && !isset($_POST['uid']) ) {
    $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['submit']) && 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";
    }
}
  • 1
    Your conditionals can be better structured. You are declaring single-use variables. The queries are insecure/unstable. This is a code-only answer which makes it low-value on Stackoverflow. Please largely improve this answer. – mickmackusa Jul 31 '18 at 05:58
  • 1
    ...AND you are not encrypting the password values being stored in the database. No one should be using this code. – mickmackusa Jul 31 '18 at 06:08