繁体   English   中英

我无法将HTML表单与MySQL数据库连接

[英]I can't connect my HTML form with MySQL database

我正在尝试构建一个表单,然后使用PHP将其连接到SQL数据库以填充表格。 但是我无法连接数据库。 当我按下commit时,将打开一个空白页面。.并且什么都没有插入到我的数据库中。 请你能帮我吗? 问题是什么?

这是我的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:&nbsp &nbsp &nbsp &nbsp &nbsp </strong> <input type="text" name="fname" />
   <span class="error">* <?php echo $fnameErr;?></span>
   <br>

   <strong> Mbiemri:&nbsp &nbsp </strong> <input type="text" name="lname" />
   <span class="error">* <?php echo $dtlErr;?></span>
   <br>


<input type="submit" name= "shto" value="Add"/>

</form>

</body>
</html>

这是insert.php 我的数据库称为test ,而表称为user 该表具有三列: 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>

尝试添加一些消息,以使您知道代码必须像这样:

<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>

现在,如果您没有在屏幕上看到任何东西,则表示非常严重的错误。 记住die()命令也应该写入浏览器。

最终编辑:

<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>

我更改了您的代码的多行。 尤其是die()mysql_error() 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);

变成:

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);

我为IF语句添加了括号。

添加; mysql_close($con)像这样mysql_close($con);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM