Jump to content

MySQL Update more than one w/PHP


Lamez

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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.