Jump to content


Photo

Editing a row in mysql using php


  • Please log in to reply
16 replies to this topic

#1 mikew2

mikew2
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 01 August 2006 - 09:17 AM

Hi guys,

Im trying to edit a row in mysql using php. This is my code:

<?
//connect to mysql
//change user and password to your mySQL name and password
mysql_connect("localhost","root","admin");

//select which database you want to edit
mysql_select_db("ict");

//If cmd has not been initialized
if(!isset($cmd))
{
  //display all the news
  $result = mysql_query("select * from contact order by id");
 
  //run the while loop that grabs all the news scripts
  while($r=mysql_fetch_array($result))
  {
      //grab the title and the ID of the news
      $title=$r["title"];//take out the title
      $id=$r["id"];//take out the id
   
//make the title a link
      echo "<a href='edit.php?cmd=edit&id=$id'>$title - Edit</a>";
      echo "<br>";
    }
}
?>

<?
if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit")
{
  if (!isset($_POST["submit"]))
  {
      $id = $_GET["id"];
      $sql = "SELECT * FROM contact 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="title" VALUE="<?php echo $myrow["title"] ?>" SIZE=30><br>
      Content:<TEXTAREA NAME="message" ROWS=10 COLS=30><? echo $myrow["content"] ?></TEXTAREA><br>
      Date:<INPUT TYPE="TEXT" NAME="who" VALUE="<?php echo $myrow["date"] ?>" SIZE=30><br>
 
      <input type="hidden" name="cmd" value="edit">
 
      <input type="submit" name="submit" value="submit">
 
      </form>
 
  <? } ?>

<?
  if ($_POST["$submit"])
  {
      $title = $_POST["title"];
      $content = $_POST["content"];
      $date = $_POST["date"];

      $sql = "UPDATE contact SET title='$title',content='$content',date='$date' WHERE id=$id";

      $result = mysql_query($sql);
      echo "Thank you! Information updated.";
  }
}
?>


It works fine until i hit submit and then it just goes back to the edit.php page. And doesnt update the row. It also doesnt display the "Thank you! Information updated." However there are no mysql errors.

Any help would be much appreciated
Thanks
Mike

#2 hackerkts

hackerkts
  • Members
  • PipPipPip
  • Advanced Member
  • 593 posts
  • LocationSingapore
  • Age:18

Posted 01 August 2006 - 09:39 AM

Try changing
   if ($_POST["$submit"])
to
   if ($_POST["submit"])


Regards,
hackerkts

To be a coder, you must learn how to think and not to give up so easily.


#3 mikew2

mikew2
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 01 August 2006 - 09:45 AM

ah thanks. But ive still got a problem. Now it says "Information has been updated" however it hasnt actually updated the row.

any clues?

#4 ronverdonk

ronverdonk
  • Members
  • PipPipPip
  • Advanced Member
  • 277 posts
  • LocationNetherlands

Posted 01 August 2006 - 09:51 AM

No clue yet, but you'd better inspect the $result of the mysql_query and display the mysql error messge if not correct.

Ronald  ;D
RTFM is an almost extinct art form, it should be subsidized.

#5 hackerkts

hackerkts
  • Members
  • PipPipPip
  • Advanced Member
  • 593 posts
  • LocationSingapore
  • Age:18

Posted 01 August 2006 - 09:51 AM

Hmm I'm unsure about this
if(!isset($cmd))

What is your $cmd ?

Regards,
hackerkts

To be a coder, you must learn how to think and not to give up so easily.


#6 mikew2

mikew2
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 01 August 2006 - 09:54 AM

hmm im not quite sure what you mean. sorry im quite new at this.

and ronald there isnt any mysql errors. Which is odd.

#7 hackerkts

hackerkts
  • Members
  • PipPipPip
  • Advanced Member
  • 593 posts
  • LocationSingapore
  • Age:18

Posted 01 August 2006 - 09:57 AM

You have wrote this "//If cmd has not been initialized"
But does it actually means ? What does that cmd do ?

Regards,
hackerkts

To be a coder, you must learn how to think and not to give up so easily.


#8 mikew2

mikew2
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 01 August 2006 - 10:06 AM

it means if the edit query has been started. Then display the titles.

#9 hostfreak

hostfreak
  • Members
  • PipPipPip
  • Advanced Member
  • 581 posts

Posted 01 August 2006 - 10:07 AM

What is your primary key? You need to update it as well. I am assuming it is "id", if it is, this should work:

