Update mysql column using select and from a postDeleting an element from an array in PHPShould I use the datetime or timestamp data type in MySQL?How to get a list of user accounts using the command line in MySQL?How do I get a YouTube video thumbnail from the YouTube API?Insert into a MySQL table or update if existsSQL select only rows with max value on a columnHow to reset AUTO_INCREMENT in MySQL?php script echoing part of the php instead of what intendedHow do I import an SQL file using the command line in MySQL?'Invalid parameter number' error using bindParam to create an mySQL query
Does the United States guarantee any unique freedoms?
Does this Foo machine halt?
Did WWII Japanese soldiers engage in cannibalism of their enemies?
How would I as a DM create a smart phone-like spell/device my players could use?
Why was CPU32 core created, and how is it different from 680x0 CPU cores?
Is TA-ing worth the opportunity cost?
Colleagues speaking another language and it impacts work
Dereferencing a pointer in a for loop initializer creates a seg fault
Romyar Sharifi's notes: Group and Galois Cohomology; ideal vs submodule?
Why are the inside diameters of some pipe larger than the stated size?
Yajilin minicubes: the Hullabaloo, the Brouhaha, the Bangarang
Is this cheap "air conditioner" able to cool a room?
Acceptable to cut steak before searing?
Can a PC attack themselves with an unarmed strike?
Where to pee in London?
Why can I log in to my Facebook account with a misspelled email/password?
Are there any financial disadvantages to living significantly "below your means"?
Infeasibility in mathematical optimization models
In the movie Harry Potter and the Order or the Phoenix, why didn't Mr. Filch succeed to open the Room of Requirement if it's what he needed?
In a topological space if there exists a loop that cannot be contracted to a point does there exist a simple loop that cannot be contracted also?
Why should public servants be apolitical?
Why does Intel's Haswell chip allow multiplication to be twice as fast as addition?
As a 16 year old, how can I keep my money safe from my mother?
Can ads on a page read my password?
Update mysql column using select and from a post
Deleting an element from an array in PHPShould I use the datetime or timestamp data type in MySQL?How to get a list of user accounts using the command line in MySQL?How do I get a YouTube video thumbnail from the YouTube API?Insert into a MySQL table or update if existsSQL select only rows with max value on a columnHow to reset AUTO_INCREMENT in MySQL?php script echoing part of the php instead of what intendedHow do I import an SQL file using the command line in MySQL?'Invalid parameter number' error using bindParam to create an mySQL query
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I want to update a field with a default value 0 to 1 whenever the column match with the post values. 
The issue is I have two columns in my db, I want to use post values to compare whether it exist in the database and if it exist it should update the column which has a default value of 0 to 1. I have tried some code but is not working.
public function ticket()
// user coupon credentials
$validate_coupon = "";
$validate_coupon_err = "";
// Processing form data when form is submitted
if (isset($_POST['submit_coupon']))
// Check if coupon field is empty
if (empty(trim($_POST["usercoupon"])))
$validate_coupon_err = 'Please enter your coupon number.';
else
$usercoupon = htmlspecialchars($_POST["usercoupon"]);
$is_used = 1;
// Validate credentials
if (empty($validate_coupon_err))
// Prepare a select statement
$sql = "SELECT * FROM coupon WHERE coupon_value = :vcoupon LIMIT 1";
if ($stmt = $this->conn->prepare($sql))
// Bind variables to the prepared statement as parameters
$stmt->bindParam(':vcoupon', $param_vcoupon, PDO::PARAM_STR);
// Set parameters
$param_vcoupon = trim($_POST["usercoupon"]);
// Attempt to execute the prepared statement
if ($stmt->execute())
// Check if coupon exists, if yes then proceed
if ($stmt->rowCount() == 1)
$stmt = $this->conn->prepare('UPDATE coupon
SET is_used = :used
WHERE coupon_value=:coupon');
$stmt->bindParam(':used',$is_used);
$stmt->bindParam(':coupon',$usercoupon);
if ($row = $stmt->fetch())
/* coupon value is correct, so start a new session and
save the value to the session */
session_start();
$_SESSION['usercoupon'] = $usercoupon;
header("location: selectexams.php");
else
// Display an error message if coupon doesn't exist
$validate_coupon_err = 'No account found with that coupon.';
else
echo "Oops! Something went wrong. Please try again later.";
// Close statement
unset($stmt);
// Close connection
unset($pdo);
I want the is_used column value to change from 0 to 1 whenever the post value matches with the values in the coupon_value field.
php mysql
add a comment |
I want to update a field with a default value 0 to 1 whenever the column match with the post values. 
The issue is I have two columns in my db, I want to use post values to compare whether it exist in the database and if it exist it should update the column which has a default value of 0 to 1. I have tried some code but is not working.
public function ticket()
// user coupon credentials
$validate_coupon = "";
$validate_coupon_err = "";
// Processing form data when form is submitted
if (isset($_POST['submit_coupon']))
// Check if coupon field is empty
if (empty(trim($_POST["usercoupon"])))
$validate_coupon_err = 'Please enter your coupon number.';
else
$usercoupon = htmlspecialchars($_POST["usercoupon"]);
$is_used = 1;
// Validate credentials
if (empty($validate_coupon_err))
// Prepare a select statement
$sql = "SELECT * FROM coupon WHERE coupon_value = :vcoupon LIMIT 1";
if ($stmt = $this->conn->prepare($sql))
// Bind variables to the prepared statement as parameters
$stmt->bindParam(':vcoupon', $param_vcoupon, PDO::PARAM_STR);
// Set parameters
$param_vcoupon = trim($_POST["usercoupon"]);
// Attempt to execute the prepared statement
if ($stmt->execute())
// Check if coupon exists, if yes then proceed
if ($stmt->rowCount() == 1)
$stmt = $this->conn->prepare('UPDATE coupon
SET is_used = :used
WHERE coupon_value=:coupon');
$stmt->bindParam(':used',$is_used);
$stmt->bindParam(':coupon',$usercoupon);
if ($row = $stmt->fetch())
/* coupon value is correct, so start a new session and
save the value to the session */
session_start();
$_SESSION['usercoupon'] = $usercoupon;
header("location: selectexams.php");
else
// Display an error message if coupon doesn't exist
$validate_coupon_err = 'No account found with that coupon.';
else
echo "Oops! Something went wrong. Please try again later.";
// Close statement
unset($stmt);
// Close connection
unset($pdo);
I want the is_used column value to change from 0 to 1 whenever the post value matches with the values in the coupon_value field.
php mysql
Please set the$param_vcouponvariable before you bind it.
– srimaln91
Mar 27 at 7:04
After youprepare('UPDATE couponyou then call$stmt->fetch()- did you mean toexecute()this instead?
– Nigel Ren
Mar 27 at 7:11
@NigelRen that was the problem, thanks....
– Akua
Mar 27 at 7:21
1
Making a SELECT first to check whether the coupon code exists is superfluous - the UPDATE statement will only affect any record if that’s the case anyway. You should check that (the number of affected rows) after the UPDATE, and completely remove the SELECT.
– 04FS
Mar 27 at 7:48
add a comment |
I want to update a field with a default value 0 to 1 whenever the column match with the post values. 
The issue is I have two columns in my db, I want to use post values to compare whether it exist in the database and if it exist it should update the column which has a default value of 0 to 1. I have tried some code but is not working.
public function ticket()
// user coupon credentials
$validate_coupon = "";
$validate_coupon_err = "";
// Processing form data when form is submitted
if (isset($_POST['submit_coupon']))
// Check if coupon field is empty
if (empty(trim($_POST["usercoupon"])))
$validate_coupon_err = 'Please enter your coupon number.';
else
$usercoupon = htmlspecialchars($_POST["usercoupon"]);
$is_used = 1;
// Validate credentials
if (empty($validate_coupon_err))
// Prepare a select statement
$sql = "SELECT * FROM coupon WHERE coupon_value = :vcoupon LIMIT 1";
if ($stmt = $this->conn->prepare($sql))
// Bind variables to the prepared statement as parameters
$stmt->bindParam(':vcoupon', $param_vcoupon, PDO::PARAM_STR);
// Set parameters
$param_vcoupon = trim($_POST["usercoupon"]);
// Attempt to execute the prepared statement
if ($stmt->execute())
// Check if coupon exists, if yes then proceed
if ($stmt->rowCount() == 1)
$stmt = $this->conn->prepare('UPDATE coupon
SET is_used = :used
WHERE coupon_value=:coupon');
$stmt->bindParam(':used',$is_used);
$stmt->bindParam(':coupon',$usercoupon);
if ($row = $stmt->fetch())
/* coupon value is correct, so start a new session and
save the value to the session */
session_start();
$_SESSION['usercoupon'] = $usercoupon;
header("location: selectexams.php");
else
// Display an error message if coupon doesn't exist
$validate_coupon_err = 'No account found with that coupon.';
else
echo "Oops! Something went wrong. Please try again later.";
// Close statement
unset($stmt);
// Close connection
unset($pdo);
I want the is_used column value to change from 0 to 1 whenever the post value matches with the values in the coupon_value field.
php mysql
I want to update a field with a default value 0 to 1 whenever the column match with the post values. 
The issue is I have two columns in my db, I want to use post values to compare whether it exist in the database and if it exist it should update the column which has a default value of 0 to 1. I have tried some code but is not working.
public function ticket()
// user coupon credentials
$validate_coupon = "";
$validate_coupon_err = "";
// Processing form data when form is submitted
if (isset($_POST['submit_coupon']))
// Check if coupon field is empty
if (empty(trim($_POST["usercoupon"])))
$validate_coupon_err = 'Please enter your coupon number.';
else
$usercoupon = htmlspecialchars($_POST["usercoupon"]);
$is_used = 1;
// Validate credentials
if (empty($validate_coupon_err))
// Prepare a select statement
$sql = "SELECT * FROM coupon WHERE coupon_value = :vcoupon LIMIT 1";
if ($stmt = $this->conn->prepare($sql))
// Bind variables to the prepared statement as parameters
$stmt->bindParam(':vcoupon', $param_vcoupon, PDO::PARAM_STR);
// Set parameters
$param_vcoupon = trim($_POST["usercoupon"]);
// Attempt to execute the prepared statement
if ($stmt->execute())
// Check if coupon exists, if yes then proceed
if ($stmt->rowCount() == 1)
$stmt = $this->conn->prepare('UPDATE coupon
SET is_used = :used
WHERE coupon_value=:coupon');
$stmt->bindParam(':used',$is_used);
$stmt->bindParam(':coupon',$usercoupon);
if ($row = $stmt->fetch())
/* coupon value is correct, so start a new session and
save the value to the session */
session_start();
$_SESSION['usercoupon'] = $usercoupon;
header("location: selectexams.php");
else
// Display an error message if coupon doesn't exist
$validate_coupon_err = 'No account found with that coupon.';
else
echo "Oops! Something went wrong. Please try again later.";
// Close statement
unset($stmt);
// Close connection
unset($pdo);
I want the is_used column value to change from 0 to 1 whenever the post value matches with the values in the coupon_value field.
php mysql
php mysql
edited Mar 27 at 7:08
Magnus Eriksson
8,5564 gold badges15 silver badges28 bronze badges
8,5564 gold badges15 silver badges28 bronze badges
asked Mar 27 at 6:55
AkuaAkua
11 bronze badge
11 bronze badge
Please set the$param_vcouponvariable before you bind it.
– srimaln91
Mar 27 at 7:04
After youprepare('UPDATE couponyou then call$stmt->fetch()- did you mean toexecute()this instead?
– Nigel Ren
Mar 27 at 7:11
@NigelRen that was the problem, thanks....
– Akua
Mar 27 at 7:21
1
Making a SELECT first to check whether the coupon code exists is superfluous - the UPDATE statement will only affect any record if that’s the case anyway. You should check that (the number of affected rows) after the UPDATE, and completely remove the SELECT.
– 04FS
Mar 27 at 7:48
add a comment |
Please set the$param_vcouponvariable before you bind it.
– srimaln91
Mar 27 at 7:04
After youprepare('UPDATE couponyou then call$stmt->fetch()- did you mean toexecute()this instead?
– Nigel Ren
Mar 27 at 7:11
@NigelRen that was the problem, thanks....
– Akua
Mar 27 at 7:21
1
Making a SELECT first to check whether the coupon code exists is superfluous - the UPDATE statement will only affect any record if that’s the case anyway. You should check that (the number of affected rows) after the UPDATE, and completely remove the SELECT.
– 04FS
Mar 27 at 7:48
Please set the
$param_vcoupon variable before you bind it.– srimaln91
Mar 27 at 7:04
Please set the
$param_vcoupon variable before you bind it.– srimaln91
Mar 27 at 7:04
After you
prepare('UPDATE coupon you then call $stmt->fetch() - did you mean to execute() this instead?– Nigel Ren
Mar 27 at 7:11
After you
prepare('UPDATE coupon you then call $stmt->fetch() - did you mean to execute() this instead?– Nigel Ren
Mar 27 at 7:11
@NigelRen that was the problem, thanks....
– Akua
Mar 27 at 7:21
@NigelRen that was the problem, thanks....
– Akua
Mar 27 at 7:21
1
1
Making a SELECT first to check whether the coupon code exists is superfluous - the UPDATE statement will only affect any record if that’s the case anyway. You should check that (the number of affected rows) after the UPDATE, and completely remove the SELECT.
– 04FS
Mar 27 at 7:48
Making a SELECT first to check whether the coupon code exists is superfluous - the UPDATE statement will only affect any record if that’s the case anyway. You should check that (the number of affected rows) after the UPDATE, and completely remove the SELECT.
– 04FS
Mar 27 at 7:48
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55371392%2fupdate-mysql-column-using-select-and-from-a-post%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.
Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55371392%2fupdate-mysql-column-using-select-and-from-a-post%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Please set the
$param_vcouponvariable before you bind it.– srimaln91
Mar 27 at 7:04
After you
prepare('UPDATE couponyou then call$stmt->fetch()- did you mean toexecute()this instead?– Nigel Ren
Mar 27 at 7:11
@NigelRen that was the problem, thanks....
– Akua
Mar 27 at 7:21
1
Making a SELECT first to check whether the coupon code exists is superfluous - the UPDATE statement will only affect any record if that’s the case anyway. You should check that (the number of affected rows) after the UPDATE, and completely remove the SELECT.
– 04FS
Mar 27 at 7:48