[英]I can't connect my HTML form with MySQL database
I am trying to build a form, and then connect it to my SQL database to fill a table, using PHP. 我正在尝试构建一个表单,然后使用PHP将其连接到SQL数据库以填充表格。 But I can't connect the database. 但是我无法连接数据库。 When I press commit, then a blank page is open.. And nothing is inserted into my database. 当我按下commit时,将打开一个空白页面。.并且什么都没有插入到我的数据库中。 Please can you help me? 请你能帮我吗? What is the problem? 问题是什么?
Here is my HTML form 这是我的HTML表格
<html>
<head>
<title> Futja e te dhenave nepermjet nje forme </title>
<style type = "text/css">
body { background-color: #cfd0f9 }
h2 { font-family: arial, sans-serif;
color: blue }
h3 {font-color: yellow}
.error {color: #FF0000;}
</style>
</head>
<body>
<h1><center>please enter data</center></h1>
<p> <span class="error">* please fill these fields.</span> </p>
<form method="post" action="insert.php">
<strong> Emri:          </strong> <input type="text" name="fname" />
<span class="error">* <?php echo $fnameErr;?></span>
<br>
<strong> Mbiemri:    </strong> <input type="text" name="lname" />
<span class="error">* <?php echo $dtlErr;?></span>
<br>
<input type="submit" name= "shto" value="Add"/>
</form>
</body>
</html>
And here is the insert.php
. 这是insert.php
。 My database is called test
, and the table is called user
. 我的数据库称为test
,而表称为user
。 The table has three columns: User_id, fname,lname
该表具有三列: User_id, fname,lname
<html>
<body>
<?php
$con = mysql_connect('127.0.0.1','root','');
if (!$con)
{
die('The connection is not possible: ' . mysql_error());
}
if(empty($_POST['fname']))
die ('Name is empty'. mysql_error());
mysql_select_db("test", $con);
if(empty($_POST['lname']))
die ('Last name is empty'. mysql_error());
mysql_select_db("test", $con);
$sql="INSERT INTO user(fname,lname)
VALUES
('$_POST[fname]','$_POST[lname]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "Successful insertion!!! ";
mysql_close($con)
?>
</body>
</html>
Try adding some messages so you know where the code has got to like this: 尝试添加一些消息,以使您知道代码必须像这样:
<html>
<body>
<?php
echo '<pre>Before Connect' . print_r($_POST, true) . '</pre>'; // display the $_POST array
$con = mysql_connect('127.0.0.1','root','');
if (!$con)
{
die('The connection is not possible: ' . mysql_error());
}
echo 'After Connect';
mysql_select_db('test', $con);
echo 'Database selected';
if(empty($_POST['fname'])) die ('Name is empty'. mysql_error());
if(empty($_POST['lname'])) die ('Last name is empty'. mysql_error());
$sql = "INSERT INTO user(fname,lname) VALUES ('$_POST[fname]','$_POST[lname]')";
echo "<br>$sql</br>";
if (!mysql_query($sql,$con)) {
die('Error: ' . mysql_error());
}
echo "Successful insertion!!! ";
mysql_close($con)
?>
</body>
</html>
Now if you dont get anything to the screen, something is very seriously wrong. 现在,如果您没有在屏幕上看到任何东西,则表示非常严重的错误。 Remember the die()
commands should write to the browser as well. 记住die()
命令也应该写入浏览器。
Final Edit: 最终编辑:
<html>
<head><title>Insert</title></head>
<body>
<?php
if(!isset($_POST['fname']))
{
echo ('<b>Name is empty!</b>');
}
elseif(!isset($_POST['lname']))
{
echo ('<b>Name is empty!</b>');
}
else {
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$con = mysql_connect('127.0.0.1','root','');
if (!$con)
{
die('The connection is not possible: ' . mysql_error());
}
mysql_select_db("test", $con);
$sql="INSERT INTO user(fname,lname) VALUES('".$fname."','".$lname."')";
if (!mysql_db_query($con, $sql))
{
die('Error: ' . mysql_error());
}
echo ('<b>Successful insertion!!!</b>');
mysql_close($con);
}
?>
</body>
</html>
I have changed multiple lines of your code. 我更改了您的代码的多行。 Especially the die()
and mysql_error()
the mysql_error did not apply because you were do doing anything with you server at that point in you code. 尤其是die()
和mysql_error()
mysql_error并不适用,因为那时您在代码中对服务器做了任何事情。 Some of the other comments on your code can be found in my initial answer. 在我的初始答案中可以找到关于您的代码的其他一些注释。 I have included it in my answer for reference. 我已将其包含在我的答案中以供参考。
Old version: 旧版本:
Change: 更改:
if(empty($_POST['fname']))
die ('Name is empty'. mysql_error());
mysql_select_db("test", $con); if(empty($_POST['lname']))
die ('Last name is empty'. mysql_error());
mysql_select_db("test", $con);
into: 变成:
if(empty($_POST['fname'])){
die ('Name is empty'. mysql_error());
}
mysql_select_db("test", $con);
if(empty($_POST['lname'])){
die ('Last name is empty'. mysql_error());
}
mysql_select_db("test", $con);
I have added brackets for IF statement. 我为IF语句添加了括号。
Add ; 添加; after mysql_close($con)
like so mysql_close($con);
在mysql_close($con)
像这样mysql_close($con);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.