I'm not sure if I can answer your question directly. I'd say double check the correct param is being passed - because if you type the wrong param you wont get an error and nothing will update.

Anyways, I went ahead and tried to rewrite the code. This should work and give you errors and success messages:

<?php
/****************************************************
*                     Settings
*****************************************************/
// MySQL Connection Settings
$settings['mysql']['host'] = "localhost";
$settings['mysql']['user'] = "root";
$settings['mysql']['pass'] = "af981t58time";
$settings['mysql']['dbname'] = "dev_andy_sms";
/****************************************************
*                     Connections
*****************************************************/
$con = mysql_connect($settings['mysql']['host'], $settings['mysql']['user'], $settings['mysql']['pass']) or die('Error: Could not connect to mysql, Please check server, username and password.');
@mysql_query("CREATE DATABASE IF NOT EXISTS ".$settings['mysql']['dbname']);
$ok = mysql_select_db($settings['mysql']['dbname'], $con) or die("Could not connect to the specified database on line ".__LINE__.": ".mysql_error());

//Set the param variable from the URL via $_GET
$gparam = mysql_real_escape_string(htmlspecialchars(trim($_GET["param"])));
?>
<form action="<?php echo $PHP_SELF; ?>" method="post" name="form1">
<input type="text" name="param" value="<?php echo $gparam; ?>" disabled="disabled" />
<input type="submit" name="submit" id="submit" value="Submit" />
</form>
<?php
if(isset($_POST['submit'])){
//Get param from the form
$param = mysql_real_escape_string(htmlspecialchars(trim($_POST["param"])));
if(!empty($param)){

//First, lets check if the param exists...if you type the wrong param, then you wont get an error message and no result.
$c = mysql_query("SELECT * FROM table_name WHERE param_code='$param' LIMIT 1");
$cc = mysql_num_rows($c);

if($cc > 0){
$ud = mysql_query("UPDATE table_name SET code_used='0' WHERE param_code='$param'");
if($ud){
echo "Successfully Updated!";
}else{
echo "There was an error: ".mysql_error();
}
}else{
echo "There Param does not exists in the database!";
/*$code = "0";
// or
$code = "1";
$ins = mysql_query("INSERT INTO table_name (id,param_code,code_used) VALUES (id,'$param','$code')");

if($ins){
echo "Successfully Inserted!";
}else{
echo "There was an error inserting: ".mysql_error();
}
*/
}
}else{
echo "No param was present";
}
}
 
mysql_close($con);
 
?>

I got no param was present...

O.k remove the apostrophe enclosing the "0", if the field is not char, varchar, text, etc.

mysql_query(" UPDATE table_name SET code_used= 0 WHERE param_code ='$submit' ", $con) or die (mysql_error());

In My Installation it makes no difference whether I use 0 or '0' in my TINYINT(1)

I got no param was present...

tried my suggestion?

tried my suggestion?

Yes, i get request is invalid and the coupon code doesn't transfer in the text box anymore.

I have

<input type="text" name="couponcode" disabled value="<?php echo $coupon_code; ?>">

should i switch it back to print_r ?

I got no param was present...

So it sounds like you are submitting a code in the form that does not already exist in the database. Take 10 minutes to go through the code and understand it, you will see what I mean.

Another suggestion, if all else fails, comment your code thoroughly, for every single line, for ever action, and include why you do it, or what you are trying to do with it. I think you are missing something like, using $_GET instead of $_POST, etc. Go through and comment everything so we can see what you are trying to accomplish. You code seamed to have code that was not being used.

Eureka, your post is not delivering data to insert.php because of the "disable" tag in the input box. I just tried the two and i found that the one with disable is not sending the disabled value while the one without does...

commented: You got it right! +13

Brilliant!

Eureka, your post is not delivering data to insert.php because of the "disable" tag in the input box. I just tried the two and i found that the one with disable is not sending the disabled value while the one without does...

Bingo - now it works

Thank you so much

Is there a way to also check in your code if the code is already used ? I tried to use on your code from here: http://www.daniweb.com/web-development/php/threads/372581/1603563#post1603563

the following:

$used = mysql_query ("SELECT * FROM coupons WHERE coupon_used=0 AND coupon_code='$param'");
if($param=$used){
	echo "Coupon already used";
}else{
	echo "";
}

Yea, of course. In order to check if the code is already in use, first you select from database where code=yourcode, and then we count the results. If the code is in use, you will get a $results > 0, otherwise results il be == 0. See code:

$c = mysql_query("SELECT * FROM table_name WHERE param_code='$param' LIMIT 1");
$cc = mysql_num_rows($c);
if($cc > 0){

echo "Code is already in use."; 

//Do you need to update the existing code? If so, do it here

}else{

echo "Code is not already in use. ";


//If you would like to add the code to the database, do it here.

}
*/

Did I answer your question?

Sorry i am confused. Maybe the question was wrongly put.
After i press submit on the form, if someone tries to press again submit for the same code again i would like to show : "Code is already in use" message.

My approach was to look in the db for the code_used = 0 when the submited code is found in the database.

That's what the previous code does. You may need to modify the SQL, but it checks for results and gives a message if they exist or not.

So, any combination of SQL queries can produce your desired results:

$c = mysql_query("SELECT * FROM table_name WHERE code='0' && param='$param' LIMIT 1");

Do you understand how it works? If you want to check if something exists, query for the result, and count how many results were returned. If you don't get any results returned then it doesn't exist.

Eureka, your post is not delivering data to insert.php because of the "disable" tag in the input box. I just tried the two and i found that the one with disable is not sending the disabled value while the one without does...

Man, you are brilliant. I was finding a fault all around the room and I didn't see that needle.
Bravo!

First of all, thank you to everyone for helping me with this. It takes me one step(or more actually) further to learning web design.

I still need you help on showing the user an code already used error if he tries to use that again. I will post here if in the mean time i find how.

@cjohnweb: I am sorry, since i am a beginner, i don;t know exactly how to implement in the script that part (and at the same time keeping what you gave me already).

First of all, thank you to everyone for helping me with this. It takes me one step(or more actually) further to learning web design.

I still need you help on showing the user an code already used error if he tries to use that again. I will post here if in the mean time i find how.

@cjohnweb: I am sorry, since i am a beginner, i don;t know exactly how to implement in the script that part (and at the same time keeping what you gave me already).

You are welcome!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.