gpt4 book ai didi

php - 将用户输入从文本框插入到数据库(PHP 到 PHPMYADMIN 使用 mysql)

转载 作者:行者123 更新时间:2023-11-30 22:24:07 24 4
gpt4 key购买 nike

你们如何使用 mysql 将用户输入从文本框(html/php)插入到数据库(phpmyadmin)

我一直收到“无法插入”错误,是我的代码缺少某些内容吗。

我确实在网上搜索了如何修复它,但没有任何效果。我认为我的代码中缺少某些东西,我无法查明它。

以下所有文件都在 1 个名为 index.php 的 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>

最佳答案

尝试使用服务器变量添加表单 Action

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

关于php - 将用户输入从文本框插入到数据库(PHP 到 PHPMYADMIN 使用 mysql),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35813147/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com