2016-03-05 61 views
1

你们如何从文本(HTML/PHP)插入用户输入使用的数据库(phpMyAdmin的)mysql的插入用户输入(PHP到phpMyAdmin用mysql)

我不断收到错误“失败插入“是否有我的代码丢失的东西。

我没有在网上搜索如何解决它,但没有任何工作。我认为我的代码缺少一些东西,我不能指出它。

下的所有文件都在一个名为index.php的

<!DOCTYPE html> 
<?php 

$dbhost = 'localhost'; 
$dbuser = 'root'; 
$dbpass = ''; 
$db = 'dad_trading'; 

$dbconn = mysql_connect($dbhost, $dbuser, $dbpass); 
mysql_select_db($db); 




if (isset($_POST['submit'])) 
{ 
    $Lastname = $_POST['LastName']; 
    $firstname = $_POST['FirstName']; 
    $Middlename = $_POST['MiddleName']; 
    $address = $_POST['Address']; 
    $city  = $_POST['City']; 
    $zipcode = $_POST['ZipCode']; 
    $email  = $_POST['email']; 
    $number  = $_POST['number']; 


    $query = ("INSERT INTO customer ([LName], [FName], [MName], [Street], [City], [ZipCode], [Email], [ContactNo]) VALUES ('$Lastname', '$firstname', '$Middlename', '$address', '$city','$zipcode', '$email', '$number')"); 

if(mysql_query($query)) 
{ 
echo "<script>alert('INSERTED SUCCESSFULLY');</script>"; 
} 
else 
{ 
echo "<script>alert('FAILED TO INSERT');</script>"; 
} 

} 
?> 

<html> 
    <head> 
     <meta charset="UTF-8"> 
     <title>sample</title> 
    </head> 
    <body> 
     <form action="" method = "POST"> 

    First name: 
    &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
    Middle Name: 
    &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
    Last Name:<br> 
    <input name="FirstName" size="15" style="height: 19px;" type="text" required> 
     &nbsp; &nbsp; &nbsp; 
    <input name="MiddleName" size="15" style="height: 19px;" type="text" required> 
     &nbsp; &nbsp; &nbsp; 
    <input name="LastName" size="15" style="height: 19px;" type="text" required> 

    <br><br> 

    Email Address:<br> 
    <input name="email" type="text" required placeholder="Enter A Valid Email Address" style="height: 19px;" size="30"><br><br> 

    Home Address: <br> 
    <input name="Address" type="text" required placeholder="Enter your home Address" style="height: 19px;" size="30" maxlength="30"><br><br> 

    City: 
    &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; 
    Zipcode: 
    <br> 
    <input name="City" size="7" style="height: 19px;" type="text" required> 
    &nbsp; &nbsp; 
    <input name="ZipCode" size="7" style="height: 19px;" type="text" required> 
    <br><br> 

    Telephone/Mobile Number: <br> 
    <input name="number" type="text" required id="number" placeholder="Mobile Number" style="height: 19px;"> 

<br> 
<br> 

<button type ="submit" name="submit" value="send to database"> SEND TO DATABASE </button> 
</form> 
    </body> 
</html> 
+0

从字段名称中删除方括号并试试可能会有所帮助 –

+0

好吧我现在试试吧 –

+0

@AmitChauhan它的工作..哈哈傻了我。我强调了一个简单的错误 –

回答

0

1个PHP文件尝试使用服务器变量

<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post"> 
+0

您的第一评论的作品。来自文本框的数据现在正被插入到数据库中。 –

+0

您还必须将服务器变量置于操作中,并欢迎您 –

0

这里的,工程的代码示例添加表单动作。 From w3Schoolsmysql_connect已弃用,new mysqli有效。

<?php 
$servername = "localhost"; 
$username = "username"; 
$password = "password"; 
$dbname = "myDB"; 

// Create connection 
$conn = new mysqli($servername, $username, $password, $dbname); 
// Check connection 
if ($conn->connect_error) { 
    die("Connection failed: " . $conn->connect_error); 
} 

$sql = "INSERT INTO MyGuests (firstname, lastname, email) 
VALUES ('John', 'Doe', '[email protected]')"; 

if ($conn->query($sql) === TRUE) { 
    echo "New record created successfully"; 
} else { 
    echo "Error: " . $sql . "<br>" . $conn->error; 
} 

$conn->close(); 
?> 

尝试过自己,使你的代码的工作,作为一个初学者不知道它被打破,我碰到使用你的代码的几个问题来了。我将这一点留给任何可能最终在这里学习如何在数据库中插入数据的人,以及任何想要指出编辑此答案所产生的OP错误的人。