<?
//connect to mysql
//change user and password to your mySQL name and password
mysql_connect("localhost","root","admin");
   
//select which database you want to edit
mysql_select_db("ict");

//If cmd has not been initialized
if(!isset($cmd))
{
   //display all the news
   $result = mysql_query("select * from contact order by id");
   
   //run the while loop that grabs all the news scripts
   while($r=mysql_fetch_array($result))
   {
      //grab the title and the ID of the news
      $title=$r["title"];//take out the title
      $id=$r["id"];//take out the id
     
    //make the title a link
      echo "<a href='edit.php?cmd=edit&id=$id'>$title - Edit[/url]";
      echo "
";
    }
}
?>

<?
if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit")
{
   if (!isset($_POST["submit"]))
   {
      $id = $_GET["id"];
      $sql = "SELECT * FROM contact 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="title" VALUE="<?php echo $myrow["title"] ?>" SIZE=30>

      Content:<TEXTAREA NAME="message" ROWS=10 COLS=30><? echo $myrow["content"] ?></TEXTAREA>

      Date:<INPUT TYPE="TEXT" NAME="who" VALUE="<?php echo $myrow["date"] ?>" SIZE=30>

   
      <input type="hidden" name="cmd" value="edit">
      <input type="hidden" name="id" value="<?php echo $id; ?>">
   
      <input type="submit" name="submit" value="submit">
   
      </form>
   
   <? } ?>

<?
   if ($_POST["$submit"])
   {
      $title = $_POST["title"];
      $content = $_POST["content"];
      $date = $_POST["date"];
      $id = $_POST["id"];

      $sql = "UPDATE contact SET title='$title',content='$content',date='$date',id='$id' WHERE id=$id";

      $result = mysql_query($sql);
      echo "Thank you! Information updated.";
   }
}
?>


#10 hackerkts

hackerkts
  • Members
  • PipPipPip
  • Advanced Member
  • 593 posts
  • LocationSingapore
  • Age:18

Posted 01 August 2006 - 10:11 AM

Try change
<form action="edit.php" method="post">
to
<form action="<?php echo $_SERVER['PHP_SELF']; ?>?cmd=edit&id=$id" method="post">


Regards,
hackerkts

To be a coder, you must learn how to think and not to give up so easily.


#11 techiefreak05

techiefreak05
  • Members
  • PipPipPip
  • Advanced Member
  • 494 posts
  • LocationER, MN

Posted 01 August 2006 - 10:15 AM

Uh, How could I change this for my use??
Link shortener with advanced, detailed statistics:

http://tyny.us/

#12 hackerkts

hackerkts
  • Members
  • PipPipPip
  • Advanced Member
  • 593 posts
  • LocationSingapore
  • Age:18

Posted 01 August 2006 - 10:18 AM

Uh, How could I change this for my use??


Create a new thread and ask for help :P

Regards,
hackerkts

To be a coder, you must learn how to think and not to give up so easily.


#13 techiefreak05

techiefreak05
  • Members
  • PipPipPip
  • Advanced Member
  • 494 posts
  • LocationER, MN

Posted 01 August 2006 - 10:20 AM

I was about to, but then I saw this and thought it was easier just to post in here.. but i didnt know if I could start a new one, but now I know, thanks.
Link shortener with advanced, detailed statistics:

http://tyny.us/

#14 mikew2

mikew2
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 01 August 2006 - 10:23 AM

hmm none of that changed anything. however when i added mysql_error to the query. i got this:

Line 66:
MySQL Error:
Incorrect date value: '' for column 'date' at row 1


I dont understand tho because the date is in the right format

#15 hackerkts

hackerkts
  • Members
  • PipPipPip
  • Advanced Member
  • 593 posts
  • LocationSingapore
  • Age:18

Posted 01 August 2006 - 10:28 AM

Hmm.. Try chaning the date table to "text".

Regards,
hackerkts

To be a coder, you must learn how to think and not to give up so easily.


#16 mikew2

mikew2
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 01 August 2006 - 10:36 AM

that works hacker, but its not really a solution.

#17 mikew2

mikew2
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 01 August 2006 - 10:39 AM

lol i just realised the answer rofl. Im kicking myself. The name of my date input element was called "who" and not "date" OMFOGMGOMFOI MFOM OM

ROFL OROFL LOL :P

thanks for the help guys
mike

EDITED BY WILDTEEN88: REMOVED LANGUAGE. PLEASE DO NOT SWEAR IN POSTS. THERE ARE MINORS THAT BROWSE AROUND THIS FORUM




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users