Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP表单在mySQL中不显示任何内容_Php_Mysql - Fatal编程技术网

PHP表单在mySQL中不显示任何内容

PHP表单在mySQL中不显示任何内容,php,mysql,Php,Mysql,我试图弄明白为什么我的php表单没有将字段中的数据放入mySQL数据库。我一直想弄明白这一点,但已经走到了死胡同 当使用每个字段的值进行硬编码时,我的insert$sql可以正常工作,但当我尝试使用从php表单输入的字段时,就不行了 当我点击submit时,我没有收到任何错误,但是当我检查mySQL是否添加了另一个所有者时,没有显示任何内容 如果有人能帮我解决这个问题,我会非常感激 顺便问一下,我的$SQLINSERT语句加引号是否正确 <head> <style>

我试图弄明白为什么我的php表单没有将字段中的数据放入mySQL数据库。我一直想弄明白这一点,但已经走到了死胡同

当使用每个字段的值进行硬编码时,我的insert$sql可以正常工作,但当我尝试使用从php表单输入的字段时,就不行了

当我点击submit时,我没有收到任何错误,但是当我检查mySQL是否添加了另一个所有者时,没有显示任何内容

如果有人能帮我解决这个问题,我会非常感激

顺便问一下,我的$SQLINSERT语句加引号是否正确

    <head>
<style>
table, th, td {
    border: 1px solid black;
    border-collapse: collapse;
}
th, td {
    padding: 5px;
}
</style>
</head>
<body>
<?php  #index.php for Assignment 10
$page_title = 'Assignment 10 for Marina Database';
include('header.html');
require('dbConn.php');
echo '<h1> Please enter the following fields:</h1>';


if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
$OwnerNum=$_POST['OwnerNum'];
$LastName=$_POST['LastName'];
$FirstName=$_POST['FirstName'];
$Address=$_POST['Address'];
$City=$_POST['City'];
$State=$_POST['State'];
$Zip=$_POST['Zip'];

