0

everyone.

I have a problem with updating value into database.

Now, I have $SumTotal as a PHP variable. I want to update value in database by using value in $SumTotal.

I try it but it doesn't work. The value in database is 0.

here is my code

$strSQL3 = "UPDATE OrderCustomer SET TotalPrice = '".$SumTotal."' WHERE OrderCustomerID = '".$_SESSION["OrderCustomerID"]."' ";

Thank you very much.

Xanthics
  • 3
  • 2

3 Answers3

0

Try using this code, this code should work for you.

{
$dbhost = 'localhost:3036';
$dbuser = 'root';
$dbpass = 'rootpassword';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}

$SumTotal="**Some value**";
//**QUERY**
$strSQL3= "UPDATE OrderCustomer".
       "SET Totalprice= $SumTotal".
       "WHERE emp_id = $emp_id" ;

mysql_select_db('test_db');
$retval = mysql_query( $strSQL3, $conn );
if(! $retval )
{
  die('Could not update data: ' . mysql_error());
}
echo "Updated data successfully\n";
mysql_close($conn);
}
Ragesh Kr
  • 1,573
  • 8
  • 29
  • 46
  • _"**Warning** This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used."_ – Debflav Jul 29 '14 at 11:45
  • mysql_* are deprecated. So, please don't encourage the use of mysql_*. – Debflav Jul 29 '14 at 13:06
0

First of all, I would make sure that $SumTotal and $_SESSION['OrderCustomerID'] are equal to what they are meant to be equal to.

I would do something like: echo "$SumTotal"; and echo $_SESSION['OrderCustomerID']; to check these variables.

Then, you could do the following:

  • Make sure that the database is actually selected (to select the database in your query, you can use UPDATE databasename.table (where table is equal to OrderCustomer in your case)
  • Check for errors in your query by adding or die(mysql_error()); to the end of your query.
  • Use the following at the very top of your PHP document to show all errors that have occurred: https://stackoverflow.com/a/6575502/3593228.

In addition to this, make sure that your query is actually being executed.

You can do this by using the mysql_query function as follows:

$strSQL3 = mysql_query("UPDATE databasename.OrderCustomer SET TotalPrice = '$SumTotal' WHERE OrderCustomerID = '" . $_SESSION["OrderCustomerID"] . "'");

Also, before someone beats me to it, you should be using PDO or MySQL Improved, yada yada yada.

Community
  • 1
  • 1
Alex Yorke
  • 48
  • 1
  • 8
0

You can have a look at this piece of code.

This will definitely work, i have tried this personally.

Edit: (Pulled from link)

<html>
<head>
<title>Update a Record in MySQL Database</title>
</head>
<body>

<?php
if(isset($_POST['update']))
{
$dbhost = 'localhost:3036';
$dbuser = 'root';
$dbpass = 'rootpassword';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}

$emp_id = $_POST['emp_id'];
$emp_salary = $_POST['emp_salary'];

$sql = "UPDATE employee ".
       "SET emp_salary = $emp_salary ".
       "WHERE emp_id = $emp_id" ;

mysql_select_db('test_db');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not update data: ' . mysql_error());
}
echo "Updated data successfully\n";
mysql_close($conn);
}
else
{
?>
<form method="post" action="<?php $_PHP_SELF ?>">
<table width="400" border="0" cellspacing="1" cellpadding="2">
<tr>
<td width="100">Employee ID</td>
<td><input name="emp_id" type="text" id="emp_id"></td>
</tr>
<tr>
<td width="100">Employee Salary</td>
<td><input name="emp_salary" type="text" id="emp_salary"></td>
</tr>
<tr>
<td width="100"> </td>
<td> </td>
</tr>
<tr>
<td width="100"> </td>
<td>
<input name="update" type="submit" id="update" value="Update">
</td>
</tr>
</table>
</form>
<?php
}
?>
</body>
</html>
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Ragesh Kr
  • 1,573
  • 8
  • 29
  • 46