PHP没有向mysql表提交值

时间:2014-02-01 19:06:04

标签: php html mysql forms

我正在尝试使用php提交表单以将数据提交到我已在数据库中创建的表中。所有mysql_connect信息都是正确的。我的代码中有什么东西我做得不对吗?

PHP:     

if (isset($_POST['submit']) && strlen($_POST['firstName'])>0 && strlen($_POST['lastName'])>0 && strlen($_POST['email'])>0)
{
    $first_name=$_POST['firstName']; 
    $last_name=$_POST['lastName']; 
    $email=$_POST['email'];
    $phone=$_POST['phone'];
    $city=$_POST['city'];
    $make=$_POST['make'];
    $model=$_POST['model'];
    $year=$_POST['year']; 
    mysql_connect("******", "*****", "****") or die(mysql_error()); 
    mysql_select_db("buddyTruk") or die(mysql_error());
    mysql_query("ALTER TABLE drivers ADD PRIMARY KEY (email)");
    mysql_query("REPLACE INTO `drivers` VALUES ('$first_name', '$last_name', '$email', '$phone', '$city', '$make', '$model', '$year')"); 

 } 


 ?> 

HTML:

<form id="drivers-form" class="form-horizontal" method="get" action="index.php">
                <input type="text" name="firstName" class="form-control input-lg" placeholder="First Name" required/> 
                <input type="text" name="lastName" class="form-control input-lg" placeholder="Last Name" required/>
                <input type="email" name="email" class="form-control input-lg" placeholder="Email" required/>
                <input type="tel" name="phone" class="form-control input-lg" placeholder="Phone" required/>
                <input type="text" name="city" class="form-control input-lg" placeholder="City" required/>
                <input type="text" name="make" class="form-control input-lg" placeholder="Make" required/>
                <input type="text" name="model" class="form-control input-lg" placeholder="Model" required/> 
                <input type="text" name="year" class="form-control input-lg" placeholder="Year" required/> 
                <button class="btn btn-success btn-lg" name="submit" type="submit" value="submit">Submit</button>
            </form>

1 个答案:

答案 0 :(得分:2)

您的表单上有错误的方法。将表单方法更改为"post"

<form id="drivers-form" class="form-horizontal" method="post" action="index.php">

如果您想使用"get"作为表单方法,则可以使用$_GET访问值,但不建议使用get

请停止使用mysql,因为它已被弃用。请改用mysqliPDO。最重要的是,使用准备好的陈述。任何人都可以使用您当前的代码轻松窃取和删除所有数据。