2 Replies - 987 Views - Last Post: 19 July 2010 - 11:22 PM Rate Topic: -----

#1 Kheyen   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 19-July 10

Updating records in database

Posted 19 July 2010 - 10:49 PM

guys, i have these codes for updating the records in the database.

please.. check if these is correct?

<?php
session_start(); 
include "db.php"; 

$username = $_SESSION['username'];


$query="SELECT * FROM members where username='".mysql_real_escape_string($username)."'";

$result=mysql_query($query);
$num=mysql_numrows($result);

mysql_close();

	
$userid				=mysql_result($result,$i, 'userid');
$firstname			=mysql_result($result,$i,'firstname');
$lastname			=mysql_result($result,$i,'lastname');
$username			=mysql_result($result,$i,'username');
$email				=mysql_result($result,$i,'email');
$age				=mysql_result($result,$i,'age');
?>

<form action="update.php" method="post">
<input type="hidden" name="u_userid" value="<? echo "$userid" ?>">
<table>
<tr><td>ID:</td> <td><? echo "$userid"?></td></tr>
<tr><td>First Name:</td> <td> <input type="text" name="u_firstname" value="<? echo "$firstname"?>"></td></tr>
<tr><td>Last Name: </td> <td><input type="text" name="u_lastname" value="<? echo "$lastname"?>"></td></tr>
<tr><td>Username:</td> <td> <input type="text" name="u_username" value="<? echo "$username"?>"></td></tr>
<tr><td>Email:</td> <td> <input type="text" name="u_email" value="<? echo "$email"?>"></td></tr>
<tr><td>Age: </td> <td><input type="text" name="u_age" value="<? echo "$age"?>"></td></tr>
<tr><td></td><td><input type="Submit" value="Update Info">
</td></tr>
</form>
<tr><td></td><td><form action="form2.html" method="post">
<input type="submit" value="Cancel"></form></td></tr>
</table> 






and in update.php

<?php
session_start(); 
include "db.php"; 

$userid				=mysql_result($result, 'userid');
$firstname			=mysql_result($result, 'firstname');
$lastname			=mysql_result($result, 'lastname');
$username			=mysql_result($result, 'username');
$email				=mysql_result($result, 'email');
$age				=mysql_result($result, 'age');

include "db.php"; 

$query="UPDATE members SET firstname='$u_firstname' , lastname='$u_lastname', username='$u_username' , email='$u_email', age='$u_age' ";

$result=mysql_query($query);


mysql_close();

echo "Record Updated
 <br><br>

<form action=\"form2.html\" >
<input type=\"submit\" value=\"ok!\" />
</form>
";

?>




actually it shows a lot of errors/warning like this



Warning: mysql_result(): supplied argument is not a valid MySQL result resource in C:\web\htdocs\salome\this\update.php on line 7

Warning: mysql_result(): supplied argument is not a valid MySQL result resource in C:\web\htdocs\salome\this\update.php on line 8

Warning: mysql_result(): supplied argument is not a valid MySQL result resource in C:\web\htdocs\salome\this\update.php on line 9

Warning: mysql_result(): supplied argument is not a valid MySQL result resource in C:\web\htdocs\salome\this\update.php on line 10

Warning: mysql_result(): supplied argument is not a valid MySQL result resource in C:\web\htdocs\salome\this\update.php on line 11

Warning: mysql_result(): supplied argument is not a valid MySQL result resource in C:\web\htdocs\salome\this\update.php on line 12


WHAT WILL I DO..

please help me... :-(

Is This A Good Question/Topic? 0
  • +

Replies To: Updating records in database

#2 Valek   User is offline

  • The Real Skynet
  • member icon

Reputation: 544
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: Updating records in database

Posted 19 July 2010 - 10:54 PM

$result is null when you're calling mysql_result(), because you didn't just run a SELECT query and put a result resource into that variable.

Also, setting all those fields via mysql_result() is extremely slow. You should use mysql_fetch_assoc() instead. It's a lot faster, and reduces the cluttered look your code will take otherwise.
Was This Post Helpful? 0
  • +
  • -

#3 Atli   User is offline

  • Enhance Your Calm
  • member icon

Reputation: 4241
  • View blog
  • Posts: 7,216
  • Joined: 08-June 10

Re: Updating records in database

Posted 19 July 2010 - 11:22 PM

There are a few things there that should be pointed out...

  • You shouldn't be using mysql_result to get the form data in your update script. You should be using the $_POST array.
  • You should check that the values exist before you try to use them. Same with the MySQL result objects, you should make sure they are valid before using them.
  • Don't use mysql_result to get the data from your queries. Use mysql_fetch_array.
  • Try to avoid the short tags (<? echo $stuff; ?>). Use the full tags instead (<?php echo $stuff; ?>). The short version is not enabled by default, so you are likely to face problems if your code is ever used on another server. If you are intent on using them, at least do it right: <?= $stuff ?>


For example, this is a proper way of executing a mysql query based on input data:
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <input type="text" name="firstname"><br>
    <input type="text" name="lastname"><br>
    <input type="submit">
</form>
<hr>
<?php
if(isset($_POST['firstname'], $_POST['lastname'])) {
    include "db.php";

    $firstname = mysql_real_escape_string($_POST['firstname']);
    $lastname = mysql_real_escape_string($_POST['lastname']);

    $sql = "SELECT stuff FROM tbl 
            WHERE Name='{$firstname}'
            AND   pass='{$lastname}'";
    $result = mysql_query($sql);
    
    if($result) {
        if(mysql_num_rows($result) > 0) {
            echo "Your data:<br>";
            while($row = mysql_fetch_array($result)) {
                echo $row['name'] . "<br>";
            }
        }
        else {
            echo "No data found";
        }
    }
    else {
        echo "Query failed: " . mysql_error();
    }
}
else {
    echo "Please enter input data.";
}
?>


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1