Jump to content

[SOLVED] moving a variable from one table to another


contra10

Recommended Posts

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
}
?>

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'";

 

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

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.

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
}
?>

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.

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?

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

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

 

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.