我的ask.php 语法有什么问题?

发布于 2024-11-17 10:58:45 字数 1478 浏览 0 评论 0原文

因此,我按照之前的指示修改了脚本,但是当我运行 HTML 表单时,我仍然得到一个空白页面。这里有什么问题吗?

更新 2:我现在收到以下错误。

解析错误:语法错误,/home/content/o/m/o/omorgan/html/dimephysicals/adviseme/ask.php 第 32 行出现意外的 T_VARIABLE

<?php

ini_set('display_errors',1);
error_reporting(E_ALL|E_STRICT);

//include('config.php');
//include('open_connection.php');



if (!isset($_POST['name']) || !isset($_POST['question']))
    {
        header ("Location: ask.html");
        exit;
    }

    // Database parameters
    $dbhost = '...';
    $dbuser = 'questionme';
    $dbpass = 'Question_2011';
    $dbname = 'questionme';

    $db_name = "questionme";
    $table_name = "questions";

    //Open connection
    $connection = mysql_connect("", "questionme", "Question_2011")
        or die(mysql_error());

    $db = mysql_select_db($db_name, $connection) or die(mysql_error())

    $name = mysql_escape_string($_POST[name]);
    $question = mysql_escape_string($_POST[question]);

    //Insert data into database
    $sql = "INSERT INTO questions
    (name, question) VALUES
    ('$name', '$question')";

?>

<html>
<head>
<title>Ask</title>
<head>
<body>

<h1>Question Submitted</h1>

<p><strong>Name:</strong>
<?php echo $_POST['name']; ?></p>

<p><strong>Question:</strong>
<?php echo $_POST['question']; ?></p>

</body>
</html>

So I modified the script as I was instructed earlier, but I am still getting a blank page when I run the HTML form. What is wrong here?

UPDATE 2: I get the following error now.

Parse error: syntax error, unexpected T_VARIABLE in /home/content/o/m/o/omorgan/html/dimephysics/adviseme/ask.php on line 32

<?php

ini_set('display_errors',1);
error_reporting(E_ALL|E_STRICT);

//include('config.php');
//include('open_connection.php');



if (!isset($_POST['name']) || !isset($_POST['question']))
    {
        header ("Location: ask.html");
        exit;
    }

    // Database parameters
    $dbhost = '...';
    $dbuser = 'questionme';
    $dbpass = 'Question_2011';
    $dbname = 'questionme';

    $db_name = "questionme";
    $table_name = "questions";

    //Open connection
    $connection = mysql_connect("", "questionme", "Question_2011")
        or die(mysql_error());

    $db = mysql_select_db($db_name, $connection) or die(mysql_error())

    $name = mysql_escape_string($_POST[name]);
    $question = mysql_escape_string($_POST[question]);

    //Insert data into database
    $sql = "INSERT INTO questions
    (name, question) VALUES
    ('$name', '$question')";

?>

<html>
<head>
<title>Ask</title>
<head>
<body>

<h1>Question Submitted</h1>

<p><strong>Name:</strong>
<?php echo $_POST['name']; ?></p>

<p><strong>Question:</strong>
<?php echo $_POST['question']; ?></p>

</body>
</html>

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

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

发布评论

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

评论(7

心房敞 2024-11-24 10:58:45

至少,您需要在这一行的数组索引周围加上引号:

if (!isset($_POST[name]) || !isset($_POST[question]))

另外,请

if (!isset($_POST['name']) || !isset($_POST['question']))

检查您的错误级别(目前无法给您链接,有点匆忙),PHP 应该警告您这一点。

At a bare minimum you need quotes around your array indexes on this line:

if (!isset($_POST[name]) || !isset($_POST[question]))

Make it

if (!isset($_POST['name']) || !isset($_POST['question']))

Also, check your error level (can't give you a link at the moment, in a bit of a rush), PHP should be warning you about this.

夏尔 2024-11-24 10:58:45

尝试更改

if (!isset($_POST[name]) || !isset($_POST[question]))

if (!isset($_POST['name']) || !isset($_POST['question']))

try changing

if (!isset($_POST[name]) || !isset($_POST[question]))

to

if (!isset($_POST['name']) || !isset($_POST['question']))
月竹挽风 2024-11-24 10:58:45

将其放在脚本的顶部:

ini_set('display_errors',1);
error_reporting(E_ALL|E_STRICT);

这应该让您知道发生了什么。

Place this at the top of the script:

ini_set('display_errors',1);
error_reporting(E_ALL|E_STRICT);

This should let you know whats going on.

没企图 2024-11-24 10:58:45

鉴于您的错误消息,很明显您的查询失败了。您已使用 @ 抑制了错误,因此 or die(...) 永远不会生效。

查询中的 $table_name未定义,因此查询看起来像是

INSERT INTO (name, question) ...

错误的 SQL。

您需要的两个主要修复:

  1. 删除 mysql_query() 上的 @ 抑制。抑制错误几乎是不可接受的,尤其是在处理数据库时。
  2. 在脚本中定义 $table_name,或将查询字符串中的变量更改为正确的表名称。

Given your error message, it's obvious that your query is failing. You've supressed errors with @ on it, so the or die(...) never kicks in.

Your $table_name in the query is undefined, so the query looks like

INSERT INTO (name, question) ...

which is incorrect SQL.

The two major fixes you need:

  1. Remove the @ supression on mysql_query(). It is almost NEVER acceptable to supress errors, particularly when dealing with a database.
  2. Define $table_name in your script, or change the variable inside the query string to a proper table name.
无语# 2024-11-24 10:58:45

首先,不要检查 isset。由于 $_POST 总是生成,因此您应该使用 !empty()。

在 mysql_* 命令之前删除@,这会使您的脚本变慢并抑制有用的错误。

您遇到问题是因为您没有设置表变量,需要定义 $table_name 。

如果您要将问题插入到名为“questions”的表中,只需将 SQL 更改为:

//Insert data into database
$sql = "INSERT INTO `questions` (name, question)
             VALUES ('$name', '$question')";

First of all, don't check with isset. Since $_POST is always generated you should be using !empty().

Remove @'s before mysql_* commands, this makes your script slow and suppresses helpful errors.

And you are having issues because you don't have table variable set, $table_name needs to be defined.

If you are inserting questions to a table named 'questions' simply change your SQL to:

//Insert data into database
$sql = "INSERT INTO `questions` (name, question)
             VALUES ('$name', '$question')";
蘑菇王子 2024-11-24 10:58:45

您忘记在第 1 行添加分号 *;*。 30

应该

   $db = mysql_select_db($db_name, $connection) or die(mysql_error());

  $db = mysql_select_db($db_name, $connection) or die(mysql_error())

You forgot add a semicolon *;* in line no. 30

it should be

   $db = mysql_select_db($db_name, $connection) or die(mysql_error());

instead of

  $db = mysql_select_db($db_name, $connection) or die(mysql_error())
眼眸印温柔 2024-11-24 10:58:45

您需要结束这一行:

$db = mysql_select_db($db_name, $connection) or die(mysql_error())

将其更改为:

$db = mysql_select_db($db_name, $connection) or die(mysql_error());

You need to end this line:

$db = mysql_select_db($db_name, $connection) or die(mysql_error())

change it to:

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