select items from mysql - php

making a simple movie review site to practice PHP. on one page ( a form) i write a title and review and submit, it then adds the info to mysql. i'm trying to create a page where i can delete reviews i've written. i'm going about this by returning all titles into a form tag, where i can select on and then submit that form to a process page and delete the item.
having issues with the WHILE statement and SQL statement.
$conn = mysql_connect($host, $user, $password)
or die("couldn't make connection");
mysql_select_db('cms', $conn)
or die("couldn't select database");
$sql = "SELECT * FROM frontPage";
$sql_result = mysql_query($sql, $conn)
or die("couldn't execute query");
while($row = mysql_fetch_array($sql_result)) {
$movieTitle = $row['title'];
}
?>
<form method="post" action="deleteReview_process.php">
<select name="title">
<option><?php echo $movieTitle; ?>
</select>
<input type="submit" name="delete" id="delete" value="delete" />
</form>

Try this, the fixes include closing your option tag, and including the option tag inside the MySQL loop so the option tag gets outputted each time there is a new item.
<?
$conn = mysql_connect($host, $user, $password)
or die("couldn't make connection");
mysql_select_db('cms', $conn)
or die("couldn't select database");
$sql = "SELECT * FROM frontPage";
$sql_result = mysql_query($sql, $conn)
or die("couldn't execute query");
?>
<form method="post" action="deleteReview_process.php">
<select name="title">
<?
while($row = mysql_fetch_array($sql_result)) {
$movieTitle = $row['title'];
?>
<option><?php echo $movieTitle; ?></option>
<?
}
?>
</select>
<input type="submit" name="delete" id="delete" value="delete" />
</form>

Well for one thing, you're overwriting $movieTitle each time your loop repeats...don't you want to display all movie titles in your select list?

If you want to display all the movie titles, you need to read the results in and store them all. Currently you are overwriting $movieTitle every time you pull a record from your result. Try something like this:
$titles = array();
while($row = mysql_fetch_array($sql_result)) {
$titles[] = $row['title'];
}
//...
<select name="title">
<option><?php echo implode("</option>\n<option>",$titles); ?></option>
</select>

Related

how to input default value into hidden input

I have this database with the following data:
data 1: text1
data 2: text2
.
.
.
data 14: text 14
I was trying to input this data into a "select" column. So I had done this:
<script>
function dropdownlistchange(dropDown) {
var selectedValue = dropDown.options[dropDown.selectedIndex].value;
document.getElementById("category").value = selectedValue;
}
</script>
and the display is:
<?php
$con= mysqli_connect("localhost","root","") or die ("could not connect to mysql");
mysqli_select_db($con,"adminsys") or die ("no database");
$query = "SELECT * FROM productmodelcategory";
$results = mysqli_query($con, $query)or die("Connection could not be established");
echo "<select class='categoryoption' name='categorylist' id='categorylist' onChange='dropdownlistchange(this);'>";
while ($row = mysqli_fetch_assoc($results))
{
echo "<option value='".$row['Category']."'selected='selected'>".$row['Category']."</option>";
}
echo "</select>";
?>
and my display output is:
<input name="category" type="hidden" id="category" value="" required="required"/>
The problem that I having is I am attaching this coding in a submit form, and when I try to not selecting any value due to leave it as default value, and when i submit form, the value displaying "Column 'Category' cannot be null". Anyone can help me solve this problem? I not sure changing the "onchange" method into what method.
Your category input will hold some value when user actually select something from dropdown. If user didn't select anything in this case category input will not hold any value.
Do one thing assign value attribute to your category input like below
<input name="category" type="hidden" id="category" required="required" value="0"/>
Else, you can set 1st row value as default as follow
<?php
$con= mysqli_connect("localhost","root","") or die ("could not connect to mysql");
mysqli_select_db($con,"adminsys") or die ("no database");
$query = "SELECT * FROM productmodelcategory";
$results = mysqli_query($con, $query)or die("Connection could not be established");
echo "<select class='categoryoption' name='categorylist' id='categorylist' onChange='dropdownlistchange(this);'>";
$i = 0;
$default_value =0;
while ($row = mysqli_fetch_assoc($results))
{
if($i == 0){
$default_value = $row['Category'];
$i = 1;
}
echo "<option value='".$row['Category']."'selected='selected'>".$row['Category']."</option>";
}
echo "</select>";
?>
<input name="category" type="hidden" id="category" required="required" value="<?php echo $default_value;?>"/>