//echo test;
try {
$sql = "INSERT INTO Owner (OwnerNum, LastName, FirstName, Address, City, State, Zip) VALUES 
('".$OwnerNum."', '".$LastName."', '".$FirstName."', '".$Address."', '".$City."', '".$State."', '".$Zip."')";

//this works when hard coded
/*$sql = "INSERT INTO Owner (OwnerNum, LastName, FirstName, Address, City, `State, Zip) VALUES ('XR34', 'Patel', 'John', '342 Picardy lane', 'Wheeling', 'IL', '60018')"; */`
$conn->exec($sql);

//echo $OwnerNum, $LastName, $FirstName, $Address, $City, $State, $Zip;
}
catch (PDOException $e)
{
echo 'Error: '.$e->getMessage();
} //end catch



}



if (isset($_POST['submit']))
{
    $stmt = $conn->prepare("select* from Owner");
    $stmt->execute();

    $result = $stmt->setFetchMode(PDO::FETCH_ASSOC); 

    echo "<table style='border: solid 1px black;'>";
    echo "<tr><th>OwnerNum</th><th>LastName</th><th>FirstName</th><th>Address</th><th>City</th><th>State</th><th>Zip</th></tr>";


class TableRows extends RecursiveIteratorIterator 
{ 
    function __construct($it) { 
        parent::__construct($it, self::LEAVES_ONLY); 
    }

    function current() {
        return "<td style='width:150px;border:1px solid black;'>" . parent::current(). "</td>";
    }

    function beginChildren() { 
        echo "<tr>"; 
    } 

    function endChildren() { 
        echo "</tr>" . "\n";
    } 

} 

    foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) 
    { 
         echo $v;

    }

    $conn = null;
    echo "</table>";
}

?>
<form name="createOwner" action="Assignment10newowner.php" method="POST">
  <table style="width:100%">
    <tr>
      <td>Owner Number:</td>
      <td><input type="text" name="OwnerNum"></td>
    </tr>

      <td>Last Name:</td>
      <td><input type="text" name="LastName"></td


    </tr>
    <tr>
      <td>First Name:</td>
      <td><input type="text" name="FirstName"></td


    </tr>
    <tr>
      <td>Address:</td>
      <td><input type="text" name="Address"></td


    </tr>
    <tr>
      <td>City:</td>
      <td><input type="text" name="City"></td


    </tr>
    <tr>
      <td>State:</td>
      <td><input type="text" name="State"></td


    </tr>
    <tr>
      <td>Zip:</td>
      <td><input type="text" name="Zip"></td


    </tr>
  </table>
  <br>
  <br>
  <input type="submit" value="Submit">
</form>
</body>

表,th,td{
边框:1px纯黑;
边界塌陷:塌陷;
}
th,td{
填充物:5px;
}

将$\u POST值分配给如下变量

if (isset($_POST['OwnerNum]))
{

    $OwnerNum=$_POST['OwnerNum'];
    $LastName=$_POST['LastName'];
    $FirstName=$_POST['FirstName'];
    $Address=$_POST['Address'];
    $City=$_POST['City'];
    $State=$_POST['State'];
    $Zip=$_POST['Zip'];

    try {
        $sql = "INSERT INTO Owner (OwnerNum, LastName, FirstName, Address, City, State, Zip) VALUES 
        ('".$OwnerNum."', '".$LastName."', '".$FirstName."', '".$Address."', '".$City."', '".$State."', '".$Zip."')";    
        $conn->exec($sql);
        $select_owner = "SELECT * FROM Owner";
        ?>
            <table>
                <tr>
                    <td>OwnerNum</td>
                    <td>FirstName</td>
                    <td>LastName</td>
                    <td>Address</td>
                    <td>City</td>
                </tr>
        <?php       
        $conn->prepare($select_owner );
        $result = $conn->fetchAll();
        if (count($result)) {

            foreach($result as $owner){

                    ?>
                    <tr>
                        <td><?=$owner['OwnerNum'];?></td>
                        <td><?=$owner['FirstName'];?></td>
                        <td><?=$owner['LastName'];?></td>
                        <td><?=$owner['Address'];?></td>
                        <td><?=$owner['City'];?></td>
                    </tr>       
            <?php
                }
        }
        else
        {
           ?>
            <tr>
                <td colspan="5">No Onwers Found</td>            
            </tr>  
           <?php
        }
        unset($result);
        unset($select_owner);
        unset($conn);
    }
    catch (PDOException $e)
    {
        echo 'Error: '.$e->getMessage();
    } //end catch

}
if(isset($\u POST['OwnerNum]))
{
$OwnerNum=$_POST['OwnerNum'];
$LastName=$\u POST['LastName'];
$FirstName=$_POST['FirstName'];
$Address=$_POST['Address'];
$City=$_POST['City'];
$State=$_POST['State'];
$Zip=$_POST['Zip'];
试一试{
$sql=“插入所有者(OwnerNum、LastName、FirstName、Address、City、State、Zip)值
(“$OwnerNum.”、“$LastName.”、“$FirstName.”、“$Address.”、“$City.”、“$State.”、“$Zip.”);
$conn->exec($sql);
$select_owner=“select*FROM owner”;
?>
所有者
名字
姓氏
地址
城市
找不到Onwers

将$\u POST值分配给如下变量

if (isset($_POST['OwnerNum]))
{

    $OwnerNum=$_POST['OwnerNum'];
    $LastName=$_POST['LastName'];
    $FirstName=$_POST['FirstName'];
    $Address=$_POST['Address'];
    $City=$_POST['City'];
    $State=$_POST['State'];
    $Zip=$_POST['Zip'];

    try {
        $sql = "INSERT INTO Owner (OwnerNum, LastName, FirstName, Address, City, State, Zip) VALUES 
        ('".$OwnerNum."', '".$LastName."', '".$FirstName."', '".$Address."', '".$City."', '".$State."', '".$Zip."')";    
        $conn->exec($sql);
        $select_owner = "SELECT * FROM Owner";
        ?>
            <table>
                <tr>
                    <td>OwnerNum</td>
                    <td>FirstName</td>
                    <td>LastName</td>
                    <td>Address</td>
                    <td>City</td>
                </tr>
        <?php       
        $conn->prepare($select_owner );
        $result = $conn->fetchAll();
        if (count($result)) {

            foreach($result as $owner){

                    ?>
                    <tr>
                        <td><?=$owner['OwnerNum'];?></td>
                        <td><?=$owner['FirstName'];?></td>
                        <td><?=$owner['LastName'];?></td>
                        <td><?=$owner['Address'];?></td>
                        <td><?=$owner['City'];?></td>
                    </tr>       
            <?php
                }
        }
        else
        {
           ?>
            <tr>
                <td colspan="5">No Onwers Found</td>            
            </tr>  
           <?php
        }
        unset($result);
        unset($select_owner);
        unset($conn);
    }
    catch (PDOException $e)
    {
        echo 'Error: '.$e->getMessage();
    } //end catch

}
if(isset($\u POST['OwnerNum]))
{
$OwnerNum=$_POST['OwnerNum'];
$LastName=$\u POST['LastName'];
$FirstName=$_POST['FirstName'];
$Address=$_POST['Address'];
$City=$_POST['City'];
$State=$_POST['State'];
$Zip=$_POST['Zip'];
试一试{
$sql=“插入所有者(OwnerNum、LastName、FirstName、Address、City、State、Zip)值
(“$OwnerNum.”、“$LastName.”、“$FirstName.”、“$Address.”、“$City.”、“$State.”、“$Zip.”);
$conn->exec($sql);
$select_owner=“select*FROM owner”;
?>
所有者
名字
姓氏
地址
城市
找不到Onwers

代码在我看来很好,但是在插入数据时,您应该首先捕获表单数据,然后将它们存储在变量中,然后在插入查询中使用这些变量

   if (isset($_POST['submit1']))//set your submit btn name to submit1
 {     
        $username=$_POST['OwnerNum'];//likewise catch all data

     try {
       $sql = "INSERT INTO Owner (OwnerNum, LastName, FirstName, Address, City, State, Zip) VALUES 
    (?,?,?.....)";//put the placeholders here.the num of placeholders should be equal to number of bound values

代码在我看来很好,但是在插入数据时,您应该首先捕获表单数据,然后将它们存储在变量中,然后在插入查询中使用这些变量

   if (isset($_POST['submit1']))//set your submit btn name to submit1
 {     
        $username=$_POST['OwnerNum'];//likewise catch all data

     try {
       $sql = "INSERT INTO Owner (OwnerNum, LastName, FirstName, Address, City, State, Zip) VALUES 
    (?,?,?.....)";//put the placeholders here.the num of placeholders should be equal to number of bound values

这些变量“.$OwnerNum.”、“$LastName.”、“$FirstName.”、“$Address.”、“$City.”在哪里?因为这些变量都没有定义。您正在查找
$\u POST['OwnerNum']
不是
$OwnerNum
。另外,将变量直接放入查询中是不好的做法。另外,请检查您的数据库连接。请尽早学会使用参数化查询。警告:使用
mysqli
时,您应该使用参数化查询并将用户数据添加到查询中。不要使用str由于您创建了严重的错误,请使用插值或连接来完成此操作。切勿将
$\u POST
$\u GET
数据直接放入查询中。使用准备好的语句意味着您的引用将始终是正确的。这些变量“.$OwnerNum.”、“$LastName.”、“$FirstName.”、“$Address.”、“$City”在哪里这是因为这些变量都没有定义。您正在查找
$\u POST['OwnerNum']
不是
$OwnerNum
。另外,将变量直接放入查询中是不好的做法。另外,请检查您的数据库连接。请尽早学会使用参数化查询。警告:使用
mysqli
时,您应该使用参数化查询并将用户数据添加到查询中。不要使用str由于您创建了一个严重的错误,请使用插值或连接来完成此操作。切勿将
$\u POST
$\u GET
数据直接放入查询中。使用准备好的语句意味着您的引用始终是正确的。谢谢,还有一个问题。只有当用户单击“提交”时,我如何才能在同一页面中显示结果tton?我已在我的代码中进行了上述更改,并添加了if(isset)部分,但一旦我在表单中输入数据并单击submit,它就不会显示添加了mySQL表中的用户和其他所有者的表。谢谢,还有一个问题。只有当用户单击submit按钮时,我如何才能在同一页面中显示结果?我已经在我的代码中做了上述更改,并添加了if(isset)部分,但一旦我在表单中输入数据并单击“提交”,它就不会显示添加了用户的表以及mySQL表中的其他所有者。我在这里没有使用mysqli…它只是代码的一部分,更像pdo…为此,您始终不需要绑定参数…占位符(?)当参数未绑定时使用…您可以在php手册中检查。我在这里没有使用mysqli…它只是代码的一部分,更像pdo…因此您始终不需要绑定参数…当参数未绑定时使用占位符(?),您可以在php手册中检查。