Lamez Posted February 12, 2008 Share Posted February 12, 2008 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! Quote Link to comment https://forums.phpfreaks.com/topic/90605-mysql-update-more-than-one-wphp/ Share on other sites More sharing options...
peranha Posted February 12, 2008 Share Posted February 12, 2008 If the column names are 1,2,3, 4, 5, 6, 7 $q = "UPDATE `points` SET `1`=$a, '2'=$b, '3'=$c WHERE `1`='$a'"; This will set each variable to a column and update where 1 is equal to $a. If this is what you want. Not quite sure. Quote Link to comment https://forums.phpfreaks.com/topic/90605-mysql-update-more-than-one-wphp/#findComment-464529 Share on other sites More sharing options...
Lamez Posted February 12, 2008 Author Share Posted February 12, 2008 Ya that sounds like it, in my DB table I have 1 2 3 4 5 6 7 and I want to update them with the respective post variable (i.e $_POST['1'] = column 1) Quote Link to comment https://forums.phpfreaks.com/topic/90605-mysql-update-more-than-one-wphp/#findComment-464530 Share on other sites More sharing options...
Lamez Posted February 12, 2008 Author Share Posted February 12, 2008 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{ ?> Quote Link to comment https://forums.phpfreaks.com/topic/90605-mysql-update-more-than-one-wphp/#findComment-464536 Share on other sites More sharing options...
Lamez Posted February 12, 2008 Author Share Posted February 12, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/90605-mysql-update-more-than-one-wphp/#findComment-464558 Share on other sites More sharing options...
gardan06 Posted February 12, 2008 Share Posted February 12, 2008 have you tried checking your query for errors? if not, try echoing your query. your problem could be your query variable not generating the right query. Quote Link to comment https://forums.phpfreaks.com/topic/90605-mysql-update-more-than-one-wphp/#findComment-464652 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.