help w/mysqli update

12Strings

Solid State Member
Messages
8
Location
at home
Hi, I am having trouble coding for mysqli update. Please, somebody
tell me the correct way. I'm trying to update the "lastused" (current date)
field in "emailtbl". Somebody please tell the best way to code this.
Below is the message and following, the current code:

Fatal error: Call to undefined function curdate() in C:\xampp\htdocs\home\lastused.php on line 14
$
PHP:
 db = new mysqli('localhost', 'root', 'pass', 'mydb');
if($db->connect_errno > 0)
{die('Unable to connect to database [' . $db->connect_error . ']');}
$sql = <<<SQL
    SELECT *
    FROM `emailtbl`
    WHERE `id` = '$id' 
SQL;
 if(!$result = $db->query($sql))
{die('There was an error running the query [' . $db->error . ']');}
$lastused = $_POST['lastused'];
$lastused = curdate();
echo "last date accessed is ".$data['lastused'];
$result->free();
$db->escape_string('This is an unescape "string"');
$db->close();
?>
$update = mysqli_query($dbconnect, "UPDATE emailtbl SET 
   lastused = curdate() WHERE id ='$id'");
    if($update == false)
    { die("UPDATE FAILED: ".mysqli_error($dbconnect)); }
    echo "$lastused is the last date this account was accessed";
 
Curdate() is a MySQL function. You're doing the operation on the PHP side of things, so you'll need to use PHP's date() function

Either that or in your MySQL query variable $sql you can use the curdate() function.

Sent from my Nexus 7
 
thanks for the input. I know to you this is horrible code; I'd appreciate if you would
advise how best to code this.
 
Last edited:
Hi and glad you asked. I'm trying to update the "lastused" field with the current date.
following is current code:
$con=mysqli_connect("localhost","root","cookie","homedb");
if(mysqli_errno($con))
{echo "Can't Connect to mySQL:".mysqli_connect_error();}
if(isset($_POST['lastused']))
{
$name = $_POST['lastused'];
$update = mysqli_query($dbconnect, "UPDATE emailtbl SET
lastused = curdate() WHERE id ='$id'");
if($update == false)
{ die("UPDATE FAILED: ".mysqli_error($dbconnect)); }
echo "$lastused is the last date this account was accessed";
}
 
Hi all, after way too much time, research, criticism and even some suggestions this is my code to create a dropdown
from a table, select row, display the row and update one field "lastused" using NOW().

error is"No rows returned matching id"
PHP:
 <?php
$db = new mysqli('localhost', 'root', 'cookie', 'homedb');
if ($db->connect_error) die ('Database connection failed: ' . $db->connect_error );
 $emailStmt = $db->prepare('SELECT * FROM emailtbl WHERE id = ? ');
$emailStmt->bind_Param('s', $id); // *****************
$emailStmt->execute();
                                  
    if ($email = $emailStmt->fetch())
    { echo 'Last date accessed was ', $email['lastused'];
    $lastStmt = $db->prepare('UPDATE emailtbl SET lastused = NOW() WHERE id = ? ');
    $lastStmt->bind_Param('s', $id); // **********************
    $lastStmt->execute(); }      
  
    else die( 'No rows returned matching id ' . $id ); // *******************
 ?>
Code:
<!DOCTYPE html><html><head><title>email menu</title>
<LINK REL=StyleSheet HREF="lastused.css" TYPE="text/css">
</head><body>
<!-- ------------------------------------------------------------------------ -->
PHP:
 <?php 
    $db = new mysqli('localhost', 'root', 'cookie', 'homedb');
    if ($db->connect_error) die ('Database connection failed: ' . $db->connect_error);
    if (isset($_POST['target']))
 { 
    $emailStmt = $db->prepare('SELECT target, username, password, emailused, lastused, purpose, saved
        FROM emailtbl WHERE target = ? ');
    $emailStmt->bind_Param('s', $_POST['target']); // *******************
    $emailStmt->execute();
                          
    if ($email = $emailStmt->fetch())
 {
        echo '
            <table class="emailMenu">
               <caption>Email menu</caption>
                <thead>
                    <tr>
                        <th scope="col">username</th>
                        <th scope="col">password</th>
                        <th scope="col">emailused</th>
                        <th scope="col">lastused</th>
                        <th scope="col">purpose</th>
                        <th scope="col">saved</th>
                    </tr>
                </thead><tbody>';
                 do
// ------------------------------------------------------
                 { echo '
                    <tr>
                        <td>', $email['target'], '</td>
                       <td>', $email['username'], '</td>
                        <td>', $email['password'], '</td>
                        <td>', $email['emailused'], '</td>
                        <td>', $email['lastused'], '</td>
                        <td>', $email['purpose'], '</td>
                        <td>', $email['saved'], '</td>
                    </tr>'; } 
// --------------------------------------------------------
        while ($email = $emailStmt->fetch());
        echo '
                </tbody>
            </table>';
   } 
           else echo 'No Matches found!<br />';
 
  } 
          else echo 'You failed to fill out a required field<br />'; 
  ?>
Code:
</body></html>
 
Just an FYI, you should look into using stored procedures instead of inline queries for MySQL. Stored procedures are less inclined to SQL injections.
 
Back
Top Bottom