完全是 PHP 新手,但正在尝试将简单的表单提交到 MySQL 数据库。我不断收到通知: undefined index :表单输入变量上的警告。这里可能有什么问题。该表单位于提交到 process.php 文件的单独 PHP 页面上。
表单代码:
<form name="example" id="example" method="POST" action="process.php" enctype="multipart/form-data">
<p>
<label for="first_name">First Name:</label>
<input size="20" name="first_name" id="first_name" />
</p>
<p>
<label for="city">city</label>
<input size="20" name="city" id="city" />
</p>
<p>
<label for="state">state</label>
<input size="20" name="state" id="state" />
</p>
<p>
<label for="zip">zip</label>
<input size="20" name="zip" id="zip" />
</p>
<p>
<label for="email">e-mail</label>
<input size="20" name="email" id="email" />
</p>
<p>
<label for="form_upload">file</label>
<input size="40" type="file" name="form_data" id="form_data" />
</p>
<p>
<input type="submit" value="Submit form" name="submit" />
</p>
</form>
流程.php
<?php
mysql_connect("localhost", 'root', '') or die('Could not connect:' . mysql_error());
mysql_select_db("practice") or die(mysql_error());
mysql_query("CREATE TABLE user (id INT(5)NOT NULL AUTO, name VARCHAR(30), email VARCHAR(40), city VARCHAR(40), state VARCHAR(3), zip INT(5) )");
if (isset($_POST)) {
$name = $_POST['first_name'];
$email = $_POST['email'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
}
mysql_query("INSERT INTO user VALUES('$name', '$email', '$city', '$state', '$zip')");
?>
最佳答案
我想你应该改变这个
if (isset($_POST)){
并给出这样的索引
if (isset($_POST['submit'])){
还有
将查询更改为
mysql_query("INSERT INTO user (name , email, city,state, zip)
VALUES('$name', '$email', '$city', '$state', '$zip')" );
并且您应该在插入变量之前像这样转义它们。
$name = mysql_real_escape_string($name) ;
// and so on
最后一件事是为什么每次提交时都创建表?你应该检查是否存在。
关于php - 未定义索引 PHP : Can't Find Form Values,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19012901/