www.webdeveloper.com
Results 1 to 3 of 3

Thread: mysqli dropdown

  1. #1
    Join Date
    Apr 2013
    Posts
    73

    mysqli dropdown

    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 Code:
    <?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>

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,252
    Let's start with the last error first: you can't mix mysqli_*() functions with mysql_*() functions. 'Nuff said?

    As far as the undefined indexes go, there are a few possibilities:
    • The form that accessed that page used GET instead of POST.
    • The form fields have different (case-sensitive) names than what the PHP code is expecting.
    • You got to that script without any sort of form submission at all.
    • Whatever else I haven't thought of.

    In any case, it's always a good idea to verify such fields are set (see isset() and empty()) before trying to do anything with them.
    Last edited by NogDog; 05-27-2014 at 08:55 PM.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Apr 2013
    Posts
    73
    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>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles