Jump to content


Photo

problem inserting data to a mysql database


  • Please log in to reply
2 replies to this topic

#1 fogofogo

fogofogo
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 05 July 2006 - 09:04 AM

Hello All,

I have a script that displays and allows users to edit records in a mysql database. It displays the information correctly, but when I try to edit the information through forms I have created,  it gives me the "successful" message, but makes no changes to the database. I have printed out the sql, which looks like this...

UPDATE division_1 SET Team='Dundee', Played='2', Won='3', Lost='4', Drawn='3', [For]='12', Against='5', Points='12' WHERE ID=2

and heres what the sql in the code looks like...

UPDATE division_1 SET Team='".$Team."', Played='$Played', Won='$Won', Lost='$Lost', Drawn='$Drawn', [For]='$For', Against='$Against', Points='$Points' WHERE ID=".$id;


theres probably blinding errors - I'm fairly new to this.

Heres my code that inserts it into the database (sorry its a mess) -

<?php
$host = "localhost";
$user = "root";
$passw="password";
$db = "millhill";
$connection = @mysql_connect($host, $user) or die (mysql_error());
mysql_select_db($db, $connection) or die (mysql_error());

$id = $_GET['id'];
$Team = $_POST['Team'];
$Played = $_POST['Played'];
$Won = $_POST['Won'];
$Lost = $_POST['Lost'];
$Drawn = $_POST['Drawn'];
$For = $_POST['For'];
$Against = $_POST['Against'];
$Points = $_POST['Points'];
$submit = $_POST['submit']; 

if ($id) {
  if ($submit) {
    $sql = "UPDATE division_1 SET Team='".$Team."', Played='$Played', Won='$Won', Lost='$Lost', Drawn='$Drawn', [For]='$For', Against='$Against', Points='$Points' WHERE ID=".$id;
	echo $sql;
    $result = mysql_query($sql);
	echo $result;
    echo "Thank you! Information updated.\n";

  } else {

    // query the DB
    $sql = "SELECT * FROM division_1 WHERE id=$id";
    $result = mysql_query($sql);	
    $myrow = mysql_fetch_array($result);

    ?>

    <form method="post" action="<?php echo $PHP_SELF?>">
    <input type=hidden name="id" value="<?php echo $myrow["ID"] ?>">
    Team:<input type="Text" name="Team" value="<?php echo $myrow["Team"] ?>"><br>
    Played<input type="Text" name="Played" value="<?php echo $myrow["Played"] ?>"><br>
    Lost<input type="Text" name="Lost" value="<?php echo $myrow["Lost"] ?>"><br>
	Won:<input type="Text" name="Won" value="<?php echo $myrow["Won"] ?>"><br>
    Drawn:<input type="Text" name="Drawn" value="<?php echo $myrow["Drawn"] ?>"><br>
	For:<input type="Text" name="For" value="<?php echo $myrow["For"] ?>"><br>
	Against:<input type="Text" name="Against" value="<?php echo $myrow["Against"] ?>"><br>
	Points:<input type="Text" name="Points" value="<?php echo $myrow["Points"] ?>"><br>
    <input type="Submit" name="submit" value="Enter information">
    </form>

	<?php
	}
} else {

  // display list of employees
  $result = mysql_query("SELECT * FROM division_1",$connection );
  while ($myrow = mysql_fetch_array($result)) {
    printf("<a href=\"%s?id=%s\">%s %s</a><br>\n", $PHP_SELF, $myrow["ID"], $myrow["Team"], $myrow["Played"]);
	
  }
}


any ideas what I'm doing wrong?

Cheers



#2 mrwhale

mrwhale
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts

Posted 05 July 2006 - 09:13 AM

[For]='$For'

think that is the problem?

#3 fogofogo

fogofogo
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 05 July 2006 - 10:17 AM

not sure if it is - I thought FOR is a reserved name? I could be wrong. anyone know if this is the problem?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users