Jump to content

updating mysql table


rugzo

Recommended Posts

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

 

Link to comment
https://forums.phpfreaks.com/topic/139079-updating-mysql-table/
Share on other sites

 

$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());

Link to comment
https://forums.phpfreaks.com/topic/139079-updating-mysql-table/#findComment-727422
Share on other sites

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

}

}

?>

 

Link to comment
https://forums.phpfreaks.com/topic/139079-updating-mysql-table/#findComment-727438
Share on other sites

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.