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