简体   繁体   English

当用户更新文本框值时更新mysql

[英]update mysql as user updates textbox value

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. 直到知道我能够使用ajax和json从数据库的下拉列表中选择的名称基础上,在文本框中获取并显示值。 but when i try to update database it do not works... 但是当我尝试更新数据库时不起作用...

HTML: 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 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);
            }
?>

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. 您应该像上面的示例一样使用WHERE语句,以确保可以更新正确的行。

You should also consider using mysql_real_escape_string function: 您还应该考虑使用mysql_real_escape_string函数:

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

http://php.net/manual/en/function.mysql-real-escape-string.php http://php.net/manual/zh/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. 如果这将是更新表格,则还应该在输入字段等中包含value ,以便他们看到值并更新所需的内容。

I also suggest that you use mysqli functions instead of mysql functions as mysql is no longer supported and deprecated. 我还建议您使用mysqli functions而不是mysql functions因为不再支持和弃用mysql

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM