show one table from mysql in select option? - php

look at my plugin it show all table from WordPress in select option. I want to show default one table in option this my code and screenshot.
<form id="wp_csv_to_db_form" method="post" action="">
<table class="form-table">
<tr valign="top"><th scope="row"><?php _e('Select
Database Table:','wp_csv_to_db'); ?></th>
<td>
<select id="table_select" name="table_select" value="w<option name=""
value="wp_orderlist"></option>
<?php // Get all db table names
global $wpdb;
$sql = "SHOW TABLES";
$results = $wpdb->get_results($sql);
$repop_table = isset($_POST['table_select'])
? $_POST['table_select'] : null;
foreach($results as $index => $value) {
foreach($value as $tableName) {
?><option name="<?php echo
$tableName ?>" value="<?php echo $tableName ?>" <?php
if($repop_table === $tableName) { echo 'selected="selected"'; } ?>>
<?php echo $tableName ?></option><?php
}
}
?>
</select>
[enter image description here][1]

Replace your current sql code:
$sql = "SHOW TABLES";
to:
$sql = "SHOW TABLES LIKE '" . $_POST['table_select'] . "'";
This will select one table specified in $_POST['table_select']

try this
work on native php
<select name="Kode" class="form-control">
<?php
$konek = mysqli_connect("localhost","root","","dbsia");
$query = "select * from tbpelajaran";
$hasil = mysqli_query($konek,$query);
while($data=mysqli_fetch_array($hasil)){
echo "<option value=$data[kodepelajaran]>$data[kodepelajaran]</option>";
}
?>

Related

PHP Array input name to database

I am working on a hotel feature page.
the peice of code I am stuck on is the following:
<?php
$result = mysqli_query($conn, "select * from facilities_type");
while($row = mysqli_fetch_assoc($result))
{
?>
<tr>
<th scope="row">
<input class="form-check-input" type="checkbox" id="blankCheckbox" name="room_feature_cb[]" value="<?php echo $row["facilitiestype_id"]; ?>">
</th>
<td>
<?php echo $row["room_facilities"] ?>
</td>
<td>
<img src="upload-img/icon/<?php echo $row["facilities_icon"]; ?>" width="25">
</td>
</tr>
<?php
}
?>
I want to save the data by using the array, but i cannot get the name to save into the database
if (isset($_POST["room_feature_savebtn"]))
{
$feature = $_POST['room_feature_cb'];
for($result=0;$result>$feature;$result++)
{
mysqli_query($conn,"insert into facilities_details(facilitiestype_id) value ('$feature')");
}
}
You can use the foreach, is a better option in this case:
if (isset($_POST["room_feature_savebtn"]))
{
$features = $_POST['room_feature_cb'];
foreach ($features as $feature) {
mysqli_query($conn, "insert into facilities_details(facilitiestype_id) value ('$feature')");
}
}
Your error is when make a for you dont use the index of array ($result) that create in this, try with:
if (isset($_POST["room_feature_savebtn"]))
{
$feature = $_POST['room_feature_cb'];
for($result=0; $result < $feature; $result++) {
mysqli_query(
$conn,
"insert into facilities_details(facilitiestype_id) value ('" . $feature[$result] . "')"
);
}
}
I recomend the foreach

PHP/SQL - How can I get the name of tables in my database?

I am trying to use the function mysqli_fetch_field() to get the name of each of my tables in the database. However when i try to output the table name using $fieldInfo->table i get duplicates. How can i select only 1 column from each table so that $fieldInfo->table isnt called for every column of each table?
current sql:
$sql = "SELECT * from administrators, bookings, customers, rooms";
$results = mysqli_query($conn, $sql)
or die ('Problem with query' . mysqli_error($conn));
my code to display the table name in radio buttons:
<?php
while ($fieldInfo = mysqli_fetch_field($results)) {
?>
<input type="radio" name="tableNames" value="<?php echo $fieldInfo->table; ?>"> <?php echo $fieldInfo->table ?> <br>
<?php } ?>
I added 2 temporary table name holder and made an IF condition that only outputs the radio buttons once the 2 temporary name holders are different.
<?php
$tempName2 = "";
while ($fieldInfo = mysqli_fetch_field($results)) {
$tempName = $fieldInfo->table;
if ($tempName != $tempName2) {
$tempName2 = $tempName;
?>
<input type="radio" name="tableNames" value="<?php echo $tempName; ?>" > <?php echo $tempName ?> <br>
<?php }
} ?>
<?php
$query='SHOW TABLES FROM DB_NAME';
$results=mysqli_query($conn,$query);
while ($fieldInfo = mysqli_fetch_array($results)) { ?>
<input type="radio" name="tableNames" value="<?php echo $fieldInfo[0]; ?>"> <?php echo $fieldInfo[0]; ?> <br>
<?php } ?>

PHP Form with MySql Database

Hi I need some help to create an form from database:Database
And I have this:
global $wpdb;
//$query ="SELECT modelo,ID FROM volumes";
$query ="SELECT Modelo,ID FROM volumes order by ID";
$wpdb->query($query)
And this:
<div class="form_fields">
<p>
<label for="modelo">Model:
echo "<select name=Modelo value=''>Modelo</option>"; // list box select command
foreach ($wpdb->query($query) as $row){//Array or records stored in $row
echo "<option value=$row[ID]>$row[Modelo]</option>";
/* Option values are added by looping through the array */
}
echo "</select>";// Closing of list box
</label>
</p></div>
What is wrong?
Please check this :
<?php
global $wpdb;
$query ="SELECT Modelo,ID FROM volumes order by ID";
$wpdb->query($query);
?>
<div class="form_fields">
<label for="modelo">Model: </label>
<select name=Modelo value=''>
<option>Modelo</option>
<?php
foreach ($wpdb->query($query) as $row) {
?>
<option value="<?php echo $row->ID; ?>"><?php echo $row->Modelo; ?></option>
<?php
}
?>
</select>
</div>
Try this :
<?php
global $wpdb;
//$query ="SELECT modelo,ID FROM volumes";
$query = "SELECT Modelo,ID FROM volumes order by ID";
$results = $wpdb->get_results($query);
?>
<div class="form_fields">
<p>
<label for="modelo">Model:
<select name=Modelo>
<option value=''>Modelo</option>
<?php
foreach ($results as $row) {//Array or records stored in $row
echo "<option value='{$row->ID}'>{$row->Modelo}</option>";
/* Option values are added by looping through the array */
}
?>
</select>
</label>
</p>
</div>
Please try this
Model:
Modelo";
foreach ($wpdb->query($query) as $row) {
echo "" . $row['Modelo'] . "";
}
echo "";
?>

Drop down, results table not showing Mysql php

Trying to get the results from the Mysql to show up on the web page.
The process is that the user would select a make of a car and then it will show just that make in a table.
I've been trying different things but I cant seem to get it to show the results. As soon as I get rid of the WHERE statement in the sql query it shows all the cars/makes. I think the problem is in the sql statement or the if.
This is what I've got so far.
<HTML >
<head>
<title>Inventory</title>
</head>
<body>
<form method="get" action="TaskC.php">
Please select a make:
<select name = "make" >
<option value = "All">All</option>
<option value = "Toyota">Toyota</option>
<option value = "Holden">Holden</option>
<option value = "Ford">Ford</option>
<option value = "Nissan">Nissan</option>
</select> <br/>
<br/>
<input type="submit" value="Search" name="Search" />
<table width="600" border="1" cellpadding="1" cellspacing="1">
<tr>
<th>Make</th>
<th>Model</th>
<th>Price</th>
<th>Quantity</th>
<tr>
</form>
<?php
//error_reporting (E_ALL ^ E_NOTICE);
$dbConnect = mysqli_connect('xxxxxxxxx', 'xxxxxxxxx','xxxxxxxx')
or die("<p>The database server is not available.</p>");
$dbSelect = mysqli_select_db( $dbConnect,'xxxxxxxx_db' )
or die("<p>The database is not available.</p>");
$make = $_GET['make'];
$sqli = "SELECT * FROM inventory WHERE make = '" .$make. "'";
$result = mysqli_query($dbConnect,$sqli);
if (isset($_GET['make']) )
{
while ($inventory = mysqli_fetch_assoc($result) )
{
echo "<tr>";
echo "<td>".$inventory['make']."</td>";
echo "<td>".$inventory['model']."</td>";
echo "<td>".$inventory['price']."</td>";
echo "<td>".$inventory['quantity']."</td>";
echo "</tr>";
}
}
mysqli_close($dbConnect);
?>
</body>
</HTML>
Hope you can help.
Thanks
There is an error in the query. It should be -
$sqli = "SELECT * FROM inventory WHERE make = '" .$make. "'";
Edit
if (isset($_GET['make']) ){
$make = $_GET['make'];
$sqli = "SELECT * FROM inventory WHERE make = '" .$make. "'";
$result = mysqli_query($dbConnect,$sqli);
while ($inventory = mysqli_fetch_assoc($result) )
{
echo "<tr>";
echo "<td>".$inventory['make']."</td>";
echo "<td>".$inventory['model']."</td>";
echo "<td>".$inventory['price']."</td>";
echo "<td>".$inventory['quantity']."</td>";
echo "</tr>";
}
}

Pass PHP array through Select Option Fields

I am writing a basic CMS system and have come across something which should be seemingly simple -but is beginning to frustrate me.!
I am trying to pass an array through a select option field to populate a list of categories in which I can save a post.
I have a 'posts' form which comprises of 3 fields. Title, content and Category ID (CatID).
When the user creates a post, they can select the category they wish to assign the post assigned to by using a drop down list - (this is populated by using a different form).
So the technical bit; -
MySQL DB:-
categories = catname (char60 PRIMARY), catid (INT10, AI)
posts = id (bigint20 PRIMARY), catid (int10 PRIMARY), title (text), content (varchar255)
Example of categories populates: catname = Home / catid = 1 ...etc
Output.php ;
<?php
function display_post_form($post = '') {
$edit = is_array($post);
?>
<form action="<?php echo $edit ? 'edit.php' : 'add.php' ; ?>" method="post">
<table border="0">
<tr>
<td> Title:</td>
<td> <input type="text" name="title" value="<?php echo $edit ? $post['title'] : '' ; ?>" size="60" /> </td>
</tr><tr>
<td> Content:</td>
<td> <textarea id="editor1" name="content" value="<?php echo $edit ? $post['content'] : '' ; ?>"> </textarea> </td>
</tr><tr>
<td> Category:</td>
<td><select name="catid">
<?php
$cat_array = get_categories($catid, $catname);
foreach($cat_array as $thiscat) {
echo "<option value=\"".$thiscat['catid']."\" ";
if (($edit) && ($thiscat['catid'] == $post['catid'])) {
echo " selected";
}
echo ">".$thiscat['catname']."</option>";
}
?>
</select>
</td>
</tr><tr>
<td> Button:</td>
<td <?php if (!$edit) { echo "colspan=2"; } ?> align="center">
<?php
if ($edit)
echo "<input type=\"hidden\" name=\"_id\" value=\"". $post['id'] ."\" />";
?>
<input type="submit" value="<?php echo $edit ? 'Update' : 'Add' ; ?> Post" />
</form></td>
</td>
</tr>
</table>
</form>
<?php
}
?>
Functions.php ;
function get_categories($catid, $catname) {
$conn = db_connect();
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL " .mysqli_connect_error();
}
$sql = "SELECT catname, catid FROM categories";
$result = mysqli_query($conn, $sql) or die(" Could not query database");
while($row = mysqli_fetch_assoc($result)) {
printf("\n %s %s |\n",$row["catname"],$row["catid"]);
}
mysqli_close($conn);
}
I am able to call in the 'get_cattegories()' function which generates a flat data of categories and their respective id's. I then combined this with the Select Option Field in the Output.php file and it doesn't generate anything.
Can anyone give some useful tips or advice? Many thanks :)
You are not returning the array but printing a string to the output. Change printf to return:
function get_categories($catid, $catname) {
$conn = db_connect();
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL " .mysqli_connect_error();
}
$sql = "SELECT catname, catid FROM categories";
$result = mysqli_query($conn, $sql) or die(" Could not query database");
$categories = array();
while($row = mysqli_fetch_assoc($result)) {
$categories[] = $row;
}
mysqli_close($conn);
return $categories;
}
Also I agree for the comments to your question. The arguments are useless.
You also may refactor the code, actually... alot. Move the mysql_connect() to the other place, probably at the beginning of your script.
I suggest to use some frameworks. I think KohanaPHP will be a good start. You will learn about architecture and some design patterns. Keep the good work and improve your skills ;-)

Categories