contra10 Posted December 23, 2008 Share Posted December 23, 2008 I seem to be having trouble moving a variable from one of my tables to another <?php mysql_connect("localhost", "root", "") or die(mysql_error()); mysql_select_db("registration") or die(mysql_error()); if(is_numeric($_GET['grp'])){ $id = $_GET['grp']; $query= "SELECT * FROM groups WHERE id = $id"; $result = mysql_query($query) or die(mysql_error());; $group = mysql_fetch_assoc($result); $groupname = $group['name']; } if (isset($_POST['submit'])){ mysql_connect("localhost", "root", "") or die(mysql_error()); mysql_select_db("registration") or die(mysql_error()); $insert = "INSERT INTO users (mygroups) WHERE username = '$username' VALUES ('$groupname')"; $add_group = mysql_query($insert) or die(mysql_error()); ?> <h1>Good Job</h1> <p>Thank you<a href= "http://localhost/main"</a>Main</p> <?php } else { ?> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <input type='submit' name='submit' value='Add Group'></td>"; </form> <?php } ?> Link to comment https://forums.phpfreaks.com/topic/138195-solved-moving-a-variable-from-one-table-to-another/ Share on other sites More sharing options...
Maq Posted December 23, 2008 Share Posted December 23, 2008 You don't get an error for this? $insert = "INSERT INTO users (mygroups) WHERE username = '$username' VALUES ('$groupname')"; I think it should be: $insert = "INSERT INTO users (mygroups) VALUES ('$groupname') WHERE username = '$username'"; Link to comment https://forums.phpfreaks.com/topic/138195-solved-moving-a-variable-from-one-table-to-another/#findComment-722489 Share on other sites More sharing options...
contra10 Posted December 23, 2008 Author Share Posted December 23, 2008 the error i get now is You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE username = 'qwe'' at line 2 the username is the one i logged in with Link to comment https://forums.phpfreaks.com/topic/138195-solved-moving-a-variable-from-one-table-to-another/#findComment-722493 Share on other sites More sharing options...
Maq Posted December 23, 2008 Share Posted December 23, 2008 What error were you getting before? Could you also post your current code with ALL of the errors? Link to comment https://forums.phpfreaks.com/topic/138195-solved-moving-a-variable-from-one-table-to-another/#findComment-722498 Share on other sites More sharing options...
premiso Posted December 23, 2008 Share Posted December 23, 2008 There cannot be a WHERE in the insert. You would use the UPDATE syntax to perform that. $insert = "UPDATE users SET mygroups = '$groupname' WHERE username = '$username'"; If you are indeed meaning to insert, the question is why are you trying to choose a specific username? Usually you insert their username in there. I think you meant update though. Link to comment https://forums.phpfreaks.com/topic/138195-solved-moving-a-variable-from-one-table-to-another/#findComment-722499 Share on other sites More sharing options...
contra10 Posted December 23, 2008 Author Share Posted December 23, 2008 ok the last code went through without any errors $insert = "UPDATE users SET mygroups = '$groupname' WHERE username = '$username'"; but when i look in the row mygroups there is no value, i wonder if the other name value is not being transferred over... heres the updated code <?php mysql_connect("localhost", "root", "") or die(mysql_error()); mysql_select_db("registration") or die(mysql_error()); if(is_numeric($_GET['grp'])){ $id = $_GET['grp']; $query= "SELECT * FROM groups WHERE id = $id"; $result = mysql_query($query) or die(mysql_error());; $group = mysql_fetch_assoc($result); $groupname = $group['name']; } if (isset($_POST['submit'])){ mysql_connect("localhost", "root", "") or die(mysql_error()); mysql_select_db("registration") or die(mysql_error()); $insert = "UPDATE users SET mygroups = '$groupname' WHERE username = '$username'"; $add_group = mysql_query($insert) or die(mysql_error()); ?> <h1>Good Job</h1> <p>Thank you<a href= "http://localhost/main"</a>Main</p> <?php } else { ?> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <input type='submit' name='submit' value='Add Group'></td>"; </form> <?php } ?> Link to comment https://forums.phpfreaks.com/topic/138195-solved-moving-a-variable-from-one-table-to-another/#findComment-722511 Share on other sites More sharing options...
Maq Posted December 23, 2008 Share Posted December 23, 2008 It's because you need to put $id = $_GET['grp']; in a hidden field. When you get to the page you have the value from the URL. But when you post again you lose it. So, you need to have a hidden value with $id in the form and post it. Link to comment https://forums.phpfreaks.com/topic/138195-solved-moving-a-variable-from-one-table-to-another/#findComment-722515 Share on other sites More sharing options...
premiso Posted December 23, 2008 Share Posted December 23, 2008 Where is the $_GET being set when you post the data? I do not see that anywhere... If you put it in a hidden field it would become $_POST instead of $_GET Link to comment https://forums.phpfreaks.com/topic/138195-solved-moving-a-variable-from-one-table-to-another/#findComment-722517 Share on other sites More sharing options...
contra10 Posted December 23, 2008 Author Share Posted December 23, 2008 k this is going to be alot...my get is coming from groups page which is <?php mysql_connect("localhost", "root", "") or die(mysql_error()); mysql_select_db("registration") or die(mysql_error()); $result = mysql_query("SELECT * FROM groups WHERE `group` = 'Alumni' ORDER BY RAND() LIMIT 1"); $result2 = mysql_query("SELECT * FROM groups WHERE `group` = 'Alumni' ORDER BY RAND() LIMIT 1"); $result3 = mysql_query("SELECT * FROM groups WHERE `group` = 'Alumni' ORDER BY RAND() LIMIT 1"); $row = mysql_fetch_array($result); $row2 = mysql_fetch_array($result2); $row3 = mysql_fetch_array($result3); $grp = "{$row['id']}"; $nameq = "{$row['name']}"; $name2 = "{$row2['name']}"; $name3 = "{$row3['name']}"; $city = "{$row['city']}"; echo "<table border='1' width='400'> <tr> <td WIDTH='33%' height='100'>IMAGE</td> <td WIDTH='33%' height='100'>IMAGE</td> <td WIDTH='33%' height='100'>IMAGE</td> </tr>"; { echo "<tr>"; echo "<td align='center'><a style='text-decoration:none' href='http://localhost/groupsio/index.php?grp=$grp'><FONT FACE='ariel' SIZE='2'>$nameq<p>"; echo "</a></FONT></td>"; echo "<td align='center'>" . $row2['name'] . "</td>"; echo "<td align='center'>" . $row3['name'] . "</td>"; echo "</tr>"; } echo "</table>"; ?> it then process to the next page with the id in the url are you guys saying that i should change the get variable to a post variable? Link to comment https://forums.phpfreaks.com/topic/138195-solved-moving-a-variable-from-one-table-to-another/#findComment-722527 Share on other sites More sharing options...
Maq Posted December 23, 2008 Share Posted December 23, 2008 Try this: mysql_connect("localhost", "root", "") or die(mysql_error()); mysql_select_db("registration") or die(mysql_error()); if (isset($_POST['submit'])){ if(is_numeric($_POST['grp'])){ $id = $_POST['grp']; $query= "SELECT * FROM groups WHERE id = $id"; $result = mysql_query($query) or die(mysql_error());; $group = mysql_fetch_assoc($result); $groupname = $group['name']; mysql_connect("localhost", "root", "") or die(mysql_error()); mysql_select_db("registration") or die(mysql_error()); $insert = "UPDATE users SET mygroups = '$groupname' WHERE username = '$username'"; $add_group = mysql_query($insert) or die(mysql_error()); } else { echo $_POST['grp'] . ": Is not numeric..."; } ?> Good Job Thank youMain } else { ?> </pre> <form action="<?php%20echo%20%24_SERVER%5B'PHP_SELF'%5D;%20?>" method="post"> "; '> </form> <br> <br> }<br>?&g Link to comment https://forums.phpfreaks.com/topic/138195-solved-moving-a-variable-from-one-table-to-another/#findComment-722531 Share on other sites More sharing options...
contra10 Posted December 23, 2008 Author Share Posted December 23, 2008 ok i see what u did, u took the value and input it into a hidden field in the form so that it would then change into a post value allowing it to go into the db...Thank you very much Link to comment https://forums.phpfreaks.com/topic/138195-solved-moving-a-variable-from-one-table-to-another/#findComment-722539 Share on other sites More sharing options...
Maq Posted December 23, 2008 Share Posted December 23, 2008 so that it would then change into a post value allowing it to go into the db... No, I did it because when you first get there the GET is alive, but when you hit submit, you refresh the page, killing the GET and essentially $id. p.s. - You can put GET values in a DB... Link to comment https://forums.phpfreaks.com/topic/138195-solved-moving-a-variable-from-one-table-to-another/#findComment-722543 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.