Jump to content

[SOLVED] PHP code to move between tables


chinclub

Recommended Posts

I am trying to write my very first PHP code and it is not going well at all!!  I am trying to code a way for someone to click a link to move an item (and all of its variables) from one table in the SQL database to another.

 

I thought I had it working but it didn't copy over everything so I editted it and now I get an error:

 

"Column count doesn't match value count at row 1"

 

Also, I don't know what code I need to include to remove the info from the first table.  Right now it is just copying it.

Here is how the database tables are layed out.One is called "pets" and the other is called "pound_pets"

 

  `user` varchar(255) NOT NULL default '',
  `petID` int(11) NOT NULL default '0',
  `id` int(11) NOT NULL auto_increment,
  `points` int(11) NOT NULL default '0',
  `status` varchar(255) NOT NULL default '',
  `petName` varchar(255) NOT NULL default '',
  `pet` varchar(255) NOT NULL default '',
  `species` varchar(255) NOT NULL default '',
  `date` varchar(255) NOT NULL default '',
  `health` int(11) NOT NULL default '100',
  `wins` int(11) NOT NULL default '0',
  `loses` int(11) NOT NULL default '0',
  `ties` int(11) NOT NULL default '0',
  `img` text NOT NULL,
  `level` int(11) NOT NULL default '1',
  `items` int(11) NOT NULL default '0',
  `maxhealth` int(11) NOT NULL default '0',
  `site` varchar(255) NOT NULL default '',
  PRIMARY KEY  (`id`)

 

 

Here is what I have so far (that is giving me the error):

 

if($a == 'giveaway') {
 	$id = $_GET['id'];
	$check = mysql_num_rows(mysql_query("SELECT * FROM `pets` WHERE `user`='{$_COOKIE['ipets']}' AND `petID`='$id' AND `site`=\"$Z\""));

		$date = date("m/d/y");
		$status = 3;
		$insert = mysql_query("INSERT INTO `pound_pets` (`site`, `img`, `status`, `user`, `petID`, `species`, `date`, `petName`, `items`, `wins`, `loses`, `ties`, `maxhealth` ) VALUES(\"$Z\", \"$img\", '$status', '{$_COOKIE['ipets']}', '$id', \"$petName\", '$date')");

		if($insert) {
			$sql = mysql_query("SELECT * FROM `pets` WHERE `user`='{$_COOKIE['ipets']}' AND `petID`='$id' AND `site`=\"$Z\"");
			while($row = mysql_fetch_array($sql)) {
			 	$idd = $row["id"];
			}

 

 

Can anyone help me with the code that will remove the data from pets after it copies it to pound_pets

and also tell me why it has the ""Column count doesn't match value count at row 1" error.

 

Thanks!!

 

 

Link to comment
https://forums.phpfreaks.com/topic/48194-solved-php-code-to-move-between-tables/
Share on other sites

Column count doesn't match value count means exactly that - the number of columns doesn't match the number of values.

 

But there's a much easier way to do what I think you want.  Use one table and add a column to identify whether the thing is a 'pet' or a 'pound pet'.  Then all you need do is a simple UPDATE query to change that column's value.  When looking for a particular type of pet, add a WHERE petplace = 'pound_pet' (for example) to identify only the pound pets in the table.

That is a wonderful idea!!!  If I set the user to pound then I can have it set the user to whoever adopts it.

 

But now I have a new problem.  I created my PHP code to do that and the code seems to run fine, but it isn't changing the database.  What am I missing??

 

 <?
include("header.php");

$a = $_GET['action'];
if($a == 'giveaway')  {

$sql = mysql_query("UPDATE `pets` SET `user`='pound' WHERE `id`='$id'");
		if($sql) {
		 	print "How sad!";
		}
		}
include("footer.php");
?>

Clicking a link or typing in the address bar are the same thing (which makes $_GET liable to abuse).

 

You should probably start reading up on how to 'sanitize' user input to protect your database. (worth searching the php help forum since it's a frequent question/problem).

 

As to why your code isn't performing, it's not obvious that a database connection and database selection ever occurs.  As a matter of good practice when testing, use error messages to tell you what's happening (or not) and echo actual queries.

 

$sql = mysql_query("UPDATE `pets` SET `user`='pound' WHERE `id`='$id'");
if($sql) {
print "How sad!";
}

 

Better:

$sql = "UPDATE pets SET user='pound' WHERE id='$id'";
echo "query: ". $sql. "<br/>"; // what's happening
$result = mysql_query($sql) or die("Error: ". mysql_error(). " with query ". $sql); // helpful
if($result) {
    print "How sad!";
}

 

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.