0

actualy im just a newbie programmer and i made my own update javascript function for my program but sadly the codes doesnt update..can anyone help me assist in order my codes work? please.

i want to do is if i change the values in the textboxes and click update it will update.

enter image description here

script code:

<script type="text/javascript">
$(document).ready(function () {
       $('#updates').click(function (e) {

           e.preventDefault();
           var $id1 = $('#id1').val();
           var $id2 = $('#id2').val();
           var $name1 = $('#name1').val();
           var $name2 = $('#name2').val();
           var $optA1 = $('#optA1').val();
           var $optA2 = $('#optA2').val();
           var $optB1 = $('#optB1').val();
           var $optB2 = $('#optB2').val();
           var $other_qual1 = $('#other_qual1').val();
           var $other_qual2 = $('#other_qual2').val();
           var $interview1 = $('#interview1').val();
           var $interview2 = $('#interview2').val();
           var $total1 = $('#total1').val();
           var $total2 = $('#total2').val();

           $.ajax({
               type: "POST",
               url: "update.php",
               data: {
                   id1_text: $id1,
                   id2_text: $id2,
                   name1_text: $name1,
                   name2_text: $name2,
                   optA1_text: $optA1,
                   optA2_text: $optA2,
                   optB1_text: $optB1,
                   optB2_text: $optB2,
                   other_qual1_text: $other_qual1,
                   other_qual2_text: $other_qual2,
                   interview1_text: $interview1,
                   interview2_text: $interview2,
                   total1_text: $total1,
                   total2_text: $total2
               },
               cache: false,
               success: function (data) {
                   alert('data has been updated!');
               }
           });
       });
   });
</script>

update.php page:

<?php

    mysql_connect("localhost", "root", "") or die("cant connect to database!");
    mysql_select_db("test") or die("cant find database!");
    $id1         = @$_POST['id1_text'];
    $id2         = @$_POST['id2_text'];
    $name1       = @$_POST['name1_text'];
    $name2       = @$_POST['name2_text'];
    $optA1       = @$_POST['optA1_text'];
    $optA2       = @$_POST['optA2_text'];
    $optB1       = @$_POST['optB1_text'];
    $optB2       = @$_POST['optB2_text'];
    $other_qual1 = @$_POST['other_qual1_text'];
    $other_qual2 = @$_POST['other_qual2_text'];
    $interview1  = @$_POST['interview1_text'];
    $interview2  = @$_POST['interview2_text'];
    $total1      = @$_POST['total1_text'];
    $total2      = @$_POST['total2_text'];

    $query1 = mysql_query("UPDATE score SET name=$name1, score1=$optA1, score2=$optB1, other_qual=$other_qual1, interview=$interview1, total=$total1 WHERE id=$id1");
    $resource1 = mysql_query($query1) or die(mysql_error());

    $query2 = mysql_query("UPDATE score SET name=$name2, score1=$optA2, score2=optB2, other_qual=$other_qual2, interview=$interview2, total=$total2 WHERE id=$id2");
    $resource2 = mysql_query($query2) or die(mysql_error());


?>

html:

<form>
    Search batchcode: <input id="query" name="search" type="text"><input id="send_search_form" type="button" value="Go" /><br>
Batch #: <label id="batchcode" class="empty_batchcode"></label>
    <table>
        <tr>
            <td>ID:<br>
            <input readonly id="id1" class="search_form_input" name="id1" type="text"><br>
            <input readonly id="id2" class="search_form_input" name="id2" type="text"><br></td>

            <td>Name:<br>
            <input id="name1" class="search_form_input" name="name1" type="text"><br>
            <input id="name2" class="search_form_input" name="name2" type="text"><br></td>

            <td>Score 1:<br>
            <input id="optA1" class="search_form_input" name="optA1" type="text"><br>
            <input id="optA2" class="search_form_input" name="optA2" type="text"><br></td>

            <td>Score 2:<br>
            <input id="optB1" class="search_form_input" name="optB1" type="text"><br>
            <input id="optB2" class="search_form_input" name="optB2" type="text"><br></td>

            <td>Other Qualification:<br>
            <input id="other_qual1" class="search_form_input" name="other_qual1" type="text"><br>
            <input id="other_qual2" class="search_form_input" name="other_qual2" type=
            "text"><br></td>

            <td>Interview:<br>
            <input id="interview1" class="search_form_input" name="interview1" type="text"><br>
            <input id="interview2" class="search_form_input" name="interview2" type="text"><br></td>

            <td>Total:<br>
            <input id="total1" class="search_form_input" name="total1" type="text"><br>
            <input id="total2" class="search_form_input" name="total2" type="text"><br></td>
        </tr>
    </table>
    <input type="button" value="update" id="updates" />
</form>
2
  • What exactly doesn't update? Your database? The form elements (you didn't write any code that updates the elements, so of course they cannot update)? Please explain your problem properly. We cannot look into your head. Commented Feb 3, 2014 at 5:18
  • And post your HTML as well. Commented Feb 3, 2014 at 5:20

2 Answers 2

2

You're missing quotes around the name in your SQL query.

$query1 = mysql_query("UPDATE score SET name='$name1', score1=$optA1, score2=$optB1, other_qual=$other_qual1, interview=$interview1, total=$total1 WHERE id=$id1");

It would be better if you used PDO or mysqli, so you could use parametrized queries instead of string substitution. Your code is in serious danger of SQL injection.

Sign up to request clarification or add additional context in comments.

Comments

1

Neither of these will work.

$resource1 = mysql_query($query1) or die(mysql_error());
$resource2 = mysql_query($query2) or die(mysql_error());

Because the referenced $query1 and $query2 are the actual query. You need to change them to simple strings.

Corrected PHP:

$query1 = "UPDATE score SET name=$name1, score1=$optA1, score2=$optB1, other_qual=$other_qual1, interview=$interview1, total=$total1 WHERE id=$id1";
$resource1 = mysql_query($query1) or die(mysql_error());

$query2 = "UPDATE score SET name=$name2, score1=$optA2, score2=optB2, other_qual=$other_qual2, interview=$interview2, total=$total2 WHERE id=$id2";
$resource2 = mysql_query($query2) or die(mysql_error());

Or even better, update to something more current, like so:

$mysqli = new mysqli("localhost", "root", "", "test");
if ($mysqli->connect_errno) {
    echo "Failed to connect to MySQL: (Error code: " . $mysqli->connect_errno . ")... " . $mysqli->connect_error;
}
$query1 = "UPDATE score SET name='$name1', score1='$optA1', score2='$optB1', other_qual='$other_qual1', interview='$interview1', total='$total1' WHERE id='$id1'";
$resource1 = $mysqli->query($query1) or die(mysqli_error($mysqli));
$query2 = "UPDATE score SET name='$name2', score1='$optA2', score2='$optB2', other_qual='$other_qual2', interview='$interview2', total='$total2' WHERE id='$id2'";
$resource2 = $mysqli->query($query2) or die(mysqli_error($mysqli));
mysqli_close($mysqli);

@Barmar also has a point - you need quotes around those variables in case the have spaces or other characters that might result in syntax error.

5 Comments

k il pOSt the html...i change the query as your advise still it wont update.
did you use the first set of PHP I posted or the set I just updated with mysqli?
nvm the query just need '' on query values its working now thanks :D
well I'm glad it works now but that was from @Barmar you should select his answer instead.
i used ur corrected php code...do you still have time? can you help me that if the its not updated it will say "failed to update!"

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.