php - 使用表单 php>form>php 传递变量时出错

标签 php mysql insert

请问这段代码有什么问题?

我用它来向数据库中添加一些数据,但在尝试插入时我得到的是空的 $toid$toname

这是表格。变量 $toid$toname 在这里没问题。

//write new message
if (isset($_GET['action']) && $_GET['action'] == compose) { 
        if (isset($_GET['toid'])) {
            $toid = $_GET['toid'];
            $tosql = "select * from authors where id =".$toid.""; 
            $toquery = mysql_query($tosql,$connection) or die(mysql_error());
            $torow = mysql_fetch_array($toquery);   
            $toname = $torow['displayname'];
        if (isset($_GET['subject'])) { $subject = $_GET['subject']; }
        if (isset($_GET['message'])) { 
            $message = $_GET['message']; 
            echo "<h3>Replying</h3>
            <table>
                <tr>
                    <td colspan='2'>Replying to ".$toname.".</td>
                </tr>
                <tr>
                    <td colspan='2'>".$subject."".nl2br($message)."<br />
                    </td>
                </tr>
            </table><br />Type your answer:<br /><br />";
        } else { echo "New message"; }
        echo "<form action=\"mail.php?action=send\" method=post>
            <table>
                <tr>
                    <td>To:</td><td><input type=\"text\" name=\"to\" size=\"50\" value=\"".$toname."\"></td>
                </tr>
                <tr>
                    <td>Title:</td><td><input type=text name=subject size=50 value=".$subject."></td>
                </tr>
                <tr>
                    <td valign=\"top\">Message:</td><td><textarea  rows=\"10\" cols=\"70\" name=\"message\"></textarea></td>
                </tr>
                <tr>
                    <td align=\"right\" colspan=\"2\"><input id=\"submitstyle\" type=\"submit\" value=\"Enviar Mensagem\"></td>
                </tr>
            </table>
        </form>"; 
        }
} 

这是将消息插入数据库的代码,$toid$toname 在这里是空的。它应该是从上面的表格中检索的,对吧?

//send message  
if (isset($_GET['action']) && $_GET['action'] == send) { 

    if ($subject == "" || $message == "") {
        header('Location: mail.php?action=compose&toid='.$toid.'&subject=\''.$subject.'\'&sendpm=false');
        exit(); 
    }

    $date = DATE(YmdHis); 

    echo $userid."from<br />to".$toid."<br />toname".$toname;

    $sendsql = "INSERT INTO mail (sender, reciever, subject, message, created_at, status, sender_deleted, reciever_deleted) 
                        VALUES (".$userid.", ".$toid.", ".$subject.", ".$message.", ".$date.",unread, 0, 0)"; 
    $sendquery = mysql_query($sendsql,$connection) or die(mysql_error());

    echo "<div class=\"alert\" style=\"text-align: center; margin-top: 13px;\"><b>Mensagem particular enviada com sucesso!</b></div>
            <br /><table align=\"center\" width=\"75%\" class=\"sortable\">
                <tr>
                    <td colspan='2' style=\"text-align:center;font-weight:normal;\">Mensagem particular enviada para ".$toname.".</td>
                </tr>
                <tr>
                    <td colspan='2'>
                        Title: ".$subject."
                        Message: ".nl2br($message)."

                    </td>
                </tr>
            </table>";
}

我也收到这个 sql 错误 你的 SQL 语法有错误;检查与您的 MySQL 服务器版本对应的手册,了解在第 2 行的“RE: assunto3, 3, 20121017023723,unread, 0, 0)”附近使用的正确语法,我认为这是因为提到的空变量.

最佳答案

首先,您需要将这些变量作为隐藏表单值传递:​​http://www.echoecho.com/htmlforms07.htm

然后您需要通过 $_POST 从表单中获取变量。

试试 $toid = $_POST['toid'] 和 $toname = $_POST['toname']。但要小心 SQL 注入(inject):http://php.net/manual/en/security.database.sql-injection.php

不要只是盲目地接受来自 $_POST 的值。请务必先验证和过滤它们。

或者如果用户不能更改 toid/toname,为什么不重新查询它们呢?

关于php - 使用表单 php>form>php 传递变量时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12927972/

相关文章:

php - 带有 jQ​​uery 动态内容的 SEO 安全 anchor 链接

mysql - MVC3 - 无法将长文本输入数据库

mysql - 使用 ASP.Net 插入 MySQL 时出错

java - Springs SimpleJdbcInsert 不会按预期生成自动生成的键

php - sql php函数中的插入查询

php - 快速远程 mysql 连接的最佳方法/设置是什么?

php - 如何只返回 XPath 中的第一个匹配结果?

php - 如何在 PHP 中将守护进程变成流

c# - 使用 ASPNETDB.MDF 添加到 c# asp.net 中的表

Java插入具有自动增量字段的表