PHP/MySQL 查询如果“action”被忽略是在表格中设置的

发布于 2024-10-16 21:10:53 字数 927 浏览 1 评论 0原文

我基本上有:

<form action="index.php" method="post">
    <input name="text" type="text" value="Insert text here" size="20"/>
    <input name="submit" type="submit" value="Submit" />
</form>

我的 PHP 代码然后检查是否按下了提交:

if(isset($_POST['submit'])) {
    $newDbValue = $_POST['text'];
    $sql = ("
        UPDATE `pseudo_tableName` 
        SET TEXT = '".$newDbValue."' 
        WHERE name = 'pseudo_fieldName' LIMIT 1
    ");
    //SQL-query run by php function in separate class.
}

据我了解,如果提交了表单数据,它会将用户发送回 index.php

但我的做法是无法使用新值更新并将我发送回index.php,就好像没有任何问题一样!

如果我在表单中留下 action=""action=""它会在我重新加载页面时更新 (不是 F5 - 单击地址行并按 Enter 键)。

我想要的: 我点击提交,它更新了数据库,将我发送到index.php。

如何才能实现这一目标?

I basically have:

<form action="index.php" method="post">
    <input name="text" type="text" value="Insert text here" size="20"/>
    <input name="submit" type="submit" value="Submit" />
</form>

My PHP code then checks if submit is pressed:

if(isset($_POST['submit'])) {
    $newDbValue = $_POST['text'];
    $sql = ("
        UPDATE `pseudo_tableName` 
        SET TEXT = '".$newDbValue."' 
        WHERE name = 'pseudo_fieldName' LIMIT 1
    ");
    //SQL-query run by php function in separate class.
}

And as I understand it, if the form data is submitted it sends the user back to index.php?

But what mine does is it fails to update with new values and sends me back to index.php as if there was nothing wrong!

If I leave action="<?php $PHP_SELF; ?>" or action="" in the form, it updates when I reload the page (not F5 - click in address-line and hit enter).

What I want this to be:
I hit submit, and it updates the DB, sends me to index.php.

How can this be achieved?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

很酷又爱笑 2024-10-23 21:10:53

删除表单中的 action="" (使您重新加载页面并使用 if 中的相同文件)并将 if 更改为这:

if(isset($_POST['submit'])) {
    $newDbValue = $_POST['text'];
    $sql = ("
            UPDATE `pseudo_tableName` 
            SET TEXT = '".$newDbValue."' 
            WHERE name = 'pseudo_fieldName' LIMIT 1
    ");
    header("Location: index.php");
    exit;
}

Remove action="" (making you reload the page and using same file you have you if in) in your form and change your if to this:

if(isset($_POST['submit'])) {
    $newDbValue = $_POST['text'];
    $sql = ("
            UPDATE `pseudo_tableName` 
            SET TEXT = '".$newDbValue."' 
            WHERE name = 'pseudo_fieldName' LIMIT 1
    ");
    header("Location: index.php");
    exit;
}
一桥轻雨一伞开 2024-10-23 21:10:53

所有

元素都需要 action 属性。您所描述的可能意味着您的 PHP 脚本没有检测到表单数据,因此没有对您的 MySQL 数据库进行必要的更改。其背后的原因可能取决于您使用的浏览器;据我了解,如果您按 Enter(键盘),提交按钮 value 不会包含在表单中(据说这允许多个具有不同值的提交按钮并执行正确的功能)。

如果我是你,我会检查表单中的实际文本输入,而不是按钮。

<?php
// check 'text' exists and make sure it isn't blank.
if(isset($_POST['text']) && $_POST['text']!='') {
  // do MySQL updating here.
  header("Location: index.php"); // send back to index.php after updating.
}
?>

因为最有效的重定向方法是使用 header();,因此您需要在 HTML 表单之前编写 PHP 表单检查脚本(因为标头不能发送到实际文档的一半),因此可能在发送 HTML 之前在页面顶部,此外,这样您可以根据处理后的数据控制发送哪些 HTML。

All <form> elements require the action attribute. What you're describing probably means your PHP script isn't detecting the form data and thus isn't making the necessary changes to your MySQL database. The reasons behind this may depend on the browser you're using; to my understanding, if you press Enter (keyboard) the submit button value isn't included in the form (this is said to allow multiple submit buttons with different value and perform the correct function).

If I were you, I'd check for the actual text input in the form, not the button.

<?php
// check 'text' exists and make sure it isn't blank.
if(isset($_POST['text']) && $_POST['text']!='') {
  // do MySQL updating here.
  header("Location: index.php"); // send back to index.php after updating.
}
?>

Because the most efficient method to redirect is to use header(); you're required to write your PHP form-checking script before the HTML form (because headers can't be send half-way through the actual document) so maybe at the top of the page before sending the HTML, additionally that way you can control what HTML is sent based on the processed data.

喜爱纠缠 2024-10-23 21:10:53

您是否尝试过设置:

<?php
    if (isset($_POST['text']) AND !empty($_POST['text'])) {
        // ...
    }
?>

而不是其他输入?

action='index.php' 会将您带回index.php,因此请确保您的PHP 位于表单之上(就此而言,还有您的HTML)。我还猜测您已经设置了 mysql_connect 和 mysql_query 函数。

你的 php.ini 里有 error_reporting 吗?当您对变量进行 var_dump() 时,MySQL 查询结果集会显示什么?

<?php
    $sql = mysql_query($newDbValue));
    var_dump($sql);
?>

这确实可以帮助你。

“无法发送标头信息 - 标头已发送”是因为在重定向()函数之前您已经将字符发送到浏览器 - 如果您要使用它,则没有任何内容可以发送到浏览器(甚至是空格!),所以请确保文档中的第一件事是你的?>标签,并且您的文件编码是 UTF-8,而不是 UTF-16 BOM 或类似的疯狂编码。

Have you tried setting:

<?php
    if (isset($_POST['text']) AND !empty($_POST['text'])) {
        // ...
    }
?>

Instead of the other input?

The action='index.php' will send you back to index.php, so be sure that your PHP is above the form (and your HTML, for that matter). I also gues that you have set up your mysql_connect and you mysql_query functions.

Do you have error_reporting in php.ini on? What does the MySQL query result set say when you var_dump() the variable?

<?php
    $sql = mysql_query($newDbValue));
    var_dump($sql);
?>

That could really help you out.

The "cannot send header information - headers alredy sent" is because you have characters sent to the browser before the redirect() function - nothing can be sent to the browser (even whitespace!) if you're gonna use that, so be sure that the first thing in the document is your ?> tag, and your file encoding is UTF-8, not UTF-16 BOM or something crazy like that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文