Page 1 of 1

Edit Site doesn't write data into the database

Posted: Wed Mar 31, 2010 2:47 am
by RHHC
Hey guys, I'm new here and hopefully you can help me a lil bit with my PHP Code. I got a script for editing mysql rows. The problem is that it doesn't write the new data into the database. Reading the data from the database is no problem - working very well. Does anybody see the error in the code? Thank you very much !

Code: Select all

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

//If cmd has not been initialized 
if(!isset($cmd))  
{ 
   //display all the news 
   $result = mysql_query("select * from users order by uid");  
    
   //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 
      $lname=$r["lname"];//take out the title 
      $fname=$r["fname"];//take out the title 
      $uid=$r["uid"];//take out the id 
      
     //make the title a link 
      echo "<a href='edit1.php?cmd=edit&uid=$uid'>$fname $lname - Edit</a>"; 
      echo "<br>"; 
    } 
} 
?> 
<? 
if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit") 
{ 
   if (!isset($_POST["submit"])) 
   { 
      $uid = $_GET["uid"]; 
      $sql = "SELECT * FROM users WHERE uid=$uid"; 
      $result = mysql_query($sql);         
      $myrow = mysql_fetch_array($result); 
      ?> 
       
      <form action="edit1.php" method="post"> 
      <input type=hidden name="uid" value="<?php echo $myrow["uid"] ?>"> 
    
      E-Mail-Adresse: <input type="text" name="email" VALUE="<?php echo $myrow["email"] ?>" SIZE=30/><br> 
       
       
      Vorname: 
      <input type="text" name="fname" VALUE="<? echo $myrow["fname"] ?>"/> 
      <br> 
       
       
      Name: 
      <input type="text" name="lname" Value="<? echo $myrow["lname"] ?>"/> 
     <br> 
       
         
       
       
           
      <input type="hidden" name="cmd" value="edit"> 
    
      <input type="submit" name="submit" value="submit"> 
    
      </form> 
    
<? } ?> 
<? 
   if ($_POST["$submit"]) 
   { 
      $email = $_POST["email"]; 
      $fname = $_POST["fname"]; 
      $lname = $_POST["lname"]; 
      $uid = $_POST["uid"]; 
       
      $sql = "UPDATE users SET email='$email',fname='$fname',lname='$lname' WHERE uid=$uid"; 
      //replace news with your table name above 
      $result = mysql_query($sql); 
      echo "Thank you! Information updated."; 
    } 
     
} 
?>



Re: Edit Site doesn't write data into the database

Posted: Wed Mar 31, 2010 2:53 am
by Benjamin
Here's a few things.
  • You are using short tags. (<?). Consider changing these to <?php as <? are not supported on all servers.
  • Turn on error reporting. There may be an error you don't know about because it may be turned off. The code below will turn this on for you.
  • Use mysql_real_escape_string() to prepare your data for queries. Without this they may fail and be vulnerable to MySQL injection.

Code: Select all

 error_reporting(E_ALL);
 ini_set("display_errors", 1);

Re: Edit Site doesn't write data into the database

Posted: Wed Mar 31, 2010 8:25 am
by mikosiko
first... Follow Benjamin advices....

second...

you wrote this line right:

Code: Select all

   if (!isset($_POST["submit"]))
and this wrong:

Code: Select all

   if ($_POST["$submit"])
and immediately before this line you have this:

Code: Select all

<? } ?> 
<? 
why so many short tags?.... you can improve that