Jump to content

Recommended Posts

How do I use MySQL's update function with more than one value?

 

I have values that range from 1-7, but I do not know how to update more than one column

 

here is my code

 

<?php
$getlink = $_GET["action"];
$link = md5("addpointstodb");
if ($getlink == $link) {
if(isset $_POST['Submit'])){
   $q = "Select count(*) as row_count from `points`";
   $r = mysql_query($q) or die(mysql_error()."<br /><br />".$q);
  $a = $_POST['1'];
  $b = $_POST['2'];
  $c = $_POST['3'];
  $d = $_POST['4'];
  $e = $_POST['5'];
  $f = $_POST['6'];
  $g = $_POST['7'];
  
  if(mysql_num_rows($r) >0){
    $q = "UPDATE `points` SET `1`=$a WHERE `1`='$a'";
mysql_query($q);
  }else{
    $q = "INSERT INTO `points` VALUES ('$a', '$b', '$c', '$d', '$e', '$f', '$g')";
    mysql_query($q);
  }  

}else{
?>

 

-Thanks!

Link to comment
https://forums.phpfreaks.com/topic/90605-mysql-update-more-than-one-wphp/
Share on other sites

I think there is something wrong with my query, it is not adding it to the database

 

is there something wrong in my code?

 

<?php
$getlink = $_GET["action"];
$link = md5("addpointstodb");
if ($getlink == $link) {
if(isset ($_POST['Submit'])){
   $q = "Select count(*) as row_count from `points`";
   $r = mysql_query($q) or die(mysql_error()."<br /><br />".$q);
  $a = $_POST['1'];
  $b = $_POST['2'];
  $c = $_POST['3'];
  $d = $_POST['4'];
  $e = $_POST['5'];
  $f = $_POST['6'];
  $g = $_POST['7'];
  
  if(mysql_num_rows($r) >0){
$q = "UPDATE `points` SET `1`=$a, `2`=$b, `3`=$c, `4`=$d, `5`=$e , `6`=$f, `7`=$g WHERE `1`='$a', `2`='$b', `3`='$c', `4`='$d', `5`='$e', `6`='$f', `7`='$g'";
echo "Update Points to Database";
mysql_query($q);
  }else{
    $q = "INSERT INTO `points` VALUES ('$a', '$b', '$c', '$d', '$e', '$f', '$g')";
    mysql_query($q);
echo "Add points to Database";
echo '<br><br><a href="pinval.php">View Points</a>';
  }  
}else{
echo "No data to enter";
echo '<br><br><a href="pinval.php">Try Again</a>';
}
}else{
?>

Alright I changed the code to:

<?php
$getlink = $_GET["action"];
$link = md5("addpointstodb");
if ($getlink == $link) {
if(isset ($_POST['Submit'])){
  $a = $_POST['1'];
  $b = $_POST['2'];
  $c = $_POST['3'];
  $d = $_POST['4'];
  $e = $_POST['5'];
  $f = $_POST['6'];
  $g = $_POST['7'];
  
   $q = "Select count(*) as row_count from `points`";
   $r = mysql_query($q) or die(mysql_error()."<br /><br />".$q);
  if(mysql_num_rows($r) >0){
$q = "UPDATE `points` SET `1`=$a, `2`=$b, `3`=$c, `4`=$d, `5`=$e , `6`=$f, `7`=$g WHERE `1`='$a'";
mysql_query($q)or die(mysql_error());
echo "Updated Points to Database";
echo '<br><br><a href="pinval.php">View Points</a>';
  }else{
    $q = "INSERT INTO `points` VALUES ('$a', '$b', '$c', '$d', '$e', '$f', '$g')";
    mysql_query($q);
echo "Added points to Database";
echo '<br><br><a href="pinval.php">View Points</a>';
  }  
}else{
echo "No data to enter";
echo '<br><br><a href="pinval.php">Try Again</a>';
}
}else{
//HTML FORM BELOW
?>

 

And it still will not add the newly written values to the DB!

 

WHY?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.