我试图将一些数据插入到mysql数据库中。 但我无法让它发挥作用..
这是我的reg.php文件:
<?php
include 'mysql.php';
if (isset($_POST['submit']))
{
$email = $_POST["email"];
$sql = ("INSERT INTO users (`email`) VALUES('".$email."') ");
mysql_query($sql);
}
?>
<form action="" method="post"> <br />
<input type="text" name="email">
<input type="submit" name="submit" >
</form>
这是我的mysql.php文件:
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
echo 'Connected to MySQL<br>';
mysql_close($conn);
?>
这是我在phpmyadmin的表格:
CREATE TABLE `users` (
`ID` int(11) NOT NULL,
`email` varchar(25) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1 ROW_FORMAT=DYNAMIC;
--
-- Indexes for dumped tables
--
--
-- Indexes for table `users`
--
ALTER TABLE `users`
ADD PRIMARY KEY (`ID`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `users`
--
ALTER TABLE `users`
MODIFY `ID` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
所以我有什么技巧可以解决这个问题吗?
谢谢!
答案 0 :(得分:2)
正如有人在更新版本的php mysql_*
函数中所述,您可以使用mysqli_*
函数,但由于您已关闭mysqli connection
中的mysql.php
,因此您的代码无效
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$dbname = "dbname";
$conn = mysqli_connect($dbhost, $dbuser, $dbpass,$dbname);//Put you db name here
if(! $conn )
{
die('Could not connect: ' . mysqli_error());
}
echo 'Connected to MySQL<br>';
//mysqli_close($conn); comment this line you code will work fine
?>
感谢
希望!这将有助于你
答案 1 :(得分:0)
MySQl函数已被弃用,并尝试将其放入您的代码中
mysql_query($sql);
之后
mysql_close();
还要检查您的XAMPP / PHP版本,因为PHP 7 MYSQL函数已被弃用