Fill drop down list on page load php

I have two input text fields where user has to specify the begin and end of the fly.
<input type="text" name="start" placeholder="Start destination">
<input type="text" name="end" placeholder="End destination">
I would like to change that and give user to chose start and end destination from database.
<select>
<option value="$id">$name</option>
</select>
I know how to get done if i read database and input values manually, but i know its posible if page loads and execute my SELECT QUERY.
So i have to create dropdown list and fill that with a values from database.
This dropdown list has to be filled when the page load.
Some idea for this ???
I am working with php.
Thank you in advance !!
EDIT : I get done this only with php.
<?php
$db_host = "localhost";
$db_username = "root";
$db_password = "";
$db_name = "flights";
$conn = mysql_connect("$db_host","$db_username","$db_password") or die ("no conn");
#mysql_select_db("$db_name") or die ("no database");
if ($conn = true) {
// echo "";
}
//cyrilic
$sql = "SET NAMES 'utf8'";
mysql_query($sql);
//query for end
$sql="SELECT Distinct end from flights_table;";
$result=mysql_query($sql);
echo "<select name=\"city\">";
echo "<option>end destination</option>";
while ($row = mysql_fetch_array($result))
{
echo "<option value='".$row['end']."'>".$row['end']." </option>";
}
echo "</select>";
?>
This php fires when page loads. Those select options i have putted in a form, and when form is submited, it fires php itself. I am getting selected options this way :
$startfly=$_POST['end'];
I am doing this for starting the flight :)
Thank you guys !
Try this :
At the top of page include your database connection file :
<?php
require "connection.php";
?>
Then :
<?php
$selectStart = "Start : <select name='start'>";
$selectEnd = "End : <select name='end'>";
$query = mysql_query("SELECT * FROM someTable ORDER BY dateField ASC");
if(mysql_num_rows($query) > 0)
{
while($row = mysql_fetch_assoc($query))
{
$selectStart .= "<option value='".$row['startItem']."'>".$row['startItemName']."</option>";
$selectEnd .= "<option value='".$row['endItem']."'>".$row['endItemName']."</option>";
}
}
$selectStart = "</select>";
$selectEnd = "</select>";
?>
In your HTML :
<form action='destinationPage.php' method='post'>
<?php
echo $selectStart;
echo $selectEnd;
?>
<input type='submit' value='Submit' />
</form>

Keep getting Notice: Undefined index: whatever I do

I am trying to fill a table in my database from a drop down menu which I populated from another table from my database. The problem is that whenever I submit my query, it gives me the same error "Notice: Undefined index:" and won't fill the table. I am new to coding, so please be gentle.
This is the part for populating the drop down menu
<?php
#mysql_connect("localhost", "root","") or die(mysql_error());
mysql_select_db("motocikli") or die(mysql_error());
$query = "SELECT kategorija_ime FROM kategorija";
$result = mysql_query($query) or die(mysql_error()."[".$query."]");
?>
<select name="kateg">
<?php
while ($row = mysql_fetch_array($result))
{
echo "<option value='".$row['kategorija_ime']."'>'".$row['kategorija_ime']."'</option>";
}
?>
</select>
<form action="insert.php" method="post">
<input type="submit">
</form>
And this is the insert.php
<?php
$dsn = 'mysql:dbname=motocikli;host=127.0.0.1';
$user = 'root';
$password = '';
$pdo = new \PDO($dsn, $user, $password);
function unesiPoruku($kateg)
{
global $pdo;
$upit = $pdo->prepare("INSERT INTO test (kateg) VALUES (:kateg)");
$upit->bindParam('kateg',$kateg);
$upit->execute();
}
$kateg = $_REQUEST['kateg'];
unesiPoruku($kateg);
?>
The error is showing for $kateg = $_REQUEST['kateg'];, the 'kateg' tag.
Your select box needs to be inside the form so that the value is posted properly to the server
ie.
<form action="insert.php" method="post">
<select name="kateg">
<?php
while ($row = mysql_fetch_array($result))
{
echo "<option value='".$row['kategorija_ime']."'>'".$row['kategorija_ime']."'</option>";
}
?>
</select>
<input type="submit">
</form>

