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;?>"/>
Related
I'm trying to create a feature that allows me to add a new column to a table in my database using sql and php. I currently have a front end form, where the user can select what table they wish to alter and input boxes for adding a new column name. I also want to display the new version of the selected table (i.e. with the new column attached) My front and back end scripts look like this below.
sql10.html:
<form action="sql10.php" method="post">
<select id="category">
<option value="" disabled selected>Select your option</option>
<option id="customer" value="customer">Customer Table</option>
<option id="booking "value="booking">Booking Table</option>
<option id="bookingline" value="bookingline">Order Table</option>
<option id="package" value="package">Package Table</option>
</select>
<br><br>
Column Name: <input type="text" name="colname" placeholder="Insert New Column Name">
<br>
<input type="submit" value="Submit"> <input type="reset" value="Clear">
sql.php:
<?php
$conn = mysqli_connect("localhost", "######", "#####")
or die("Could not connect: " . mysqli_error($conn));
print "Successful Connection!";
mysqli_select_db($conn, '#####') or die ('db will not open' );
print " You have connected to the Assignment 3 database.<br>";
$category = $_POST["category"];
$newcolumn = $_POST["colname"];
$query1="ALTER TABLE $category ADD $newcolumn varchar(255)";
$query2 = "SELECT * FROM $category";
mysqli_query($conn, $query1) or die ("Invalid query");
echo "Success in database entry";
$numrows = mysqli_affected_rows($conn);
echo $numrows . "row updated<br>";
$result = mysqli_query($conn, $query) or die("Invalid query");
$num = mysqli_num_rows($result);
echo "<table border='1'><tr><th>Booking ID</th></tr>";
for($i=0; $i<$num; $i++) { //uses $num as loop end value
$row = mysqli_fetch_row($result);
echo "<tr><td>" . $row[0] . "</td></tr>";
}
echo "</table>";
mysqli_close($conn);
?>
As of using this code, I am not able to create a new column for tables. My database consists of multiple tables and I want the user to be able to select which table they wish to add a column to as well as enter the name of the new column. Can anybody see what the problem is?
I think your mistake is that your select tag doesn't have a name attribute. Remember that a form when is submitted, it takes the value of elements with the name attribute.
So, if you put in your HTML code <select id="category" name="category">, you will be able to catch its value in your sql.php query with $_POST["category"]; maybe the $_POST["category"] is arriving empty, and therefore you cannot execute the $query1 sentence.
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>
So right now my input form looks something like this
<form method = "post" action = "Display_Department_Info.php">
Select Department :
<select name = "dept">
<option value="" selected >-- Select One--</option>
<?php
$conn = mysqli_connect('localhost', 'root', 'root', 'DM Phase 2');
if(!$conn) {
echo "Failed to connect to MySQL: ".mysqli_connect_error();
}
$result = mysqli_query($conn, 'select name from department');
while($row = mysqli_fetch_array($result)) {
$val = $row['name'];
echo "<option value ='".$val."'>".$val."</option>";
}
mysqli_close($conn);
?>
</select>
<input type = "submit" value = "Submit" />
</form>
but when I use the following command to read $_POST[dept] for a particular input
"Children's IKEA", it only displays "Children"
This is my reading code.
$departmentinput = mysql_real_escape_string("$_POST[dept]");
Please help! thank you :3
The ' is breaking it because you are using ' to quote the value:
$val = htmlspecialchars($row['name'], ENT_QUOTES);
//or
$val = htmlentities($row['name'], ENT_QUOTES);
I want to create a book inventory with Create, Read, Update and Delete. I got it all going except for the Update query. Here is my code. First is the proj.php
<html>
<body>
<b>BOOK INVENTORY</b>
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
Title: <input type="text" name="title">
Subtitle: <input type="text" name="subtitle">
Author: <input type="text" name="author">
Genre/Type: <select name="genre">
<option value="Business">Business</option>
<option value="Family">Family</option>
<option value="Romance">Romance</option>
<option value="Education">Education</option>
<option value="Self-help">Self-help</option>
<option value="Spiritual">Spiritual</option>
<option value="Music">Music</option>
</select>
Publish Date: <input type="date" name="publishdate">
Publisher: <input type="text" name="publisher">
Series: <input onkeypress="return isNumberKey(event)" type="text" name="series">
ISBN: <input type="text" name="isbn">
Pages: <input onkeypress="return isNumberKey(event)" type="text" name="pages">
Price: <input type="text" value="$" name="price">
<input type="submit" value="Create" name="submit">
</form>
<!--create new book entry-->
<?php
include 'connection.php';
if (!isset($_POST['submit'])) {
// form not submitted
}
else {
// get form input
// check to make sure it's all there
// escape input values for greater safety
$title = empty($_POST['title']) ? die ("ERROR: Enter a Title") : mysql_escape_string($_POST['title']);
$subtitle = empty($_POST['subtitle']) ? die ("ERROR: Enter an Subtitle") : mysql_escape_string($_POST['subtitle']);
$author = empty($_POST['author']) ? die ("ERROR: Enter an Author") : mysql_escape_string($_POST['author']);
$genre = empty($_POST['genre']) ? die ("ERROR: Enter a Genre/Type") : mysql_escape_string($_POST['genre']);
$publishdate = empty($_POST['publishdate']) ? die ("ERROR: Enter a Publish Date") : mysql_escape_string($_POST['publishdate']);
$publisher = empty($_POST['publisher']) ? die ("ERROR: Enter a Publisher") : mysql_escape_string($_POST['publisher']);
$series = empty($_POST['series']) ? die ("ERROR: Enter a Series") : mysql_escape_string($_POST['series']);
$isbn = empty($_POST['isbn']) ? die ("ERROR: Enter an ISBN") : mysql_escape_string($_POST['isbn']);
$pages = empty($_POST['pages']) ? die ("ERROR: Enter a Pages") : mysql_escape_string($_POST['pages']);
$price = empty($_POST['price']) ? die ("ERROR: Enter a Price") : mysql_escape_string($_POST['price']);
// create query
$insert = "INSERT INTO books (Title, Subtitle, Author, Genre, Publishdate, Publisher, Series, ISBN, Pages, Price)
VALUES ('$title', '$subtitle', '$author', '$genre', '$publishdate', '$publisher', '$series', '$isbn', '$pages', '$price')";
// execute query
$result = mysql_query($insert) or die ("Error in query: $insert. ".mysql_error());
// print message of the new book inserted
//echo "New book record created entitled "."$ ";
}
if (!isset($_POST['submit'])) {
}
$query = "SELECT * FROM symbols";
?>
<!------------><!------------><!------------><!------------><!------------>
<!------------><!------------><!------------><!------------><!------------>
<!--Delete Record-->
<?php
// set server access variables
$host = "localhost";
$user = "root";
$pass = "";
$db = "david";
// create mysqli object
// open connection
$mysqli = new mysqli($host, $user, $pass, $db);
// check for connection errors
if (mysqli_connect_errno()) {
die("Unable to connect!");
}
// if id provided, then delete that record
if (isset($_GET['id'])) {
// create query to delete record
$query = "DELETE FROM books WHERE id = ".$_GET['id'];
// execute query
if ($mysqli->query($query)) {
// print number of affected rows
echo $mysqli->affected_rows." row(s) affected";
}
else {
// print error message
echo "Error in query: $query. ".$mysqli->error;
}
}
// query to get records
$query = "SELECT * FROM books";
// execute query
if ($result = $mysqli->query($query)) {
// see if any rows were returned
if ($result->num_rows > 0) {
// yes
// print them one after another
echo "<table cellpadding=10 border=1>";
while($row = $result->fetch_array()) {
echo "<tr>";
echo "<td>".$row[0]."</td>";
echo "<td>".$row[1]."</td>";
echo "<td>Delete</td>";
echo "<td>More...</td>";
echo "</tr>";
}
}
// free result set memory
$result->close();
}
else {
// print error message
echo "Error in query: $query. ".$mysqli->error;
}
// close connection
$mysqli->close();
?>
<?php
include 'edit.php';
if (!isset($_POST['submit'])) {
}
?>
And here is my edit.php where I get the error undefined index : id at line 5
<?php
mysql_connect('localhost', 'root', '') or die(mysql_error());
mysql_select_db("david") or die(mysql_error());
$UID = (int)$_GET['id'];
$query = mysql_query("SELECT * FROM books WHERE 'id' = '$UID'") or die(mysql_error());
if(mysql_num_rows($query)>=1){
while($row = mysql_fetch_array($query)) {
$title = $row['title'];
$subtitle = $row['subtitle'];
$author = $row['author'];
$genre = $row['genre'];
$publishdate = $row['publishdate'];
$publisher = $row['publisher'];
$series = $row['series'];
$isbn = $row['isbn'];
$pages = $row['pages'];
$price = $row['price'];
}
?>
<form action="update.php" method="get">
<input type="hidden" name="id" value="<?=$UID;?>">
Title: <input type="text" name="ud_title" value="<?=$title?>"><br>
Subtitle: <input type="text" name="ud_subtitle" value="<?=$subtitle?>"><br>
Author: <input type="text" name="ud_author" value="<?=$author?>"><br>
Genre: <input type="text" name="ud_genre" value="<?=$genre?>"><br>
Publish Date: <input type="text" name="ud_publishdate" value="<?=$publishdate?>"><br>
Publisher: <input type="text" name="ud_publisher" value="<?=$publisher?>"><br>
Series: <input type="text" name="ud_series" value="<?=$series?>"><br>
ISBN: <input type="text" name="ud_isbn" value="<?=$isbn?>"><br>
Pages: <input type="text" name="ud_pages" value="<?=$pages?>"><br>
Price: <input type="text" name="ud_price" value="<?=$price?>"><br>
<input type="Submit">
</form>
<?php
}else{
}
?>
and here is my update.php with the update query
<?php
mysql_connect('localhost', 'root', '') or die(mysql_error());
mysql_select_db("david") or die(mysql_error());
$ud_ID = (int)$_POST["id"];
$ud_title = mysql_real_escape_string($_POST["ud_title"]);
$ud_subtitle = mysql_real_escape_string($_POST["ud_subtitle"]);
$ud_author = mysql_real_escape_string($_POST["ud_author"]);
$ud_genre = mysql_real_escape_string($_POST["ud_genre"]);
$ud_publishdate = mysql_real_escape_string($_POST["ud_publishdate"]);
$ud_publisher = mysql_real_escape_string($_POST["ud_publisher"]);
$ud_series = mysql_real_escape_string($_POST["ud_series"]);
$ud_isbn = mysql_real_escape_string($_POST["ud_isbn"]);
$ud_pages = mysql_real_escape_string($_POST["ud_pages"]);
$ud_price = mysql_real_escape_string($_POST["ud_price"]);
$query="UPDATE books
SET title = '$ud_title', subtitle = '$ud_subtitle', author = '$ud_author', genre = '$ud_genre', publishdate = '$ud_publishdate',
publisher = '$ud_publisher', series = '$ud_series', isbn = '$ud_isbn', pages = '$ud_pages', price = '$ud_price'
WHERE id='$ud_ID'";
mysql_query($query)or die(mysql_error());
if(mysql_affected_rows()>=1){
echo "<p>($ud_ID) Record Updated<p>";
}else{
echo "<p>($ud_ID) Not Updated<p>";
}
?>
Please kindly help me get rid of this error:
undefined index: id at edit.php.
I'm shocked by the terrible formating and not able to read the code.
But according to your description, you get undefined index: id because you either did not include the id field in the form, or you include it in the url but use $_POST instead of $_GET.
EDIT:
If this is exactly the code you have, then the problem should be the last few lines in proj.php.
include 'edit.php' is not the correct way to call edit page, but you should use a link tag which links to edit.php?id=some_id, just like you did with show.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>