0

i want to update value(s) as a student change value(s) in correction or update form.
till know i'm able to fetch and display values in text boxes on the bases of name selected from dropdown list from data base using ajax and json. but when i try to update database it do not works...

HTML:

<select name="u_stu" id="u_stu" onchange="show(this.value);" style="float:right; height:30px; width:180px;">
        <option selected="selected" disabled="disabled">Choose</option>
        <option>stu1</option>
        <option>stu2</option>
        <option>stu3</option> 
</select>
 name: <input type="text" id="name" name="name" style="float:right; height:20px; width:200px;"/><br /><br />
 age:   <input type="text" id="age" name="age" style="float:right;  height:20px; width:200px;" /><br /><br />
 phone: <input type="text" id="phone" name="u_ver_txt" style="float:right;  height:20px; width:200px;" /><br /><br />
 address:    <input type="text" id="add" name="add" style="float:right;  height:20px; width:200px;" /><br /><br />
 hobby:   <input type="text" id="hobby" name="hobby" style="float:right;  height:20px; width:200px;" /><br /><br />
 <input type="submit" value="Submit" name="u_s2" id="u_s2" style="position:relative; top:-180px; "/>

MYSQL PHP

<?php           
        $c=mysql_connect("localhost","abc","xyz");
        mysql_select_db("root");
            if(isset($_POST['u_s2']))
    {
        $name=$_POST["name"];
        $age=$_POST["age"];
        $phone=$_POST["phone"];
        $address=$_POST["address"];
        $hobby=$_POST["hoddy"];
            $id=$_POST["u_id"];
        $q2="UPDATE student SET 
                    name=$name,age=$age,phone=$phone,address=$address,hobby=$hobby WHERE Sr. no=$id";
                    mysql_query($q2);
            }
?>
4
  • 1
    I don't see any update statement in your code. Commented Jul 13, 2013 at 0:15
  • You cant mix POST & GET Commented Jul 13, 2013 at 0:23
  • ITS still not working @Simon _eQ Commented Jul 13, 2013 at 0:28
  • add or die(mysql_error()) and turn on error reporting to give you a better idea of what's going on. This doesn't look valid WHERE Sr. no=$id Commented Jul 13, 2013 at 0:33

1 Answer 1

1

You need something like this:

$q= "update student set name = '".$name."', age = '".$age."', phone = '".$phone."', address = '".$address."', hobby = '".$hobby."' WHERE user = 'the user id'";

You should use a WHERE statement as well like the above example so that you can be sure that you are updating the correct row.

You should also consider using mysql_real_escape_string function:

$name = mysql_real_escape_string($_POST['name']);

http://php.net/manual/en/function.mysql-real-escape-string.php

If this will be an updating form you should also include the value in the input fields and etc. so they will see the values and update what they need.

I also suggest that you use mysqli functions instead of mysql functions as mysql is no longer supported and deprecated.

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

Comments

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.