I am accessing MySQL table in PHP but it is not executing if condition and directly display the table

The code below is accessing the database table directly, but I want it to display the table content on giving conditions in drop down menu like when I select islamabad in one drop down menu and lahore in other as given in code and press search button, then it display the table flights, but it is displaying it directly
<p class="h2">Quick Search</p>
<div class="sb2_opts">
<p>
</p>
<form method="post" action="haseeb.php">
<p>Enter your source and destination.</p>
<p>
From:</p>
<select name="from">
<option value="Islamabad">Islamabad</option>
<option value="Lahore">Lahore</option>
<option value="murree">Murree</option>
<option value="Muzaffarabad">Muzaffarabad</option>
</select>
<p>
To:</p>
<select name="To">
<option value="Islamabad">Islamabad</option>
<option value="Lahore">Lahore</option>
<option value="murree">Murree</option>
<option value="Muzaffarabad">Muzaffarabad</option>
</select>
<input type="submit" value="search" />
</form>
</form> </table>
<?php
$from = isset($_POST['from'])?$_POST['from']:'';
$to = isset($_POST['to'])?$_POST['to']:'';
if( $from =='Islamabad'){
if($to == 'Lahore'){
$db_host = 'localhost';
$db_user = 'root';
$database = 'homedb';
$table = 'flights';
if (!mysql_connect($db_host, $db_user))
die("Can't connect to database");
if (!mysql_select_db($database))
die("Can't select database");
$result = mysql_query("SELECT * FROM {$table}");
if (!$result) {
die("Query to show fields from table failed");
}
$result = mysql_query("SELECT * FROM {$table}");
if (!$result) {
die("Query to show fields from table failed");
}
$fields_num = mysql_num_fields($result);
echo "<h1>Table: {$table}</h1>";
echo "<table border='1'><tr>";
while($row = mysql_fetch_row($result))
{
echo "<tr>";
// $row is array... foreach( .. ) puts every element
// of $row to $cell variable
foreach($row as $cell)
echo "<td>$cell</td>";
echo "</tr>\n";
}
}
}
mysqli_close($con);
?>
Fix the mysql_ and mysqli_.
Do not repeat the SQL Query to test $result. One is ok.
Fix the case of index - to or To.
Table displayed directly: Hope you are not refreshing the form with the pre-submitted values of your cities. Freshly open the page link, and confirm that $from, $to are really empty. If you are pressing F5 in browser, it may be wrong.

Delete from database php

I have a problem with a delete from database..So, I have:
<?php
include('createdb.php');
if(!empty ($_POST['tribuna']))
{
$delete = mysql_query("DELETE FROM tb_tribuna WHERE id = '".$_POST['tribuna']."';");
header("Location:index.php?a=buy"); //redirect
exit;
}
?>
<form id="formid" action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<label>Tribuna :</label> <select name="tribuna" class="tribuna">
<option selected="selected">-Select-</option>
<?php
$sql=mysql_query("select id,tribune_number from tb_tribuna ");
while($row=mysql_fetch_array($sql))
{
$id=$row['id'];
$tribune_number=$row['tribune_number'];
echo '<option value="'.$id.'">'.$tribune_number.'</option>';
} ?>
</select><br/><br/>
<input name="delete" type="submit" id="delete" value="Delete">
</form>
When I push on submit nothing happens...
I want that when I select an option and when I press delete to delete from the database row...
Help plizzz friends..
Assuming that "createdb.php" has the correct database connection information:
$conn = mysql_connect("$host","$db_uid","$db_pwd");
mysql_select_db("$db", $conn);
make your delete function look like this:
$sql = "DELETE FROM tb_tribuna WHERE id = '$_POST[tribuna]' ";
$result = mysql_query($sql, $conn) or die(mysql_error());
You need to pass the db connection to mysql_query.
And add "or die mysql_error()" to your mysql statements so that when something doesn't work, you get an error message that helps point you to where the problem is.

Categories