简体   繁体   中英

How can I store a value from a form into mySQL database?

I made a form and I want to store the input value of cat into my SQL database. Here is my php code:

<?php

    $cat = $_POST['cat'];

    if ( !empty($_POST)) {
        $pdo = Database::connect();
            $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $sql = "UPDATE orders set cat = ?  WHERE id = '$id'";           
            $q = $pdo->prepare($sql);
            $q->execute(array($cat));
            Database::disconnect();

    }


?> 

And this is my form:

<form action="index.php" method="post">

<div id="modal-content" class="modal fade" tabindex="-1" role="dialog">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">

                <h3><?php echo $id ?></h3>
            </div>
            <div class="modal-body">
                <p>
                    <input type="text" name="cat" />
                </p>
            </div>
            <div class="modal-footer"> 
                  <button type="submit" class="btn btn-primary">Save</button>
            </div>
        </div>
    </div>
</div>
  </form>       

Does anybody know why this is not working? No data is stored into the database. The database connection works fine. Thank you

**** Update:

I found my mistake. Here is the result:

    <?php

        $cat = $_POST['cat'];
        $id = $_POST['id'];

        if ( !empty($_POST)) {
            $pdo = Database::connect();
                $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                $sql = "UPDATE orders set cat = ?  WHERE id = '$id'";           
                $q = $pdo->prepare($sql);
                $q->execute(array($cat));
                Database::disconnect();

        }


    ?> 

And the form

    <form action="index.php" method="post">

    <div id="modal-content" class="modal fade" tabindex="-1" role="dialog">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">

                    <h3><?php echo $id ?></h3>
                </div>
                <div class="modal-body">
                    <p>
                        <input type="text" name="cat" />
                     <input type="hidden" name="id" value="1"/>
                    </p>
                </div>
                <div class="modal-footer"> 
                      <button type="submit" class="btn btn-primary">Save</button>
                </div>
            </div>
        </div>
    </div>
      </form>       

As already mentioned in the comments, you had an undefined $id .

    $cat = $_POST['cat'];

    if ( !empty($_POST)) {
        $pdo = Database::connect();
            $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $sql = "UPDATE orders set cat = ?  WHERE id = '$id'";           
            $q = $pdo->prepare($sql);
            $q->execute(array($cat));
            Database::disconnect();

    }


?> 

Instead you should have:

In your HTML:

<input type="hidden" name="check"/>

And then in your PHP:

if(isset($_POST['check'])){
        $cat = $_POST['cat'];
        $id = $_POST['id'];
            $pdo = Database::connect();
                $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                $sql = "UPDATE orders set cat = ?  WHERE id = '$id'";           
                $q = $pdo->prepare($sql);
                $q->execute(array($cat));
                Database::disconnect();

        }
    ?>

Is cat already a column in your database?

If not you need to make one. Otherwise you cant use update.

You're not doing any error checking. Try this:

if ( ! $q->execute(array($cat))) {
    echo "ERROR: ".$pdo->errorInfo();
}

That will tell you what is wrong.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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