duanhuan1147 2014-01-10 00:41
浏览 12
已采纳

无法获取表单发布

I'm having trouble getting the information submitted in my form to post into my database. I'm not sure what the issue is, no error is logged, but I get the message "There was an error that prevented the comment from being saved." which is what I set up as my failure message in my php code, so something's not working.

I've made a form like this only once before, so I'm not great at troubleshooting. I have three things that need to be submitted when the form posts: author, body, and the page_name (which is a hidden input type in the form that sends the current web address).

Here is the php code that submits the form:

<?php 
if (isset($_POST['submit'])) {
$required_fields = array("author", "body");
validate_presences($required_fields);

if (empty($errors)) {
    $author = mysql_prep($_POST['author']);
    $body = mysql_prep($_POST['body']); 
    $page_name = ($_POST['page_name']);

    $query  = "INSERT INTO comments (";
    $query .= "  $author, $body, $page_name";
    $query .= ") VALUES (";
    $query .= "  '{$author}', '{$body}', '{$page_name}'";
    $query .= ")";
    $result = mysqli_query($connection, $query);

    if ($result) {
        redirect_to("new_admin.php");
    } else {
            // Failure
            $_SESSION["message"] = "There was an error that prevented the comment from being saved.";
    }
}
} else {
    $author = "";
    $body = "";
}
?>

Here's the functions I use in the above code (it's stored on a separate page that's called at the beginning of the main page):

    function mysql_prep($string) {
    global $connection;

    $escaped_string = mysqli_real_escape_string($connection, $string);
    return $escaped_string;
}

    function redirect_to($new_location) {
    header("Location: " . $new_location);
    exit;
}

$connection is what I use for my database connection, which I've double checked is working

And here is the code for my form:

    <form action="new_admin.php" method="post">
    <input type="hidden" name="page_name" value="<?=$_SERVER['REQUEST_URI']?>" />
<table>
  <tr>
    <td>Your name:</td>
    <td><input type="text" name="author" value="<?php echo $author; ?>" /></td>
  </tr>
  <tr>
    <td>Your comment:</td>
    <td><textarea name="body" cols="40" rows="8"><?php echo $body; ?></textarea></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><input type="submit" name="submit" value="Submit Comment" /></td>
  </tr>
</table>
</form>

I think that's everything. I'm just stumped right now and I don't know if it's something stupid I'm missing or if I did something bigger and set the whole thing up wrong.

Any help would be super appreciated!!

  • 写回答

1条回答 默认 最新

  • dongmei425373 2014-01-10 00:53
    关注

    You are referencing variable values in the columns to insert into. It is a feature of PHP to replace $variable within double quotes with the value of the variable. If I put in the author name as "King" and body as "BestBody" then your query will be INSERT INTO comments (KING, BestBody, <page>) VALUES('King', 'BestBody', <page>)"

    change this line: $query .= " $author, $body, $page_name"; with this line: $query .=" author, body, pagename"; or whatever your columns are named.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