简体   繁体   中英

PHP MySQL not updating for CRUD app

I'm attempting to add the update function to my CRUD application. Essentially it uses the database specified, and uses the 'id' from the index.php page, which is 'productID' from the database. In another part of the application, a store management feature is included with the same skeleton Update page and works perfectly.

The database (Product) contains productID(PK), productName, productPrice, storeID(FK), productDate, productComments, productQuantity, and productPortion.

I'm certain it's within the PHP script, likely around the UPDATE command after using a few error checks but I can't seem to figure out what might be the main issue.

HTML

 <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <link href="css/bootstrap.min.css" rel="stylesheet"> <script src="js/bootstrap.min.js"></script> </head> <body> <div class="container"> <div class="span10 offset1"> <div class="row"> <h3>Update an Item</h3> </div> <form class="form-horizontal" action="update.php" method="post"> <input type="hidden" name="productID" value="<?php echo $id ?>"> <div class="control-group <?php echo !empty($nameError)?'error':'';?>"> <label class="control-label">Item</label> <div class="controls"> <input name="productName" type="text" placeholder="Product Name" value="<?php echo !empty($productName)?$productName:'';?>"> <?php if (!empty($nameError)): ?> <span class="help-inline"><?php echo $nameError;?></span> <?php endif;?> </div> </div> <div class="control-group <?php echo !empty($priceError)?'error':'';?>"> <label class="control-label">Price</label> <div class="controls"> <input name="productPrice" type="number" step="any" placeholder="Price" value="<?php echo !empty($productPrice)?$productPrice:'';?>"> <?php if (!empty($priceError)): ?> <span class="help-inline"><?php echo $priceError;?></span> <?php endif;?> </div> </div> <div class="control-group <?php echo !empty($storeError)?'error':'';?>"> <label class="control-label">Store</label> <div class="controls"> <select name="storeID" class="form-control"> <option value="">Select Store</option> <?php $pdo=D atabase::connect(); $sql='SELECT * FROM Store ORDER BY storeName DESC' ; foreach ($pdo->query($sql) as $row) { $selected = $row['storeID']==$storeID?'selected':''; echo ' <option value="'. $row['storeID'] .'" '. $selected .'>'. $row['storeName'] .'</option>'; } Database::disconnect(); ?> </select> <?php if (!empty($storeError)): ?> <span class="help-inline"><?php echo $storeError;?></span> <?php endif; ?> </div> </div> <div class="control-group <?php echo !empty($dateError)?'error':'';?>"> <label class="control-label">Date</label> <div class="controls"> <input name="productDate" type="date" step="any" placeholder="Date" value="<?php echo !empty($productDate)?$productDate:'';?>"> <?php if (!empty($dateError)): ?> <span class="help-inline"><?php echo $dateError;?></span> <?php endif;?> </div> </div> <div class="control-group <?php echo !empty($commentsError)?'error':'';?>"> <label class="control-label">Comments</label> <div class="controls"> <input name="productComments" type="text" placeholder="Comments" value="<?php echo !empty($productComments)?$productComments:'';?>"> <?php if (!empty($commentsError)): ?> <span class="help-inline"><?php echo $commentsError;?></span> <?php endif;?> </div> </div> <div class="control-group <?php echo !empty($quantityError)?'error':'';?>"> <label class="control-label">Quantity</label> <div class="controls"> <input name="productQuantity" type="number" placeholder="Quantity" value="<?php echo !empty($productQuantity)?$productQuantity:'';?>"> <?php if (!empty($quantityError)): ?> <span class="help-inline"><?php echo $quantityError;?></span> <?php endif;?> </div> </div> <div class="control-group <?php echo !empty($portionError)?'error':'';?>"> <label class="control-label">Portion</label> <div class="controls"> <input name="productPortion" type="number" placeholder="Portion" value="<?php echo !empty($productPortion)?$productPortion:'';?>"> <?php if (!empty($portionError)): ?> <span class="help-inline"><?php echo $portionError;?></span> <?php endif;?> </div> </div> <div class="form-actions"> <button type="submit" class="btn btn-success">Update</button> <a class="btn" href="index.php">Back</a> </div> </form> </div> </div> <!-- /container --> </body> </html>

PHP

 <?php require 'database.php'; $id = null; if ( !empty($_GET['id'])) { $id = $_REQUEST['id']; } if ( null==$id ) { header("Location: index.php"); } if ( !empty($_POST)) { // keep track validation errors $nameError = null; $priceError = null; $storeError = null; $dateError = null; $quantityError = null; $portionError = null; // keep track post values $id = $_POST['id']; $storeID= $_POST['storeID']; $productName = $_POST['productName']; $productPrice = $_POST['productPrice']; $productQuantity = $_POST['productQuantity']; $productPortion = $_POST['productPortion']; $productComments = $_POST['productComments']; $productDate = $_POST['productDate']; //error displayed for creation errors $valid = true; if (empty($productName)) { $nameError = 'Please enter the name of the product'; $valid = false; } if (empty($productPrice)) { $priceError = 'Please enter a price'; $valid = false; } if (empty($storeID)) { $storeError = 'Please enter a store'; $valid = false; } if (empty($productDate)) { $dateError = 'Please enter the purchase date'; $valid = false; } if (empty($productComments)) { $commentsError = 'Please enter any comments'; $valid = false; } if (empty($productQuantity)) { $quantityError = 'Please select the quantity'; $valid = false; } if (empty($productPortion)) { $portionError = 'Please enter the portion'; $valid = false; } // insert data if ($valid) { $pdo = Database::connect(); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $sql = "UPDATE Product SET productName=?, productPrice=?, storeID=?, productDate=?, productComments=?, productQuantity=?, productPortion=? WHERE productID=?"; $q = $pdo->prepare($sql); $q->execute(array($productName,$productPrice,$storeID,$productDate, $productComments,$productQuantity,$productPortion,$id)); Database::disconnect(); header("Location: index.php"); } } else { $pdo = Database::connect(); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $sql = "SELECT * FROM Product WHERE productID = ?"; $q = $pdo->prepare($sql); $q->execute(array($id)); $data = $q->fetch(PDO::FETCH_ASSOC); $productName = $data['productName']; $productPrice = $data['productPrice']; $storeID = $data['storeID']; $productQuantity = $data['productQuantity']; $productPortion = $data['productPortion']; $productComments = $data['productComments']; $productDate = $data['productDate']; Database::disconnect(); } ?>

Having a quick look at your code you are sending the form data via $_POST and on the php script checking $_GET then grabbing the id from $_REQUEST. Try changing

if ( !empty($_GET['id'])) {
        $id = $_REQUEST['id'];
    }

to

if ( !empty($_POST['id'])) {
        $id = $_POST['id'];
    }

Hope that helps!

Thanks Donniep!

I found that the answer was actually related to the POST values after being submitted. My impression was that I could still use the value from the GET call of 'id', but I instead needed to use the actual ID value from the product DB instead. The solution turned out to be:

 // keep track post values $id = $_POST['id'];

Needed to be changed to:

 // keep track post values $id = $_POST['productID'];

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