Obcure
Obcure

Reputation: 991

Remove Value from MYSQL table

I have a mysql table with a field called points and that value is 19. When i change the value using:

<?php
$con=mysqli_connect("blah", 'blah', 'blah', "blah");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$sql="INSERT INTO Social_points (`points`)
VALUES
('$_POST[Jpoints]')";

if (!mysqli_query($con,$sql))
  {
  die('Error: ' . mysqli_error($con));
  }
echo "1 record added";

mysqli_close($con);
?>

Which read the form data from a previous page it works fine however it merely adds the value to the end of the original value for e.g. if i had 19 as original value and then entered 5 in the form it would change to 195. Any ideas

Upvotes: 0

Views: 128

Answers (5)

ciruvan
ciruvan

Reputation: 5213

This is just a wild stab in the dark:

You're using the POST method, so I guess you're calling that script via AJAX. And my next guess is, you probably do the addition of the original value and the new value with Javascript, before the form gets sent.

So what probably happens in Javascript is this: Instead of adding both values, they are concatenated and then sent to the DB, being treated as a string (JPoints).

If i'm right, you need to cast both values to int in your Javascript.

Upvotes: 1

Erdem Ece
Erdem Ece

Reputation: 1775

read the data from database.

for example you add an item

take the ID of last added item and update it.

<?php
    // before this you have a query for last added item.
    $data['itemid'];
    $_POST['newvalue'];
    $data['value'];

    //if the value 19 and the new value 5 this will become 195
    $newvalue = trim($data['value'] . $_POST['newvalue']);

    $sql->query("UPDATE tablename SET value = '.$newvalue.' WHERE itemid = '.$data['itemid'].'");
?>

Upvotes: 0

zxc
zxc

Reputation: 1526

Try this mate

update social_points set points = convert(varchar(5),points)+'5'

Upvotes: 1

Tredged
Tredged

Reputation: 501

Use the UPDATE syntax and not insert..

$sql="UPDATE Social_points set (`points`)
VALUES
('$_POST[Jpoints]')";

Upvotes: 1

Bindiya Patoliya
Bindiya Patoliya

Reputation: 2764

If You have to add value as new record then use INSERT query otherwise use UPDATE query.

Upvotes: 1

Related Questions