简体   繁体   中英

How can I manage to change a field in my SQL DB, after i checked a value before?

在此处输入图片说明

So. I Have this Table. I'm trying to update the value of the Availability filed from 0 to 1 after I checked that a code, for example 1234 exist in my DB.

Here is the code what I wrote:

<?php 
if(isset($_POST['kodi'])) {
try {
$sql = "SELECT codevalue FROM codes WHERE codevalue = ? AND availability = 0";
$stmt = $dbh->prepare($sql);
$stmt->bindParam(1, $_POST['kodi']);
$stmt->execute(); }
catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();}

if($stmt->rowCount() > 0){
header('Location: http://mysite/userpage');
$q = "UPDATE codes SET availability = '1' WHERE codevalue= ?";
$stmt->bindParam(1, $_POST['kodi']);
$stmt = $dbh->prepare($q);
$stmt->execute($q);} else {
echo "Code Not Exists"; }

The part with the $dbh variable:

$dbh = new PDO("mysql:host=$hostname;dbname=x",$username,$password);

The HTML:

<form action ="thispage.php" method ="POST">
<input name="kodi" type="text" class="form-control" placeholder="Enter Code" aria-describedby="basic-addon1">
</form>

When I'm trying it, it redirects in my userpage but the field availability is still 0.

if(isset($_POST['kodi'])) {
$sql = "SELECT codevalue FROM codes WHERE codevalue = ? AND availability = 0";
$stmt = $dbh->prepare($sql);
$stmt->bindParam(1, $_POST['kodi']);
$stmt->execute();
$result = $stmt->fetchAll();
if(!empty($result)) {
header('Location: http://mysite/userpage');
$q = "UPDATE codes SET availability = '1' WHERE codevalue= ? AND availability = 0";
$stmt->bindParam(1, $_POST['kodi']);
$stmt = $dbh->prepare($q);
$stmt->execute($q);
} else {
echo "Code Not Exists"; 
} 
}

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