Php表单用1填充mySQL表行

时间:2015-03-29 04:19:14

标签: php sql

所以我正在为学校项目制作投诉页面。我从一个简单的PHP表单开始,直到我添加了一个

if(isset($_POST['submit'])){ PHP code to be executed here }

哪个不再有用了。所以我决定一起删除那条线;现在,无论何时我提交任何表单,即使我用文本填写表单的文本框,它也会用1填充我的MYsql表。我不知道mySQL配置中是否存在此问题,我还有另一种形式遭遇同样的问题。

我将在下面详细介绍这个问题。

我的" Klacht"表目前看起来像(请原谅我的链接问题..我不能发布图片,因为我没有超过10个代表..)

http://i.gyazo.com/6589558c59c4955f5cd48c335d79bdac.png

它的结构

http://gyazo.com/51ab9d9184a4beb2197ce41f0b98b35b

我的表单代码是一个.php文件,按下提交按钮后我无法预览我的PHP代码。它只是回应了消息

<!DOCTYPE html>
<html lang="nl">
    <head>
        <title>Prototype</title>
    </head>       
    <body>
      <h3>Klacht test</h3>    
        <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
            <br />Test nummer: <input type="text" name="Nr">
            <br />Postcode<input type="text" name="Postcode">
             <br />Datum<input type="Date" name="Datum"> 
             <br />Tijd<input type="text" name="Tijd" >
             <br />Soort klacht
                        <select name="Soort">
                            <option value="    "></option>
                            <option value="Geluid">Geluid</option>
                            <option value="Milieu">Milieu</option>
                            <option value="Veiligheid">Veiligheid</option>
                        </select>
                <!-- Submit button -->
                <br /><input type="submit" value="Versturen">  
                <input type="reset" value="Reset">
        </form>
   </body>
  <?php 

    mysql_connect("localhost", "root", "") or die ("Could not connect to database");
    mysql_select_db("schiphol") or die("could not select database");
    $Nr = isset($_POST["Nr"]);
    $Postcode = isset($_POST["Postcode"]);
    $Datum = isset($_POST["Datum"]);
    $Tijd = isset($_POST["Tijd"]);
    $Soort = isset($_POST["Soort"]);

    $query = ("INSERT INTO klacht (Nr, Postcode, Datum, Tijd, Soort)
    VALUES ('$Nr', '$Postcode', '$Datum', '$Tijd', '$Soort')");
    $resultaat = mysql_query($query) or die (mysql_error( ));
    if($resultaat) echo "Uw klacht is toegevoegd" ; else echo "ERROR";


    ?>
</html>

那么......我做错了什么?正如我上面提到的,另一种形式正遭受同样的问题。它曾经工作但不再适用。

提前致谢。

4 个答案:

答案 0 :(得分:1)

嗨,我认为您的问题在于isset()函数。实际上isset()函数将返回一个基于1或0的布尔值。所以请在没有isset()的情况下更新你的php部分,如下所示并尝试。

<?php 

mysql_connect("localhost", "root", "") or die ("Could not connect to database");
mysql_select_db("schiphol") or die("could not select database");
$Nr =$_POST["Nr"];
$Postcode =$_POST["Postcode"];
$Datum = $_POST["Datum"];
$Tijd = $_POST["Tijd"];
$Soort = $_POST["Soort"];

$query = ("INSERT INTO klacht (Nr, Postcode, Datum, Tijd, Soort)
VALUES ('$Nr', '$Postcode', '$Datum', '$Tijd', '$Soort')");
$resultaat = mysql_query($query) or die (mysql_error( ));
if($resultaat) echo "Uw klacht is toegevoegd" ; else echo "ERROR";


?>

答案 1 :(得分:1)

你的问题在isset()上返回0或1

您使用吹码

<input type="submit" value="Versturen">

但您忘记提及姓名

<input type="submit" name="submit" value="Versturen">

尝试此操作

<!DOCTYPE html>
<html lang="nl">
<head>
    <title>Prototype</title>
</head>       
<body>
  <h3>Klacht test</h3>    
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
        <br />Test nummer: <input type="text" name="Nr">
        <br />Postcode<input type="text" name="Postcode">
         <br />Datum<input type="Date" name="Datum"> 
         <br />Tijd<input type="text" name="Tijd" >
         <br />Soort klacht
                    <select name="Soort">
                        <option value=""></option>
                        <option value="Geluid">Geluid</option>
                        <option value="Milieu">Milieu</option>
                        <option value="Veiligheid">Veiligheid</option>
                    </select>
            <!-- Submit button -->
            <br /><input type="submit" name="submit" value="Versturen">  
            <input type="reset" value="Reset">
    </form>
   </body>
   <?php 
if(isset($_POST['submit'])){

echo "<pre/>";
print_r($_POST);
//die;


mysql_connect("localhost", "root", "") or die ("Could not connect to database");
mysql_select_db("schiphol") or die("could not select database");
$Nr = $_POST["Nr"];
$Postcode = $_POST["Postcode"];
$Datum = $_POST["Datum"];
$Tijd = $_POST["Tijd"];
$Soort = $_POST["Soort"];

$query = ("INSERT INTO klacht (Nr, Postcode, Datum, Tijd, Soort) VALUES ('$Nr', '$Postcode', '$Datum', '$Tijd', '$Soort')");

echo $query;

$resultaat = mysql_query($query) or die (mysql_error( ));
if($resultaat) echo "Uw klacht is toegevoegd" ; else echo "ERROR";

}
?>

答案 2 :(得分:0)

$var = is set($_POST['some_var']); // this will just return a 1 to $var if the post variable is set

试试这个

if(isset($_POST['var'])){ $db_var = $_POST['var'];}

答案 3 :(得分:0)

以下if(isset($_POST['submit'])){ PHP code to be executed here }无效,因为您没有为按钮命名

<input type="submit" value="Versturen">

同样指出@Azeez Kallayi isset()返回布尔值。

试试这个:

<?php 
if(isset($_POST["Nr"],$_POST["Postcode"],$_POST["Datum]",$_POST["Tijd"],$_POST["Soort"])){

    mysql_connect("localhost", "root", "") or die ("Could not connect to database");
    mysql_select_db("schiphol") or die("could not select database");
    $Nr = mysql_real_escape_string($_POST["Nr"]);
    $Postcode = mysql_real_escape_string($_POST["Postcode"]);
    $Datum = mysql_real_escape_string($_POST["Datum"]);
    $Tijd = mysql_real_escape_string($_POST["Tijd"]);
    $Soort = mysql_real_escape_string($_POST["Soort"]);

    $query = ("INSERT INTO klacht (Nr, Postcode, Datum, Tijd, Soort)
               VALUES ('$Nr', '$Postcode', '$Datum', '$Tijd', '$Soort')");
    $resultaat = mysql_query($query) or die (mysql_error( ));
    if($resultaat) echo "Uw klacht is toegevoegd" ; else echo "ERROR";
}else{?>
<!DOCTYPE html>
<html lang="nl">
    <head>
        <title>Prototype</title>
    </head>       
    <body>
      <h3>Klacht test</h3>    
        <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
            <br />Test nummer: <input type="text" name="Nr">
            <br />Postcode<input type="text" name="Postcode">
             <br />Datum<input type="Date" name="Datum"> 
             <br />Tijd<input type="text" name="Tijd" >
             <br />Soort klacht
                        <select name="Soort">
                            <option value="    "></option>
                            <option value="Geluid">Geluid</option>
                            <option value="Milieu">Milieu</option>
                            <option value="Veiligheid">Veiligheid</option>
                        </select>
                <!-- Submit button -->
                <br /><input type="submit" value="Versturen">  
                <input type="reset" value="Reset">
        </form>
   </body>
</html>
<?php}?>

<?php 
if(isset($_POST["Nr"],$_POST["Postcode"],$_POST["Datum]",$_POST["Tijd"],$_POST["Soort"])){

    mysql_connect("localhost", "root", "") or die ("Could not connect to database");
    mysql_select_db("schiphol") or die("could not select database");
    $Nr = mysql_real_escape_string($_POST["Nr"]);
    $Postcode = mysql_real_escape_string($_POST["Postcode"]);
    $Datum = mysql_real_escape_string($_POST["Datum"]);
    $Tijd = mysql_real_escape_string($_POST["Tijd"]);
    $Soort = mysql_real_escape_string($_POST["Soort"]);

    $query = ("INSERT INTO klacht (Nr, Postcode, Datum, Tijd, Soort)
               VALUES ('$Nr', '$Postcode', '$Datum', '$Tijd', '$Soort')");
    $resultaat = mysql_query($query) or die (mysql_error( ));
    if($resultaat) echo "Uw klacht is toegevoegd" ; else echo "ERROR";
}else{?>
<!DOCTYPE html>
<html lang="nl">
    <head>
        <title>Prototype</title>
    </head>       
    <body>
      <h3>Klacht test</h3>    
        <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
            <br />Test nummer: <input type="text" name="Nr">
            <br />Postcode<input type="text" name="Postcode">
             <br />Datum<input type="Date" name="Datum"> 
             <br />Tijd<input type="text" name="Tijd" >
             <br />Soort klacht
                        <select name="Soort">
                            <option value="    "></option>
                            <option value="Geluid">Geluid</option>
                            <option value="Milieu">Milieu</option>
                            <option value="Veiligheid">Veiligheid</option>
                        </select>
                <!-- Submit button -->
                <br /><input type="submit" value="Versturen">  
                <input type="reset" value="Reset">
        </form>
   </body>
</html>
<?php}?>