我的ask.php 语法有什么问题?
因此,我按照之前的指示修改了脚本,但是当我运行 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
至少,您需要在这一行的数组索引周围加上引号:
另外,请
检查您的错误级别(目前无法给您链接,有点匆忙),PHP 应该警告您这一点。
At a bare minimum you need quotes around your array indexes on this line:
Make it
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.
尝试更改
为
try changing
to
将其放在脚本的顶部:
这应该让您知道发生了什么。
Place this at the top of the script:
This should let you know whats going on.
鉴于您的错误消息,很明显您的查询失败了。您已使用
@
抑制了错误,因此or die(...)
永远不会生效。查询中的
$table_name
未定义,因此查询看起来像是错误的 SQL。
您需要的两个主要修复:
@
抑制。抑制错误几乎是不可接受的,尤其是在处理数据库时。$table_name
,或将查询字符串中的变量更改为正确的表名称。Given your error message, it's obvious that your query is failing. You've supressed errors with
@
on it, so theor die(...)
never kicks in.Your
$table_name
in the query is undefined, so the query looks likewhich is incorrect SQL.
The two major fixes you need:
@
supression on mysql_query(). It is almost NEVER acceptable to supress errors, particularly when dealing with a database.$table_name
in your script, or change the variable inside the query string to a proper table name.首先,不要检查 isset。由于 $_POST 总是生成,因此您应该使用 !empty()。
在 mysql_* 命令之前删除@,这会使您的脚本变慢并抑制有用的错误。
您遇到问题是因为您没有设置表变量,需要定义 $table_name 。
如果您要将问题插入到名为“questions”的表中,只需将 SQL 更改为:
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:
您忘记在第 1 行添加分号 *;*。 30
应该
是
You forgot add a semicolon *;* in line no. 30
it should be
instead of
您需要结束这一行:
将其更改为:
You need to end this line:
change it to: