mysqli dropdown – PHP

  php

Q(Question):

I’m attempting a mysqli dropdown to select and print one record from table and update one
field, ("lastused" ($currdate))in that record. I get the following errors: Any help?

Notice: Undefined index: lastused in C:\xampp\htdocs\home\emaildrop.php on line 109

Notice: Undefined index: target in C:\xampp\htdocs\home\emaildrop.php on line 110
target Total results: 0

Warning: mysql_fetch_array() expects parameter 1 to be resource, object given in C:\xampp\htdocs\home\emaildrop.php on line 141
target username password emailused lastused purpose saved
could not retrieve data from database

<?php
$db = new mysqli('localhost', 'root', 'cookie', 'homedb');
if($db->connect_errno > 0)
{die('Unable to connect to database [' . $db->connect_error . ']');}
$lastused = $_POST['lastused']; // 108 ****************************
$target = $_POST['target']; // 109 *********************************
$currdate = date('Y-m-d');
$lastused = $currdate;
if($target=="email"){$target=$username;}
//echo "lastused $lastused";
echo "target $target";
$sql = <<<SQL
SELECT * FROM `oocust` WHERE 'target' = '$target'
SQL;
if(!$result = $db->query($sql))
{die('There was an error running the query [' . $db->error . ']');}
while($row = $result->fetch_assoc())
{echo $row['username'] . '<br />';}
echo 'Total results: ' . $result->num_rows;
$result->free();
$db->escape_string('This is an unescape "string"');
$db->close();
?>
<center>
<table cellspacing=2 cellpadding=0 border=1>
<tr>
<th bgcolor="#ccffff">target</TH>
<th bgcolor="#ccffff">username</TH>
<th bgcolor="#ccffff">password</TH>
<th bgcolor="#ccffff">emailused</TH>
<th bgcolor="#ccffff">lastused</TH>
<th bgcolor="#ccffff">purpose</TH>
<th bgcolor="#ccffff">saved</TH>
<?php
while($data = mysql_fetch_array($result)) // 139 *******************************
{
echo'<tr>';
// printing table row
echo '
<td>'.$data['target'].'</td>
<td>'.$data['username'].'</td>
<td>'.$data['password'].'</td>
<td>'.$data['emailused'].'</td>
<td>'.$data['lastused'].'</td>
<td>'.$data['purpose'].'</td>
<td>'.$data['saved'].'</td>';
// looping all data to be printed till last row in the table
echo'</tr>';
// closing table row
}
echo '</table>'; //closing table tag
mysql_query("UPDATE emailtbl SET lastused=$lastused");
$result=mysql_query("select lastused from emailtbl") or die ("could not retrieve data from database");
$data=mysql_fetch_assoc($result);
echo 'Total rows updated: ' . $db->affected_rows;
echo "lastused ".$data['lastused'];
mysql_close();
?>

</table></center></body></html>

A(Answer):

error #1: if you request the script using GET, you don’t have POST data. hence never assume that the data are always there.

tip: use filter_input()

error #2: cf. error #1

error #3: you cannot mix mysql_* functions and MySQLi.

A(Answer):

this is my current code. clicking "submit" gets no result?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<html><body>
<form name="form" action="" method="post">
Select email to view<SELECT name=target>
<OPTION class=highlight value=email selected>owenspaulette59@yahoo
<OPTION class=highlight value=paypal>paypal
<OPTION class=highlight value=ebay>ebay
</SELECT><p>
<CENTER><input type="submit" name="submit" value="THEN submit choice"><p>
<?php
$target = (isset($_POST['target']))?1:0; // this is my target
$payrec = (isset($_POST['payrec']))?1:0;
$lastused = (isset($_POST['lastused']))?1:0;
$id = (isset($_POST['id'])) ? mysqli_real_escape_string($dbconnect, $_POST['id']) : '';
$dbconnect = mysqli_connect('localhost','root','cookie')or die(mysqli_error($dbconnect));
$result = mysqli_query($dbconnect, "SELECT * FROM emailtbl where payrec='P'");
if($result === FALSE) { die(mysql_error()); }
echo date('m/d/y');
?>
<table cellspacing=0 cellpadding=0 border=1>
<tr>
<th bgcolor="#7FFF2A">target</th>
<th bgcolor="#7FFF2A">username</th>
<th bgcolor="#7FFF2A">password</th>
<th bgcolor="#7FFF2A">emailused</th>
<th bgcolor="#7FFF2A">lastused</th>
<th bgcolor="#7FFF2A">purpose</th>
<th bgcolor="#7FFF2A">saved</th>
</tr>
<?php
// $lastused = $row['CURDATE()'];
$lastused = date('Y-m-d');
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['target'] . "</td>";
echo "<td>" . $row['username'] . "</td>";
echo "<td>" . $row['password'] . "</td>";
echo "<td>" . $row['emailused'] . "</td>";
echo "<td>" . $row['lastused'] . "</td>";
echo "<td>" . $row['purpose'] . "</td>";
echo "<td>" . $row['saved'] . "</td>";
echo "</tr>";
echo "</table>
<input type='submit' name='update' value='submit' />
</form>";
if (!empty($_POST['update_lastused']))
{ $update = mysqli_query($dbconnect, "UPDATE emailtbl SET lastused = '$lastused' WHERE id ='$id");
if($update == false)
{ die("UPDATE FAILED: ".mysqli_error($dbconnect)); }
echo "Success!";
}
else { echo "oops!"; }
}
?>
</body></html>

A(Answer):

remember what I said about error #3?

A(Answer):

you mean the fetch? thanks

A(Answer):

among others. you‘re wildly mixing two incompatible systems.

A(Answer):

Guess what? 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","cookie","homedb");
//============== 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>

LEAVE A COMMENT