What is wrong with my ask.php Syntax?
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 = mys开发者_Go百科ql_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>
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.
try changing
if (!isset($_POST[name]) || !isset($_POST[question]))
to
if (!isset($_POST['name']) || !isset($_POST['question']))
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.
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:
- Remove the
@
supression on mysql_query(). It is almost NEVER acceptable to supress errors, particularly when dealing with a database. - Define
$table_name
in your script, or change the variable inside the query string to a proper table name.
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')";
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())
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());
精彩评论