0

My test.php page is as under:

<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
    <h3>Setting</h3>
    <form>
        <p>Name:<input type="text" id="updatetherone1"/></p>
        <p>Email:<input type="text" id="updateotherone2"/></p>
        <p><input id="updateone" type="button" value="Save"/></p>
    </form>
    <span id="updateotherotherone"></span>
    <script src="js/jquery.js"></script>
    <script src="js/ajax.js"></script> 
</body>
</html>

My ini.php page is as under:

<?php
  session_start();
  $_SESSION['state'] ='2';
  $conn=new mysqli('localhost','root','','people');
?>

My test1.php page is as under:

<?php
  include 'ini.php';

  if (isset($_POST['name'], $POST['email'])){
    $name = mysqli_real_escape_string(htmlentities($POST['name']));
    $email = mysqli_real_escape_string(htmlentities($POST['email']));

    $update = mysqli_query("UPDATE state SET Name='$name', email='$email' WHERE Id=".$_SESSION['state']);

    if($update === true){
        echo 'Setting have been updated.';
    }else if ($update === false){
        echo 'There was an error updating your setting.';
    }
  }
?>

My ajax.js page is as under:

$('[id=updateone]').click(function(){
  var name=$('[id=updateotherone1]').val();
  var email=$('[id=updateotherone2]').val();
  $('[id=updateotherotherone]').text('Loading...');

  $.post('test1.php',{name:name,email:email},function(data){
    $('[id=updateotherotherone]').text(data);
  });
});

Ultimately code is not working nor do it is displaying any error, I suspect there is something wrong with test1.php page, can anybody guide please:

2
  • 1
    $POST that is invalid. They are all missing an underscore. Commented Jan 2, 2015 at 14:22
  • 1
    Protip: You should make it a habit to use underscores for better readability. "update_other_one_1" is easier to read and catch typos then "updateotherone1" ;-) You do have another typo in there. Commented Jan 2, 2015 at 14:45

1 Answer 1

1

Take note that the procedural interface of mysqli_query(), the first parameter need the connection.

$update = mysqli_query($conn, "UPDATE state SET Name='$name', email='$email' WHERE Id=".$_SESSION['state']);

If these are typos $POST, then it should be fixed in your code. It's supposed to read as $_POST. (unless its a typo on the question.) It is a superglobal.

I suggest you just use the object oriented interface. So that you wouldn't need to add it everytime:

<?php
include 'ini.php';

if (isset($_POST['name'], $_POST['email'])){
    $name = $conn->real_escape_string(htmlentities($_POST['name']));
    $email = $conn->real_escape_string(htmlentities($_POST['email']));

    $update = $conn->query("UPDATE state SET Name='$name', email='$email' WHERE Id = " . $_SESSION['state']);

    if($conn->affected_rows > 0) {
        echo 'Setting have been updated.';
    } else {
        echo 'There was an error updating your setting.';
    }
}
?>

Might as well use prepared statements since mysqli supports it:

if (isset($_POST['name'], $_POST['email'])){
    $name = htmlentities($_POST['name']);
    $email = htmlentities($_POST['email']);

    $sql = 'UPDATE state SET Name = ?, email = ? WHERE Id = ?';
    $update = $conn->prepare($sql);
    $update->bind_param('ssi', $name, $email, $_SESSION['state']);
    $update->execute();

    if($update->affected_rows > 0) {
        echo 'Setting have been updated.';
    } else {
        echo 'There was an error updating your setting.';
    }
}

On the JS part:

You also have a typo on the form id and the JS:

<p>Name:<input type="text" id="updatetherone1"/></p> <!-- missing o -->
              var name=$('[id=updateotherone1]').val();

Should be: <p>Name:<input type="text" id="updatetherone1"/></p>

Sidenote:

If you want to avoid those kind of silly typos, just id and label them properly. Example:

<p>Name:<input type="text" id="name_field"/></p>
<p>Email:<input type="text" id="email_field"/></p>
Sign up to request clarification or add additional context in comments.

5 Comments

Also look at my comment under OP's question. Not just for email.
Plus, missing DB connection to mysqli_real_escape_string(). So many things wrong with OP's code. I am not touching this lol
@Fred-ii- yeah too much to tinker with. my mistake there are errors everywhere including JS
Good catch on the typo "updatetherone1". For some reason, I was looking at the JS and something seemed out of whack. You and I know, it's best to seperate words with underscores "update_other_one_1" etc. It's so easy to make a mistake when words are clustered up like that.
@Fred-ii- good point, i just made sure the forms as js were okay and by testing, they weren't lol.

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.