php - Display multiple values on one line in mysqli generated dropdown box -
using mysqli code, have been able create dropdown box using data mysql database. sql statement selects userid, f_name, , l_name database. list part of form , when user selects name, want pass userid via $_post method. how can done? again help.
<select name="names"> <option value = "">---select---</option> <?php $queryusers = 'select userid, f_name, l_name users'; $db = mysqli_query($mysqli, $queryusers); while ( $names=mysqli_fetch_assoc($db)) { echo "<option value='{".$names['f_name']."}' . {".$names['l_name']."}'>".$names['f_name']. " " . $names['l_name']."</option>"; } ?> </select>
just this:
echo '<option value="'.$names['userid'].'" >'.$names['f_name'].' '. $names['last_nm'] . '</option>';
that becomes value of option, gets submitted.
edit
this code using this
<select name="names"> <option value = "">---select---</option> <?php $queryusers = 'select userid, f_name, l_name users'; $db = mysqli_query($mysqli, $queryusers); while ( $names=mysqli_fetch_assoc($db)) { echo '<option value="'.$names['userid']. '" >'.$names['f_name'].' '. $names['last_nm'] . '</option>'; } ?> </select>
another edit show mysqli prepared statement example
assuming inserting these same values table, you'd this:
your mysqli connection this:
$conn = new mysqli($host, $username, $password, $dbname)
then you'd insert (assuming userid autoincrement key)
$stmt = $dbc->prepare("insert users (userid, f_name, l_name) values (null, ?, ?)"); $stmt->bind_param("ss", $names['f_name'],$names['l_name']) $stmt->execute(); $stmt->close();
the "ss"
means both variables strings; if integers you'd use i
. if doing update, you'd replace variables you'd use in insert ?
placeholders. works same way.
here's manual: mysqli prepared statements
Comments
Post a Comment