1

I have this code here which is supposed to help me update a phone number. It doesn't do it though, well, i get the successfully changed message but no insertion on the database.

Here is my code: index.php

<script type="text/javascript" >
$(function() {
    $(".submit").click(function() {
        var phone = $("#phone").val();

        var dataString = 'phone='+ phone ;

        if(phone=='') {
            $('.success').fadeOut(200).hide();
            $('.error').fadeOut(200).show();
        } else {
            $.ajax({
                type: "POST",
                url: "update-phone.php",
               data: dataString,
               success: function() {
                   $('.success').fadeIn(200).show();
                   $('.error').fadeOut(200).hide();
               }
           });
       }
       return false;
   });
});
</script>
<div class="modal" style="display: none;">
<?php
if (empty($phone)) {
?>
<form method="post" name="form">
    <input id="phone" name="phone" type="text" />
    <div>
        <input type="submit" value="Submit" class="submit"/>
        <span class="error" style="display:none"> Please Enter Valid Data</span>
        <span class="success" style="display:none"> Registration Successfully</span>
    </div>
</form>

update-phone.php

<?php 
require_once('db.php'); 

if($_POST) {
    $phone = $_POST['phone'];
    mysql_query("UPDATE users SET phone = '$phone' WHERE ID = 5884 ");
}else {}

?>

What am i missing? Thanks

rGil
  • 3,719
  • 1
  • 22
  • 30
al_alb
  • 39
  • 5
  • echo your query and run in phpmyadmin see if there is any error and also check your db connection – M Khalid Junaid Jun 05 '13 at 18:47
  • I need more information before I could give you an answer. What is in $_POST (is it sending the data you want). You should also not be using if($_POST)... $_POST is not a boolean, it's an array. Try isset() – paquettg Jun 05 '13 at 18:47
  • Do you ever connect to the database? From the code you posted it seems like you're just throwing a query to the skies and seeing what it will do. – Timr Jun 05 '13 at 18:48
  • Also: http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php – Timr Jun 05 '13 at 18:49
  • it's like i need to click twice for it to take the value... it takes the value only if i refresh the page, bho.. – al_alb Jun 05 '13 at 18:58
  • 2
    From previous comment, it seems probable that the is indeed saved in the DB, you just don't refresh your front-end to display the new phone (so you need to explicitely reload the page for it to be shown). In your AJAX call's "success" callback, you need to update any necessary HTML elements, in order to reflect the changes. – gkalpak Jun 05 '13 at 19:15

2 Answers2

2

Try this..

<?php 
require_once('db.php'); 

if($_POST) {
    $phone = $_POST['phone'];
    mysql_query("UPDATE `users` SET `phone` = '$phone' WHERE `ID` = 5884 ");
}else {}

?>
Pank
  • 13,800
  • 10
  • 32
  • 45
0

Have you tried inspecting the ajax request with firebug/developer tools/etc? Try adding echo mysql_error(); right after your mysql_query.

Not 100% sure but it could be the if ($_POST) should be replaced with if (isset($_POST['phone']))

Try the following php:

<?php
require_once('db.hp');
if (isset($_POST['phone']))
{
  $phone = $_POST['phone'];
  mysql_query("UPDATE users SET phone = '$phone' WHERE ID = 5884 ");
  echo mysql_error();
} 
else
{
  echo "Failed";
}
?>

EDIT: Have you confirmed if it actually updates the DB? Also you should also sanitise your input and consider rolling with mysqli instead of mysql_*

ohej
  • 243
  • 1
  • 7
  • you don't want to check if $_POST['phone'] is set, you want to check if $_POST is set. Checking $_POST['phone'] will cause a warning saying that $_POST['phone'] is not set since it will try to get the value before passing it to isset. – paquettg Jun 05 '13 at 18:49
  • Wouldn't $_POST always be set? Also, how could checking isset($_POST["phone"]) trigger a warning? – ohej Jun 05 '13 at 19:02