I got one of those AHA moments. The following code is what I want in that it creates a menu and I can select and display a table row. I still need to use that selection to update the "lastused". I really appreciate your help. <!DOCTYPE><html><head><title>email menu</title></head> <body><center> <form name="form" method="post" action=""> <?php $con=mysqli_connect("localhost","root","secret","mydb"); //============== check connection if(mysqli_errno($con)) {echo "Can't Connect to mySQL:".mysqli_connect_error();} else {echo "Connected to mySQL</br>";} //This creates the drop down box 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 value='". $row['target']."'>".$row['target'] .'</option>';} echo '</select>'; ?> <input type="submit" name="submit" value="Submit"/><!-- update "lastused" using selected "target"--> </form></body></html> <!DOCTYPE><html><head><title>email menu</title></head> <body><center> <?php $con=mysqli_connect("localhost","root","cookie","homedb"); if(mysqli_errno($con)) {echo "Can't Connect to mySQL:".mysqli_connect_error();} if(isset($_POST['target'])) { $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));} $lastused = "CURDATE()"; // update "lastused" using selected "target" //display the table echo '<table border="1">'.'<tr>'.'<td bgcolor="#ccffff align="center">'. 'Email menu'. '</td>'.'</tr>'; echo '<tr>'.'<td>'.'<table border="1">'.'<tr>'.'<td bgcolor="#ccffff align="center">'.'target'.'</td>'.'<td bgcolor="#ccffff align="center">'.'username'.'</td>'.'<td bgcolor="#ccffff align="center">'.'password'.'</td>'.'<td bgcolor="#ccffff align="center">'.'emailused'.'</td>'.'<td bgcolor="#ccffff align="center">'.'lastused'.'</td>'.'<td bgcolor="#ccffff align="center">'.'purpose'. '</td>'.'<td bgcolor="#ccffff align="center">'. 'saved' .'</td>'.'</tr>'; 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>'; } ?> </body></html>
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 $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";
1) STOP using heredoc, nowdoc or whatever the hell dumbass string method you have in that first query. I don't know who thought that PHP needed such garbage, but they need a good swift kick in the junk. 2) You're using mysqli, STOP blindly adding your values to your queries. 3) where is $id even defined? 4) REALLY might help if you used the tab key a wee bit more, and the CODE bbCode tag when posting on the forums. 5) STOP making 'variables for nothing' like $sql, even if the examples on PHP.NET seem bound and determined to waste memory for the same reason. 6) CURDATE is a SQL function, NOT a PHP one... though really I'd probably use NOW() instead if that's a "dateTime" format field. 7) You pull it from post, then on the next line override it with the function -- to what end? Which is it? I would probably gut the code in your most recent post down to: $db = new mysqli('localhost', 'user', 'pass', 'mydb'); if ($db->connect_error) die ( 'Database connection failed: ' . $db->connect_error ); $emailStmt = $db->prepare(' SELECT * FROM emailtbl WHERE id = ? '); $emailStmt->bindParam('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->bindParam('s', $id); $lastStmt->execute(); } else die( 'No rows returned matching id ' . $id ); Code (markup): Though I'm guessing a bit on that. Let's also do a quick rewrite of the second file from your first post, where you have tags and attributes like CENTER, BORDER, BGCOLOR and ALIGN that have no business in any HTML written after 1997, static style inlined in the markup, an outer table for nothing, TD doing TH's job, no THEAD, no TBODY, etc, etc, etc... which of course goes so well with the incomplete doctype :/ <!DOCTYPE html> <html><head> <title> email menu </title> <!-- in production code this style belongs in an external stylesheet --> <style type="text/css"> body { text-align:center; } .emailMenu, .emailMenu td, .emailMenu th { border:1px solid #000; } .emailMenu caption, .emailMenu thead th { text-align:center; font-weight:normal; background:#CFF; } .emailMenu caption { border:solid #000; border-width:1px 1px 0; } </style> </head><body> <?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->bindParam('s', $_POST['target']); $emailStmt->execute(); if ($email = $emailStmt->fetch()) { echo ' <table class="emailMenu"> <caption>Email menu</caption> <thead> <tr> <th scope="col">target</th> <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 />'; ?> </body></html> Code (markup): Also REALLY helps to get all the style crap the **** out of the markup from the point of view of making PHP not have to work as hard, and making the code easier to read. Pay particular attention to the SEMANTIC and properly built table, using all the proper tags you are SUPPOSED to have in there!
This is the error I get running the following code. "Fatal error: Call to undefined method mysqli_stmt::bindParam() in C:\xampp\htdocs\home\emailoptsy.php on line 11" Does this have to do w/ oo/procedural? <!DOCTYPE html><html><head><title>email menu</title> <LINK REL=StyleSheet HREF="lastused.css" TYPE="text/css"> </head><body> <!-- ------------------------------------------------------------------------ --> <?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 />'; ?> </body></html>
Yeah, my bad. I don't use mysqli often enough to keep it straight, I generally find it inferior to PDO. Also what I get for posting code directly into the edit box instead of working on it in a real editor and *shock* bothering to test it.