954,587 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

MySQL Db is not updating from html form with php script

Hi,

I am trying to update data from a column in a database using a php script. But for some reason it doesn't update the database, but i get no error feedback.
So first i select a random entry from the database and i add it as a parameter to the next page. This page contains the form and the php script to get the parameter:

<?
  $server = "myserver";
  $username = "myusername";
  $password = "mypass";
  $database = "dbname";
  $con = mysql_connect($server, $username, $password);
  $ok = mysql_select_db($database, $con);


' ' . htmlspecialchars($_GET["param"]) . '';

if(empty($_GET))
    echo "<h2>The request is invalid</h2>";
else
	$param_code = ($_GET[param]); 
	
?>
    <form name="form1" method="post" action="insert.php">
    	<input type="text" name="param" disabled value="<? print_r ($param_code); ?>">
		<input type="submit" name="submit" id="submit" value="Submit">
    </form>


here is the insert.php:

<?
  $server = "myserver";
  $username = "myusername";
  $password = "mypass";
  $database = "dbname";
  $con = mysql_connect($server, $username, $password);
  $ok = mysql_select_db($database, $con);
  
if(isset($_POST['submit']))
{
   $submit = $_POST['param_code'];
}

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

mysql_close($con);

     ?>


My database is very simple. It has only one table and 3 columns.

| id | param_code | code_used|
| 1  | abc2qsaa   |     1    |  --> This is a not used code
| 2  | safd3235   |     0    |  --> This is a used code.
observ
Newbie Poster
22 posts since Jul 2011
Reputation Points: 10
Solved Threads: 0
 

<input type="text" name="param" ...> means you need to use $submit = $_POST['param'];

pritaeas
Posting Expert
Moderator
5,484 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 

And i am using it like that, it was an edit error..(just checked)

observ
Newbie Poster
22 posts since Jul 2011
Reputation Points: 10
Solved Threads: 0
 

the error is in line fourteen. To update the "mysql_query" requires connection identifier, like this;

mysql_query(" UPDATE table_name SET code_used= '0' WHERE param_code ='{$submit}' ", $con) or die (mysql_error());
faroukmuhammad
Junior Poster
140 posts since Sep 2008
Reputation Points: 20
Solved Threads: 16
 

Just updated it, and is still not working. The database remains un-updated

observ
Newbie Poster
22 posts since Jul 2011
Reputation Points: 10
Solved Threads: 0
 

any error massage?

faroukmuhammad
Junior Poster
140 posts since Sep 2008
Reputation Points: 20
Solved Threads: 16
 

