mysqli update

limited

New Member
Hi, the code below creates the result I want (don't know how to post a localhost ping). My issue is that I don't know how to link that code, (code 1) to (code 2) which updates the "lastused" file, the current date.
-------------------------------------------------------
(code 1)
Code:
<!DOCTYPE html><html>
<title>email menu</title>
<head></head>
<BODY><center>
<FORM name=lastused method="post" action="">
PHP:
    <?php
error_reporting(E_ALL ^ E_NOTICE);
// error_reporting(0);
echo "<center>";echo date('m/d/y');echo "</center>";
$id="''";
$con=mysqli_connect("localhost","root","cookie","homedb");

// ============== check connection

    if(mysqli_errno($con))
    {echo "Can't Connect to mySQL:".mysqli_connect_error();}
    else
    {echo "</br>";}

// ==========This creates the drop down box using records in the table

       echo "<select name= 'target'>";
    echo '<option value="">'.'---select email account ---'.'</option>';
    $query = mysqli_query($con,"SELECT target FROM emailtbl");
    $query_display = mysqli_query($con,"SELECT * FROM emailtbl");
    while($row=mysqli_fetch_array($query))
    
{ echo "<option class=highlight value='". $row['target']."'>".$row target']
    .'</option>';}

    echo '</select>';
    ?>
Code:
<input type="submit" name="submit" value="Submit"/>
    </form></body></html
>
PHP:
           <?php
error_reporting(E_ALL ^ E_NOTICE);
// error_reporting(0);
    $con=mysqli_connect("localhost","root","cookie","homedb");
    if(mysqli_errno($con))
    {echo "Can't Connect to mySQL:".mysqli_connect_error();}
        if(isset($_POST['target']))
 {
    $id = $_POST['id'];
    $lastused = $_POST['lastused']; 
    $name = $_POST['target'];
    $fetch="SELECT target, username, password, emailused, lastused, purpose, saved FROM emailtbl WHERE target = '".$name."'";
    $result = mysqli_query($con,$fetch);
    if(!$result)
    {echo "Error:".(mysqli_error($con));}

// =============================== this displays the table

    echo '<table border="1">'.'<tr>'.'<td bgcolor="#FFD47F" align="center">'. 'email menu'. '</td>'.'</tr>';
    echo '<tr>'.'<td>'.'<table border="1">'.'<tr>'.'<td bgcolor="#ccffff">'.'target'.'</td>'.'<td bgcolor="#ccffff">'.'username'.'</td>'.'<td bgcolor="#ccffff">'. 'password' .'</td>'.'<td bgcolor="#ccffff">'. 'emailused'. '</td>'.'<td bgcolor="#FFD47F">'. 'lastused' .'</td>'.'<td bgcolor="#ccffff">'. 'purpose'. '</td>'.'<td bgcolor="#ccffff">'. 'saved' .'</td>'.'</tr>';
    // while($data = mysqli_fetch_row($fetch))
    while($data=mysqli_fetch_row($result))
    {echo ("<tr><td>$data[0]</td><td>$data[1]</td><td>$data[2]</td><td>$data[3]</td><td>$data[4]</td><td>$data[5]</td><td>$data[6]</td></tr>");}
    echo '</table>'.'</td>'.'</tr>'.'</table>';
 }
    ?>
Code:
</body></html>
-----------------------------------------------------------
(code 2)
PHP:
<?php
error_reporting(E_ALL ^ E_NOTICE);
$servername = "localhost";$username = "root";$password = "cookie";
$dbname = "homedb";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn)
   { die("Connection failed: " . mysqli_connect_error()); }
        
      $name = $_POST['target'];
  $sql = "UPDATE emailtbl SET visits = visits + 1, lastused = NOW() WHERE 

target = '".$name."'";
if (mysqli_query($conn, $sql))
     {echo "Record updated successfully";}
else
     {echo "Error updating record: " . mysqli_error($conn);}
?>
-----------------------------------------------------------
 

Cromewell

Administrator
Staff member
There's a lot of bonus code here. I'm trying to understand what you want to do. Part 2 looks like a page visit counter, is that all you are trying to do?

If so you can just drop
Code:
      $name = $_POST['target']; 
  $sql = "UPDATE emailtbl SET visits = visits + 1, lastused = NOW() WHERE  

target = '".$name."'"; 
if (mysqli_query($conn, $sql)) 
     {echo "Record updated successfully";} 
else 
     {echo "Error updating record: " . mysqli_error($conn);}
in after you've confirmed you connected to the database. You probably don't want the echos either, unless you want some free text in your form.

Realistically, you'd probably want to separate out the different functionality but the code is a bit ugly to start with :p
 
Top