php HTML 输入值更改
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5102487/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HTML input value change
提问by Wazan
I have a PHP update page in which I am showing a text field containing a value from the database. It is like this, and it is working,
我有一个 PHP 更新页面,我在其中显示了一个包含数据库值的文本字段。它是这样的,它正在起作用,
<input type="text" name="title" id="title" class="text_box" value="<?php echo $row['title']?>"/>
Now I need to put this updated value back in the database! I have used the code like this, but it's not updating:
现在我需要将这个更新后的值放回数据库中!我已经使用了这样的代码,但它没有更新:
$title=$_POST['title'];
$v_id = $_GET['v_id'];
$sql = mysql_query("update vehicles set title = '$title' where v_id = '$v_id'");
In detail... an input field is there. It's showing a value contained in $title
(retrieved from the database) and that value is to be edited and updated.
详细地说......有一个输入字段。它显示了包含在$title
(从数据库中检索)中的值,并且该值将被编辑和更新。
From my side my code is working perfectly without showing any error, but the value that I give $title
is giving the same one without any change.
从我的角度来看,我的代码运行良好,没有显示任何错误,但我给出的值$title
是在没有任何更改的情况下给出相同的值。
Is there any other way to show a value in an input field without putting in a "value" tag? Two things wants to happen in a single input field!
有没有其他方法可以在不放入“值”标签的情况下在输入字段中显示值?有两件事要在单个输入字段中发生!
回答by JohnP
You'll need to post your form HTML as well.
您还需要发布表单 HTML。
Unless your form looks like the following, that code won't work
除非您的表单如下所示,否则该代码将不起作用
<form method='post' action='page.php?v_id=1'>
<input type="text" name="title" id="title" class="text_box" value="<?php echo $row['title']?>"/>
</form>
This is because you're using $_GET to get the id field and $_POST to get the value field
这是因为您使用 $_GET 来获取 id 字段和 $_POST 来获取值字段
EDITYour edit has muddied the water a bit more. I'm going to assume all you want to do is show a title and let the user update the title
编辑您的编辑使水变得更加混乱。我将假设您想要做的就是显示标题并让用户更新标题
<?php
if ($_POST) {
$title = mysql_escape_string($_POST['title']);
$id = intval($_GET['v_id']);
$sql = "update vehicles set title = '$title' where v_id = '$id'";
mysql_query($sql) or trigger_error(mysql_error()." ".$sql);
}
if (isset($_GET['v_id'])) {
$id = intval($_GET['v_id']);
$sql = 'SELECT title FROM vehicles WHERE v_id = ' . $id;
$rs = mysql_query($sql) or trigger_error(mysql_error()." ".$sql);
$row = mysql_fetch_assoc($rs);
$title = htmlspecialchars($row['title']);
}
?>
<form method='post' action='?v_id=<?php echo $id?>'>
<input type="text" name="title" id="title" class="text_box" value="<?php echo $title ?>"/>
<input type='submit' value='update'>
</form>
This should work for you. I haven't error tested obviously, but the idea is sound. You should also add any other input screening you feel necessary.
这应该对你有用。我没有明显的错误测试,但这个想法是合理的。您还应该添加您认为必要的任何其他输入筛选。
回答by Veger
You are using both $_POST
and $_GET
.
Depending on the method
element value of <form>
, you need either $_POST
or $_GET
.
您同时使用$_POST
和$_GET
。根据 的method
元素值<form>
,您需要$_POST
或$_GET
。
If this is not helping, please show more code so it is possible to determine which one you need.
如果这没有帮助,请显示更多代码,以便确定您需要哪一个。
Also, you can try to update the database without reading the form, to check if the updating itself is working correctly.
此外,您可以尝试在不阅读表单的情况下更新数据库,以检查更新本身是否正常工作。
回答by Your Common Sense
is there any other way to showing a value in a input field without putting a "value" tag?
有没有其他方法可以在不放置“值”标签的情况下在输入字段中显示值?
Nope.
That's exactly what value
attribute for.
What's wrong with it?
不。
这正是value
属性的作用。
它出什么问题了?
only thing to mention, it should be not <?php echo $row['title']?>
but
唯一要提的是,它应该不是,<?php echo $row['title']?>
但
<?php echo htmlspecialchars($row['title'])?>