rugzo Posted January 1, 2009 Share Posted January 1, 2009 Hi all, i have a question about updating mysql tables via php. i have a test table like this --> CREATE TABLE IF NOT EXISTS `subjects` ( `Id` int(10) NOT NULL auto_increment, `Subject` text character set latin1 collate latin1_general_ci NOT NULL, `Troubleshoot` text NOT NULL, `Knox` text NOT NULL, `Summary` text NOT NULL, `CTI` text NOT NULL, `GR` text NOT NULL, `Info` text NOT NULL, `Description` text NOT NULL, `Script` text NOT NULL, `TrainingDocuments` text NOT NULL, `PA` text NOT NULL, PRIMARY KEY (`Id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ; ------------------ The code looks like this--> <? mysql_connect("localhost","root","123*qwe123"); mysql_select_db("kb"); if(!isset($cmd)) { $result = mysql_query("select * from subjects order by id"); while($r=mysql_fetch_array($result)) { $Subject=$r["Subject"]; $Id=$r["Id"]; echo "<a href='edit.php?cmd=edit&Id=$Id'>$Subject - Edit</a>"; echo "<br>"; } } ?> <? if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit") { if (!isset($_POST["submit"])) { $Id = $_GET["Id"]; $sql = "SELECT * FROM subjects WHERE Id=$Id"; $result = mysql_query($sql); $myrow = mysql_fetch_array($result); ?> <form action="edit.php" method="post"> <input type=hidden name="Id" value="<?php echo $myrow["Id"] ?>"> Title:<INPUT TYPE="TEXT" NAME="Subject" VALUE="<?php echo $myrow["Subject"] ?>" SIZE=30><br> <input type="hidden" name="cmd" value="edit"> <input type="submit" name="submit" value="submit"> </form> <? } ?> <? if ($_POST["$submit"]) { $Subject = $_POST["Subject"]; $sql = "UPDATE subjects SET Subject='$Subject' WHERE Id=$Id"; $result = mysql_query($sql); echo "Thank you! Information updated."; } } ?> Everything works fine. But when i click submit it doesn't changes anything in mysql. There is no error. Has anyone an idea what i am missing here. thanks in advance and happy new year Quote Link to comment https://forums.phpfreaks.com/topic/139079-updating-mysql-table/ Share on other sites More sharing options...
bluesoul Posted January 1, 2009 Share Posted January 1, 2009 $sql = "UPDATE subjects SET Subject='$Subject' WHERE Id=$Id"; Your table is called Subject but you're referring to it as subjects here. Also, always add or die(mysql_error()); to your query. $result = mysql_query("select * from subjects order by id") or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/139079-updating-mysql-table/#findComment-727422 Share on other sites More sharing options...
rugzo Posted January 1, 2009 Author Share Posted January 1, 2009 Thanks for the reply. The table is called subjects. One colum in the table is called Subject. Quote Link to comment https://forums.phpfreaks.com/topic/139079-updating-mysql-table/#findComment-727425 Share on other sites More sharing options...
bluesoul Posted January 1, 2009 Share Posted January 1, 2009 My apologies, I'm running on too little sleep. New Years and all that. In that case go ahead and append the mysql_error statement and see what it turns up. Quote Link to comment https://forums.phpfreaks.com/topic/139079-updating-mysql-table/#findComment-727429 Share on other sites More sharing options...
rugzo Posted January 1, 2009 Author Share Posted January 1, 2009 Ok i changed it to $result = mysql_query("select * from subjects order by id") or die (mysql_error()); but no errors. Nothing happened. Quote Link to comment https://forums.phpfreaks.com/topic/139079-updating-mysql-table/#findComment-727431 Share on other sites More sharing options...
bluesoul Posted January 1, 2009 Share Posted January 1, 2009 You added it to all your queries? Quote Link to comment https://forums.phpfreaks.com/topic/139079-updating-mysql-table/#findComment-727432 Share on other sites More sharing options...
rugzo Posted January 1, 2009 Author Share Posted January 1, 2009 Yes, i changed it overall ---> <? mysql_connect("localhost","root","123*qwe123") or die (mysql_error()); mysql_select_db("kb") or die (mysql_error()); if(!isset($cmd)) { $result = mysql_query("select * from subjects order by id") or die (mysql_error()); while($r=mysql_fetch_array($result)) { $Subject=$r["Subject"]; $Id=$r["Id"]; echo "<a href='edit.php?cmd=edit&Id=$Id'>$Subject - Edit</a>"; echo "<br>"; } } ?> <? if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit") { if (!isset($_POST["submit"])) { $Id = $_GET["Id"]; $sql = "SELECT * FROM subjects WHERE Id=$Id" or die (mysql_error()); $result = mysql_query($sql); $myrow = mysql_fetch_array($result); ?> <form action="edit.php" method="post"> <input type=hidden name="Id" value="<?php echo $myrow["Id"] ?>"> Title:<INPUT TYPE="TEXT" NAME="Subject" VALUE="<?php echo $myrow["Subject"] ?>" SIZE=30><br> <input type="hidden" name="cmd" value="edit"> <input type="submit" name="submit" value="submit"> </form> <? } ?> <? if ($_POST["$submit"]) { $Subject = $_POST["Subject"]; $sql = "UPDATE subjects SET Subject='$Subject' WHERE Id=$Id" or die (mysql_error()); $result = mysql_query($sql); echo "Thank you! Information updated."; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/139079-updating-mysql-table/#findComment-727438 Share on other sites More sharing options...
rugzo Posted January 2, 2009 Author Share Posted January 2, 2009 Does anyone has an idea what the problem might be? Quote Link to comment https://forums.phpfreaks.com/topic/139079-updating-mysql-table/#findComment-727937 Share on other sites More sharing options...
revraz Posted January 2, 2009 Share Posted January 2, 2009 Echo your variables (like $Id, $Subject) to make sure they are in fact set. Quote Link to comment https://forums.phpfreaks.com/topic/139079-updating-mysql-table/#findComment-728070 Share on other sites More sharing options...
rugzo Posted January 3, 2009 Author Share Posted January 3, 2009 Ok, i solved the problem after removing the "$" before submit if ($_POST["$submit"]) Quote Link to comment https://forums.phpfreaks.com/topic/139079-updating-mysql-table/#findComment-728806 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.