1

I have a table in my page which displays data about a certain request. enter image description here

When I click the 'Add Cedula Number', which is a button, it shows a bootstrap modal. And inside it, has a form. enter image description here

The value in the ID which is 14 is the ID of that row and it came from this code:

<td class=" ">
    <button class="btn btn-primary btn-xs" id="<?php echo $data['idPerson'];?>" data-toggle="modal" data-target="#myModal"> Add Cedula Number </button>
</td>

Now, in order to pass the value in the modal, I used this code:

<script>
    $('#myModal').on('show.bs.modal', function (e) {
        $(this).find('.modal-body').html('   <div class="form-group"><label class="col-sm-3 control-label">ID</label><div class="col-sm-9"><input type="text" value = ' + e.relatedTarget.id +' class="form-control" id="person_id" name="person_id" readonly="readonly"></div></div>          <div class="form-group"> <label class="col-sm-3 control-label">Date</label><div class="col-sm-9"><input type="date" readonly="readonly" class="form-control" id="cedula_date" value="<?php echo date("Y-m-d");?>" name="cedula_date"></div></div>           <div class="form-group"> <label class="col-sm-3 control-label">Cedula Number</label><div class="col-sm-9"><input type="number" class="form-control" id="cedula_number" name="cedula_number"/></div></div>' );
    })
</script>

This is my modal code:

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">
                    <span aria-hidden="true">&times;</span>
                    <span class="sr-only">Close</span>
                </button>
                <h4 class="modal-title" id="myModalLabel">Modal title</h4>
            </div>
            <form id="addCedula" action="paid_cedula.php" class="form-horizontal calender" name = "addCedula" enctype="multipart/form-data" method="post" role="form">
            <div class="modal-body"></div>
            <div class="modal-footer" style="margin:0;">
            <button type="button" class="btn btn-default" 
                            data-dismiss="modal">Close</button>
            <button id="send" type="submit" class="btn btn-success" name="addCedula">Save Record</button>
             </div>
             </form>
        </div>
    </div>
</div>

And my php code is this:

<?php
    include 'config.php';
        if(isset($_POST['addCedula'])){
        $id = $_POST['person_id'];
        $date = $_POST['cedula_date'];
        $number = $_POST['cedula_number'];                  
        $sql = mysqli_query($conn, "UPDATE person SET dateOfCedulaPayment='$date' AND cedulaNumber='$number' WHERE idPerson='$id';");
        mysqli_close($conn);                    
        }   
?>

I've been trying to look for the error for hours now. I really can't seem to find where. The value doesn't update in the database. Where did I go wrong? Your help will be much appreciated. Thank you.

5
  • that isn't how UPDATE works dev.mysql.com/doc/en/update.html and php.net/manual/en/mysqli.error.php would have told you about the syntax error. Commented Jan 9, 2016 at 15:30
  • Hi. So something is wrong with my query? Commented Jan 9, 2016 at 15:32
  • 1
    there is definitely something wrong with your query. RTM with the manual link I left you. Commented Jan 9, 2016 at 15:32
  • Hi again. I found my error. Yes, my query was definitely wrong. I removed the "AND" and it updated. It works fine now. THANK YOU SO MUCH. Commented Jan 9, 2016 at 15:34
  • You're welcome, cheers Commented Jan 9, 2016 at 15:36

2 Answers 2

3

@Fred's answer solved the problem (error in Update Query), Here is my 2 cent

This script is overkill to pass the data to modal.

<script>
$('#myModal').on('show.bs.modal', function (e) {
    $(this).find('.modal-body').html('   <div class="form-group"><label class="col-sm-3 control-label">ID</label><div class="col-sm-9"><input type="text" value = ' + e.relatedTarget.id +' class="form-control" id="person_id" name="person_id" readonly="readonly"></div></div>          <div class="form-group"> <label class="col-sm-3 control-label">Date</label><div class="col-sm-9"><input type="date" readonly="readonly" class="form-control" id="cedula_date" value="<?php echo date("Y-m-d");?>" name="cedula_date"></div></div>           <div class="form-group"> <label class="col-sm-3 control-label">Cedula Number</label><div class="col-sm-9"><input type="number" class="form-control" id="cedula_number" name="cedula_number"/></div></div>' );
})

All you need is change id="<?php echo $data['idPerson'];?>" to data-id="<?php echo $data['idPerson'];?>" and following 3 to 4 lines of script do the same job.

$(document).ready(function(){
    $('#myModal').on('show.bs.modal', function (e) {
        var id = $(e.relatedTarget).data('id');
        alert(id);
        $("#person_id").val(id);
     });
});

Modal HTML

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">
                    <span aria-hidden="true">&times;</span>
                    <span class="sr-only">Close</span>
                </button>
                <h4 class="modal-title" id="myModalLabel">Modal title</h4>
            </div>
            <form id="addCedula" action="paid_cedula.php" class="form-horizontal calender" name = "addCedula" enctype="multipart/form-data" method="post" role="form">
                <div class="modal-body">
                    <div class="form-group">
                        <label class="col-sm-3 control-label">ID</label>
                        <div class="col-sm-9">
                            <input type="text" value = "" class="form-control" id="person_id" name="person_id" readonly="readonly">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">Date</label>
                        <div class="col-sm-9">
                            <input type="date" readonly="readonly" class="form-control" id="cedula_date" value="<?php echo date("Y-m-d");?>" name="cedula_date">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">Cedula Number</label>
                        <div class="col-sm-9">
                            <input type="number" class="form-control" id="cedula_number" name="cedula_number"/>
                        </div>
                    </div>
                </div>
            <div class="modal-footer" style="margin:0;">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                <button id="send" type="submit" class="btn btn-success" name="addCedula">Save Record</button>
            </div>
            </form>
        </div>
    </div>
</div>
Sign up to request clarification or add additional context in comments.

1 Comment

This is definitely better than the code I presented. I updated my code and it works just the same and this code is much better. Thank you so much. I really have no idea that I could pass the value in just a few lines using jquery. Thank you for this. I am forever grateful.
2

Your query is incorrect. That isn't how UPDATE works. You need to use a comma and not the AND logical operator http://dev.mysql.com/doc/en/logical-operators.html

UPDATE person SET dateOfCedulaPayment='$date', cedulaNumber='$number' WHERE idPerson='$id';

Reference:

Checking for errors on your query would have triggered a syntax error:

Sidenote: To see if your query (UPDATE) was truly successful, use mysqli_affected_rows().

In not doing so, you may get a false positive.

Nota:

Your present code is open to SQL injection. Use prepared statements, or PDO with prepared statements.

5 Comments

Yes, I was told too that my query was incorrect. Thank you so much for this.
@WandaMaximoff I added a few more things to my answer which will be of help.
@WandaMaximoff I figured I would post an answer for future visitors to the question also.
Thank you for the links. I definitely need to review mysql syntax!! The links are so helpful!
@WandaMaximoff I believe I answered the real problem here but you're welcome.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.