数据没有使用php插入数据库

时间:2017-01-23 11:57:12

标签: php forms

我正在使用表单将数据插入到我创建的表中。我没有错误,但由于某种原因,它表明它有效,虽然它没有插入任何数据,

以下是我正在使用的内容:

这是表单页面:

Action

phpsidecode

<form name="myForm" action="submitform.php" 
onsubmit="return validateForm()" method="post">
Name: 
<br />
<input type="text" name="fname" style="


Email:
<br />
<input type="email" name="email" >

Subject: 
<br />
<input type="text" name="subject">

Message: 
<br />
<textarea name="message" >
    </textarea>
<input type="submit" value="Send Message" name="add"  >
</form>

3 个答案:

答案 0 :(得分:0)

由于从POST变量中读取任何单逗号字符,您的查询可能会被破坏。你应该使用addslashes()

来改变变量

无论如何,正如评论中所建议的那样,建议学习PDO或mysqli。

答案 1 :(得分:0)

    <?php
        $dbhost='localhost';
        $dbnmae='someuser';
        $dbpass='somepassword';
        $conn = mysqli_connect($dbhost,$dbnmae,$dbpass);
        if (!$conn) {
            die('Could not connect: '.mysqli_error());
        }
    if(isset($_POST['add']))
    {
        $submit=$_POST['add'];
        $Name=$_POST['fname'];
        $Email=$_POST['email'];
        $Subject=$_POST['subject'];
        $Message=$_POST['message'];    
        $sql="INSERT INTO mydb(name, email, subject, message)VALUES('$Name', '$Email', '$Subject', '$Message')";
        mysqli_select_db('project1');
        $retrieval=mysqli_query($conn,$sql);
        if(isset($retrieval)){  
echo "Entered data successfully\n";
             mysqli_close($conn);
            }           
    }else{  
     echo "something went to wrong";
 }
        ?>

答案 2 :(得分:0)

试试这个

<?php
if(isset($_POST['add']))
{
    $Email=$_POST['email'];
    $Subject=$_POST['subject'];
    $Message=$_POST['message'];
    $dbhost='localhost';
    $dbnmae='someuser';
    $dbpass='somepassword';
    $conn = mysql_connect($dbhost,$dbnmae,$dbpass);
    if (!$conn) {
        die('Could not connect: '.mysql_error());
    }
    mysql_select_db('project1');
    $sql="INSERT INTO project1 VALUES('$Email', '$Subject', '$Message')";
    $retrieval=mysql_query($conn,$sql);
    if(!$retrieval)
    {  die('Could not connect: '.mysql_error());
        }
        echo"Entered data successfully\n";
         mysql_close($conn);
}
else   
{   echo"something went to wrong";

}
?>