简体   繁体   中英

form didnt working with mysql

this is the 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>

This is the php

database connection

<?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);
?>

Now the problem is that this is inserting nothing in to my database.
What else can i try in order to check where things go wrong? updated database connection details

Place your tablename address in backticks

Using 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));
?>

PS I'd say you are at risk of mysql injection , check here How can I prevent SQL injection in PHP? . You should really use prepared statements to avoid any risk.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM