[英]php wont insert into mysql database
Im trying to insert some data into a mysql database. 我正在尝试将一些数据插入mysql数据库。 But I can not get it to work..
但是我无法使它工作。
This is my reg.php file: 这是我的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>
And this is my mysql.php file: 这是我的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);
?>
And this is my table from phpmyadmin: 这是我来自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 */;
So any tips here to how i can solve this? 因此,这里有任何提示可以解决这个问题?
Thanks! 谢谢!
As someone said in updated version of php mysql_*
function are deprecated you can use mysqli_*
functions, your code is not working because you have closed your mysqli connection
in mysql.php
正如有人说过,不赞成使用更新版本的php
mysql_*
函数,您可以使用mysqli_*
函数,您的代码无法正常工作,因为您已经在mysql.php
关闭了mysqli connection
<?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
?>
Thanks 谢谢
Hope! 希望! This will help you
这对你有帮助
MySQl functions have been deprecated and try to put this in your code MySQl函数已被弃用,请尝试将其放入代码中
mysql_query($sql);
after this 在这之后
mysql_close();
also check your XAMPP/PHP version as from PHP 7 MYSQL function has been deprecated 还要检查您的XAMPP / PHP版本,因为PHP 7 MYSQL函数已被弃用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.