表单没有使用mysql

时间:2017-02-14 06:59:20

标签: php mysql forms

  

这是html

<form action="addadd.php" method="post" enctype="multipart/form-data" name="form1" id="form1">
    <p>
        First Name<br>
        <label for="firstname"></label>
        <input type="text" name="firstname" id="firstname" />
    </p>
    <p>
        Last Name<br>
        <label for="lastname"></label>
        <input type="text" name="lastname" id="lastname" />
    </p>
    <p>
        Mobile<br>
        <label for="mobile"></label>
        <input type="text" name="mobile" id="mobile" />
    </p>
    <p>
        Email<br>
        <label for="email"></label>
        <input type="text" name="email" id="email" />
    </p>
    <p>
        <input type="submit" name="button" id="button" value="Submit" /> 
        <input type="reset" name="button3" id="button3" value="Reset" />
    </p>
</form>
  

这是php

     

数据库连接

<?php
$con = mysql_connect("localhost","root","");
if(!$con)
{
die("connection to database failed".mysql_error());
}
$dataselect = mysql_select_db("qoot",$con);
if(!$dataselect)
{
die("Database namelist not selected".mysql_error());
}
?>


<?php 
    $unm = $_SESSION['name']; 
    $fname=$_POST['firstname']; 
    $lname=$_POST['lastname']; 
    $ema=$_POST['email'];
    $mob=$_POST['mobile'];
?>



<?php 
    $qry=mysql_query("INSERT INTO address( firstname, lastname, mobile, email)VALUES('$fname',$lname','$ema','$mob')", $con);
?>

现在问题是这不会在我的数据库中插入任何内容 还有什么我可以尝试,以检查出错的地方? 更新的数据库连接详细信息

1 个答案:

答案 0 :(得分:0)

将您的表名address放入反引号

使用mysqli_

  <?php
/start session
session_start();
//establish connection
$con = mysqli_connect("localhost","root","","qoot");
if(!$con)
{
die("connection to database failed".mysqli_error($con));
}
     //read the values from form
    $unm = $_SESSION['name']; 
    $fname=$_POST['firstname']; 
    $lname=$_POST['lastname']; 
    $ema=$_POST['email'];
    $mob=$_POST['mobile'];
?>



<?php 
   //insert to database
    $qry=mysqli_query($con,"INSERT INTO `address` ( firstname, lastname, mobile, email)VALUES('$fname',$lname','$ema','$mob')") or die(mysqli_error($con));
?>

P.S我说你有mysql injection的风险,请点击这里How can I prevent SQL injection in PHP?。你应该使用准备好的陈述来避免任何风险。