从包含的文件中提交PHP表单运行功能

时间:2012-10-13 17:14:11

标签: php mysql smarty

对不起基本问题。我在Stackoverflow上看了其他答案,但是这里有:

我有一个简单的表格,应该将数据发布到我的mysql DB:

<form id="theatre" method="post" action="{$HTTP_HOST}admin/addTheatre.php"     autocomplete="on">
    <input type="hidden" name="id">
    <fieldset>
        <label>Please fill in the relevent fields.</label>
        <section>
            <label for="year">Year</label>
            <div>                   
                <select name="year" id="year">
                    <optgroup label="Year">
                            <option>2013</option>
                            <option>2012</option>
                            <option>2011</option>
                            <option>2010</option>
                            <option>2009</option>
                            <option>2008</option>
                            <option>2007</option>
                            <option>2006</option>
                    </optgroup>
                </select>
            </div>
        </section>
        <section><label for="part">Part</label>
            <div>
                <input type="text" id="part" name="part" title="A Tooltip">
            </div>
        </section>
        <section><label for="name">Name</label>
            <div>
                <input type="text" id="name" name="name" title="A Tooltip">
            </div>
        </section>
        <section><label for="theatre">Theatre</label>
            <div>
                <input type="text" id="theatre" name="theatre" title="A Tooltip">
            </div>
        </section>
        <section><label for="director">Director</label>
            <div>
                <input type="text" id="director" name="director" title="A Tooltip">
            </div>
        </section>
        <section>
            <div>
                <button name="submit" class="submit" type="submit">Submit</button>
            </div>
        </section>
    </fieldset>
</form>

并在addTheatre.php文件中我有:

if(isset($_POST['submit'])){
    hcDB::getInstance()->insert_theatre($_POST['year'], $_POST['part'], $_POST['name'], $_POST['theatre'], $_POST['director']);
}

我应该运行以下功能:

function insert_theatre($year, $part, $name, $theatre, $director) {
    $year = $this->real_escape_string($year);
    $part = $this->real_escape_string($part);
    $name = $this->real_escape_string($name);
    $theatre = $this->real_escape_string($theatre);
    $director = $this->real_escape_string($director);
    $this->query("INSERT INTO theatre (`year`, `part`, `name`, `theatre`, `director`)" .
            " VALUES (" . $year . "' , '" . $part . "' , '" . $name . "' , '" . $theatre . "' , '" . $director . "')");
    }

据我所知,没有提交任何内容。正如你可能会说的那样,我是新手,为无能而道歉!

提前感谢您的帮助。

3 个答案:

答案 0 :(得分:0)

检查query的返回值并致电errorInfo以查看您是否收到任何有意义的消息。

它应该告诉你SQL格式错误:

$this->query("INSERT INTO theatre (`year`, `part`, `name`, `theatre`, `director`)" .
        " VALUES (" . $year . "' , '" . $part . "' , '" . $name . "' , '" . $theatre . "' , '" . $director . "')");
}

AFAICT VALUES ("应更改为VALUES('"(额外的单引号)。

答案 1 :(得分:0)

你的查询中有一个拼写错误,没有错误处理,所以你永远不会看到mysql会提供的错误消息:

" VALUES ('" . $year . "' , '"
          ^--- missing quote

答案 2 :(得分:0)

正如你所说,没有提交任何内容,那么你的按钮可能没有价值而且你想要做

if(isset($_POST['submit'])){
//other stuff
}

它最终是错误的。

因此,请尝试更改按钮

<button name="submit" class="submit" type="submit">Submit</button>

<input type="submit" class="submit" name="submit" value="Submit"/>