No error, the webpage acts like it just did what it was supposed to.
Is there a way to check that the $submit parameter is correctly imported? Like print_r or something similar (I tried print_r but it didn't work)

observ
Newbie Poster
22 posts since Jul 2011
Reputation Points: 10
Solved Threads: 0
 

O.k, unless the field "param_code" has its contents with curly bracket, you need to remove the curly bracket from the update query. Like this;

mysql_query(" UPDATE table_name SET code_used= '0' WHERE param_code ='$submit' ", $con) or die (mysql_error());
faroukmuhammad
Junior Poster
140 posts since Sep 2008
Reputation Points: 20
Solved Threads: 16
 

Hey, i updated the script. Still no changes to the database. I think there is something i am missing

observ
Newbie Poster
22 posts since Jul 2011
Reputation Points: 10
Solved Threads: 0
 
Hey, i updated the script. Still no changes to the database. I think there is something i am missing


What do you currently have?

evstevemd
Senior Poster
3,713 posts since Jun 2007
Reputation Points: 462
Solved Threads: 392
 

The logic of the website is like this: When you get to it, you will get a coupon, if you press use the coupon then that coupon cannot be used again. This is why i am trying to update the database with value 0 for coupon used.

Here is the code again:


page1.php

<?
$server = "myserver";
  $username = "myusername";
  $password = "mypass";
  $database = "dbname";
  $con = mysql_connect($server, $username, $password);
  $ok = mysql_select_db($database, $con);

$num_displayed = 1 ;

$result = mysql_query ("SELECT * FROM coupons WHERE coupon_used=1 ORDER BY RAND() LIMIT $num_displayed");


while ($row = mysql_fetch_array($result)) 

{

echo "<a href=\"coupon.php?coupon=".$row["coupon_code"]."\">Get a coupon</a>" ;
}
?>


coupon.php

<?
  $server = "myserver";
  $username = "myusername";
  $password = "mypass";
  $database = "dbname";
  $con = mysql_connect($server, $username, $password);
  $ok = mysql_select_db($database, $con);

' ' . htmlspecialchars($_GET["coupon"]) . '';

if(empty($_GET))
    echo "<h2>The request is invalid</h2>";
else
	$coupon_code = ($_GET[coupon]); 
	
?>
<--!html form-->
    <form name="forma1" method="post" action="insert.php">
    	<input type="text" name="couponcode" disabled value="<? print_r ($coupon_code); ?>">
		<input type="submit" name="usecode" value="Use Coupon">
    </form>


insert.php:

<?

  $server = "myserver";
  $username = "myusername";
  $password = "mypass";
  $database = "dbname";
  $con = mysql_connect($server, $username, $password);
  $ok = mysql_select_db($database, $con);
  
  
  if(isset($_POST['usecode']))
{
  $submit = $_POST['couponcode'];
  $sql = " UPDATE coupons SET coupon_used= '0' WHERE coupon_code = '$submit' ";
}

mysql_query($sql, $con) or die(mysql_error());
  
mysql_close($con);

?>


database example:

| id | param_code | code_used|
| 1  | abc2qsaa   |     1    |  --> This is a not used code
| 2  | safd3235   |     0    |  --> This is a used code.


[/QUOTE]

observ
Newbie Poster
22 posts since Jul 2011
Reputation Points: 10
Solved Threads: 0
 

Check line number 9 in coupon.php.
' ' . htmlspecialchars($_GET["coupon"]) . '';
What is this ? Perhaps, you should have.

$coupon = htmlspecialchars($_GET["coupon"]);

And then, if...else statement should go like below

if($coupon == ''){
echo "<h2>The request is invalid</h2>";
}
else{
$coupon_code = $coupon; 
}

And the final one is, print_r in input field. Line 19 in coupon.php.
Replace above with:

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

'<? ?>', may be the problem, you must enable to support this syntax in php.ini file.

Zero13
Practically a Master Poster
624 posts since Jan 2009
Reputation Points: 120
Solved Threads: 139
 

well, the code as i posted it works if i change this line:

$sql = " UPDATE coupons SET coupon_used= '0' WHERE coupon_code = '$submit' ";

to

$sql = " UPDATE coupons SET coupon_used= '0' WHERE coupon_code = 'abc2qsaa' ";


where abc2qsaa is one of the codes in my database.

I have made the changes you suggested as well, but the database still doesn't get updated.

observ
Newbie Poster
22 posts since Jul 2011
Reputation Points: 10
Solved Threads: 0
 

well, the code as i posted it works if i change this line:

to

$sql = " UPDATE coupons SET coupon_used= '0' WHERE coupon_code = 'abc2qsaa' ";

where abc2qsaa is one of the codes in my database.

I have made the changes you suggested as well, but the database still doesn't get updated.


this information is useful. Now, print the value of $submit and hence whole $sql and post result

evstevemd
Senior Poster
3,713 posts since Jun 2007
Reputation Points: 462
Solved Threads: 392
 
this information is useful. Now, print the value of $submit and hence whole $sql and post result


Is not printing it. Maybe i am doing it wrong:

if(isset($_POST['usecode']))
{
  $submit = $_POST['couponcode'];
  $sql = " UPDATE coupons SET coupon_used= '0' WHERE coupon_code = '$submit' ";
  print $submit;
}
observ
Newbie Poster
22 posts since Jul 2011
Reputation Points: 10
Solved Threads: 0
 

Is not printing it. Maybe i am doing it wrong:

if(isset($_POST['usecode']))
{
  $submit = $_POST['couponcode'];
  $sql = " UPDATE coupons SET coupon_used= '0' WHERE coupon_code = '$submit' ";
  print $submit;
}


do this

print_r($_POST);
echo "";
 if(isset($_POST['usecode']))
{
  $submit = $_POST['couponcode'];
  $sql = " UPDATE coupons SET coupon_used= '0' WHERE coupon_code = '$submit' ";
  echo $submit;
die()
}
evstevemd
Senior Poster
3,713 posts since Jun 2007
Reputation Points: 462
Solved Threads: 392
 

This is shown on the webpage:
Array ( [usecode] => Use Coupon )

observ
Newbie Poster
22 posts since Jul 2011
Reputation Points: 10
Solved Threads: 0
 

Remove white space from this line.
$sql = " UPDATE coupons SET coupon_used= '0' WHERE coupon_code = '$submit' ";
Should be

$sql = "UPDATE `coupons` SET `coupon_used` = '0' WHERE `coupon_code` = '$submit'";

And print the above the query line, copy and past it into phpMyadmin and run it manually, and check how it works.

Zero13
Practically a Master Poster
624 posts since Jan 2009
Reputation Points: 120
Solved Threads: 139
 

The sql command works without the " ' " . As i said above:

well, the code as i posted it works if i change this line:

$sql = " UPDATE coupons SET coupon_used= '0' WHERE coupon_code = '$submit' ";

to

$sql = " UPDATE coupons SET coupon_used= '0' WHERE coupon_code = 'abc2qsaa' ";

where abc2qsaa is one of the codes in my database.

observ
Newbie Poster
22 posts since Jul 2011
Reputation Points: 10
Solved Threads: 0
 
Array ( [usecode] => Use Coupon )


As you posted above. $submit has wrong value stored. Check where $submit comes from.

Zero13
Practically a Master Poster
624 posts since Jan 2009
Reputation Points: 120
Solved Threads: 139
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You