我已经在内容管理系统之外使用PHP了,我已经忘记了一些基本的语法等。我正在尝试构建的是一个收集一些用户数据并发送它的简单表单到名为'creathive_applications'的数据库表。
这是我的HTML表单:
<form action="<?php bloginfo('home'); ?>" method="post">
<fieldset id="membershipform">
<ul class="clearfix">
<li id="li-status">
<span>I am a:</span>
<menu>
<li><label for="student"><input disabled="disabled" type="radio" name="status" id="student" checked="checked" value="Graduate" /> Graduate</label></li>
<li><label for="student2"><input disabled="disabled" type="radio" name="status" id="student2" value="Undergraduate" /> Undergraduate</label></li>
</menu>
</li>
<li id="li-firstname">
<label for="firstname">First Name</label> <input name="firstname" disabled="disabled" type="text" placeholder="First Name" id="firstname" title="First Name" />
</li>
<li id="li-lastname">
<label for="lastname">Last Name</label> <input name="lastname" disabled="disabled" type="text" placeholder="Last Name" id="lastname" title="Last Name" />
</li>
<li id="li-email">
<label for="email">Email address</label> <input name="email" disabled="disabled" type="text" placeholder="Email address" id="email" title="Email address" />
</li>
<li id="li-url">
<label for="url">URL</label> <input name="url" disabled="disabled" type="text" placeholder="URL of something you've made" id="url" title="URL of something you've made" />
</li>
<li id="li-buttons">
<input name="submit" type="submit" value="Send Application ►" title="Send Application" onclick="alert('Invites available from March 2011');" />
</li>
</ui>
</fieldset>
</form>
这是PHP爵士乐:
if(isset($_POST['submit']))
{
$status = $_POST['status'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$url = $_POST['url'];
$host = '####';
$username = '####';
$pass = '####';
mysql_connect($host,$username,$pass);
mysql_select_db($username);
$query = "INSERT INTO creathive_applications VALUES (NULL,'".$status."','".$firstname."','".$lastname."','".$email."','".$url."')";
$result = mysql_query($query);
}
任何人都可以帮我解决这个问题,因为我不记得如何运行我的SQL语句并将数据发送到数据库:/我还添加了数据库用户名和密码以及主机但是数据库名称怎么样?那又怎么了?
我知道这是一个相当难以理解的问题,但任何帮助都会非常感激。
感谢很多
答案 0 :(得分:2)
嗯,没有错误就无法确定要解决的问题 但是,有一些明显的事情要做 见评论:
<?
//show all possible errors. should be ALWAYS set to that level
error_reporting(E_ALL);
echo "landed at form handler<br>";
// sometimes buttons not being sent or gets misspelled
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
echo "here goes POST processing<br>";
$host = '####';
$username = '####';
$pass = '####';
mysql_connect($host,$username,$pass);
mysql_select_db($username);
// all strings should be escaped
// and it should be done after connecting to DB
$status = mysql_real_escape_string($_POST['status']);
$firstname = mysql_real_escape_string($_POST['firstname']);
$lastname = mysql_real_escape_string($_POST['lastname']);
$email = mysql_real_escape_string($_POST['email']);
$url = mysql_real_escape_string($_POST['url']);
$query = "INSERT INTO creathive_applications
VALUES (NULL,'$status','$firstname','$lastname','$email','$url')";
echo $query;
// always run your queries this way to be notified in case of error
$result = mysql_query($query) or trigger_error(mysql_error().". Query: ".$query);
var_dump($result);
}
如果您仍然没有看到任何错误,请暂时将此行添加到脚本顶部
ini_set('display_errors',1);
并在问题解决后立即将其删除。
EDIT 添加了一些调试信息 如果您没有看到任何消息,则表示您将表单发送到错误的网址。 如果您看到其中一些,请在此处发布
答案 1 :(得分:1)
正如Brian所说,如果发生任何错误,你应该发布。
我注意到的主要问题是你使用的是可能没有值的变量。如果未切换,单选按钮不会发送其值。
使用类似的东西来掩盖你的基础:
if(isset($_POST['status']))
{
$status = $_POST['status'];
}
不知道那是不是你的问题。考虑添加更多信息。 :)
答案 2 :(得分:1)
if(count($_POST) > 0){
$status = $_POST['status'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$url = $_POST['url'];
$host = '####';
$username = '####';
$pass = '####';
$mydb = mysql_connect($host,$username,$pass);
// mysql_select_db($username);
//are you using your username as db name?
mysql_select_db("yourdbname");
$query = "INSERT INTO creathive_applications VALUES (NULL,'".mysql_real_escape_string($status)."','".mysql_real_escape_string($firstname)."','".mysql_real_escape_string($lastname)."','".mysql_real_escape_string($email)."','".mysql_real_escape_string($url)."')"; //use mysql_real_escape_string for security
$result = mysql_query($query);
//show error if query fails.
if (!$result) {
die('Invalid query: ' . mysql_error());
}
mysql_close($mydb); //close the connection
}
添加mysql_error();
并查看是否打印出错误