Update mysql query with reading HTML SELECT - php

I'm making an orderform, just an internal one.
My form can already add a new order and summarize it on the internal page.
Form is made in a table.
Database queries (which are all displayed in the table): ordernr, klantnaam (customername), productnaam (productname), productid, status
Now, I'd like to change the status (with the HTML select option) by.. lets say "Not ordered" to "Ordered". and when I press the submit button it should "update" it in the SQL database.
HTML select list: "Niet besteld, Besteld, Onderweg naar hoofdlocatie, Onderweg naar vestiging, Ontvangen".
How do I do this?
$con=mysqli_connect("localhost","root","","bestelformulier");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM overzicht");
//table heading
echo "<table align='center' width='700px' border='2'>
<tr>
<th width='10px'>Ordernr</th>
<th width='10px'>Klantnaam</th>
<th width='10px'>Productnaam</th>
<th width='10px'>ProductID</th>
<th width='10px'>Status</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['ordernr'] . "</td>";
echo "<td>" . $row['klantnaam'] . "</td>";
echo "<td>" . $row['productnaam'] . "</td>";
echo "<td>" . $row['productid'] . "</td>";
echo "<td><select><option>" . $row['status'] . "</option></select></td>";
//end table
mysqli_close($con);
//adding order table
echo "<h5>Bestelling Toevoegen</h5>";
echo "<form method='post'>";
echo "<table width='700px' border='1'>
<tr>
<th>klantnaam</th>
<td><input type='text' name='klantnaam'/></td>
</tr>
<tr>
<th>Productnaam</th>
<td><input type='text' name='productnaam'/></td>
</tr>
<tr>
<th>productid</th>
<td><input type='text' name='productid'/></td>
</tr>
<tr>
<th>Status</th>
<td>
<select name='status'>
<option value='Niet besteld'>Niet besteld</option>
<option value='Besteld'>Besteld</option>
<option value='Onderweg naar hoofdlocatie'>Onderweg naar hoofdlocatie</option>
<option value='Onderweg naar vestiging'>Onderweg naar vestiging</option>
<option value='Ontvangen'>Ontvangen</option>
<select>
</td>
</tr>
<tr>
<td></td>
<td><input type='submit' name='submit' value='Toevoegen'/></td>
</tr>";
echo "</table>";
echo "</form>";
$klantnaam = $_POST['klantnaam'];
$productnaam = $_POST['productnaam'];
$productid = $_POST['productid'];
$status= $_POST['status'];
if(isset($_POST['submit'])) {
$query = mysqli_query($con,"INSERT INTO overzicht (klantnaam, productnaam, productid, status)
VALUES ('$klantnaam', '$productnaam', '$productid', '$status')");
$current_url = (empty($_SERVER['HTTPS']) ? "http://" : "https://") . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
header ('Location: ' . $current_url);
exit ();
}
mysqli_close($con);
This is the code I have been using so far.
Thanks in advance!

to create a select with all options
change this
echo "<td><select><option>" . $row['status'] . "</option></select></td>";
to
echo "<td><select>
<option>" . $row['status'] . "</option>";
if($row['status'] != "Niet besteld")
echo "<option>Niet besteld</option>";
if($row['status'] != "Besteld")
echo "<option>Besteld</option>";
if($row['status'] != "Onderweg naar hoofdlocatie")
echo "<option>Onderweg naar hoofdlocatie</option>";
if($row['status'] != "Onderweg naar vestiging")
echo "<option>Onderweg naar vestiging</option>";
if($row['status'] != "Ontvangen")
echo "<option>Ontvangen</option>";
echo" </select></td>";

Related

php mysql dropdown value select

Currently the following code displays the filters for the individual publishers ie if I select DC I will only see the DC titles.
The issue is that from the current code I cannot get it to display all publishers from the dropdown menu.
<div id="main">
<form method="post" action="">
<div id="search_query" >
<select name="make" size="0">
<option value="all">All Publishers</option>
<option value="DC">DC</option>
<option value="Marvel">Marvel</option>
<option value="Image">Image</option>
</select>
<input type="submit" name="submit" value="submit">
</div>
</form>
<div id="main_container">
<?php
$db_con = mysql_connect('127.0.0.1','root','root');
if (!$db_con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('work', $db_con);
if(isset($_POST['submit']))
{
$make = mysql_real_escape_string($_POST['make']);
$sql = sprintf("SELECT * FROM products WHERE publisher= '$make' ");
$result = mysql_query($sql);
echo "<table width= 970 border=1>
<tr>
<th width='120' scope='col'>Title</th>
<th width='170' scope='col'>Publisher</th>
<th width='185' scope='col'>Price</th>
<th width='126' scope='col'>Desc</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>".$row['title']."</td>";
echo "<td>". $row['publisher'] . "</td>";
echo "<td>". $row['price'] ."</td>";
echo "<td>". $row['desc'] ."</td>";
echo "</tr>";
}
echo "</table>";
}
else
{
$sql = sprintf("SELECT * FROM products");
$result = mysql_query($sql);
echo "<table width= 970 border=1>
<tr>
<th width='120' scope='col'>Title</th>
<th width='170' scope='col'>Publisher</th>
<th width='185' scope='col'>Price</th>
<th width='126' scope='col'>Desc</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>".$row['title']. "</td>";
echo "<td>". $row['publisher'] . "</td>";
echo "<td>". $row['price'] ."</td>";
echo "<td>". $row['desc'] ."</td>";
echo "</tr>";
}
echo "</table>";
}
mysql_close($db_con);
?>
In your query, it's searching for publisher = $make where $make = "all".
Your database table probably doesn't have any record with publisher = "make".
I'd suggest you to add an if else condition here:
if (isset($_POST['submit'])) {
$make = mysql_real_escape_string($_POST['make']);
if ($make != "all") {
$sql = sprintf("SELECT * FROM products WHERE publisher= '$make' ");
} else {
$sql = sprintf("SELECT * FROM products");
}
/* Other code */
}

PHP connection to a database (no output!)

I wrote a code to connect a html page to the database, but it does not search for all employees in the table. I'm not sure why the code seems to be correct. It does not give any error or warning.
Can anyone explain to me what is wrong?
<div>
<form id='searchform' action='index.php' method='get'>
<a href='index.php'>All employees</a> ---
Search by a last name:
<input id='search' name='search' type='text' size='20' value='<?php echo #$_GET['search']; ?>' />
<input id='submit' type='submit' value='Go!' />
</form>
</div>
<?php
// check if search view of list view
if (isset($_GET['search'])) {
$sql = "SELECT * FROM Employee WHERE LastName like '%" . #$_GET['search'] . "%'";
} else {
$sql = "SELECT * FROM Employee";
}
// execute sql statement
$stmt = oci_parse($conn, $sql);
oci_execute($stmt);
?>
<table style='border: 1px solid #DDDDDD'>
<thead>
<tr>
<th>Personal Number</th>
<th>Insurance Number</th>
<th>First Name</th>
<th>Last Name</th>
<th>Birth Date</th>
<th>Gender</th>
<th>Hire Date</th>
<th>Salary</th>
<th>Office ID</th>
</tr>
</thead>
<tbody>
<?php
// fetch rows of the executed sql query
while ($row = oci_fetch_assoc($stmt)) {
echo "<tr>";
echo "<td>" . $row['PersonalNumber'] . "</td>";
echo "<td>" . $row['InsuranceNumber'] . "</td>";
echo "<td>" . $row['FirstName'] . " " . $row['LastName'] . "</td>";
echo "<td>born on " . $row['BirthDate'] . "</td>";
echo "<td>" . $row['Gender'] . "</td>";
echo "<td>hired on " . $row['HireDate'] . "</td>";
echo "<td>" . $row['Salary'] . "</td>";
echo "<td>" . $row['OfficeID'] . "</td>";
echo "</tr>";
}
?>
</tbody>
</table>
<div>There are found <?php echo oci_num_rows($stmt); ?> employees!</div>
<?php oci_free_statement($stmt); ?>
</body>

Having issue showing image returning from database with $.post() jquery and php

I'm retrieving a table from database as result of a search action, but when I try to display the result I see the table and the data, but the image returned in each row is not render, I think my problem is in the $('#search').html(data), I'm not sure please someone knows what is the problem?
this is the result
http://s9.postimg.org/mro5qn46n/search_result.jpg
****This is the search page, where result table is displayed****
<table align="center">
<tr>
<td>
<label for="criteria">Select Criteria</label>
</td>
<td>
<select name="select" id="criteria">
<option selected="true" style="display:none;"></option>
<option value="value1">name</option>
<option value="value2">apartment</option>
</select>
</td>
<td>
<input type="text" name="value" size="40" maxlength="60" id="value"\>
</td>
</tr>
<tr>
<td>
<input name="name-submit" type="button" id="submit" value="Search"\>
</td>
</tr>
<tr>
<td >
<div id="search"></div>
</td>
</tr>
</table>
<script type="text/javascript" src="../js/jquery-1.11.1.min.js"></script>
<script type="text/javascript">
$('#submit').click(function(){
var criteria = $("#criteria option:selected").text();
var value = $("#value").val();
$.post("search_r.php",{criteria:criteria,value:value},function(data){
$('#search').html(data);
});
});
</script>
****This is the Page that calls $.post() in the main seach page ****
<?php
$criteria = $_POST['criteria'];
$value = $_POST['value'];
$con = mysqli_connect("localhost","root","");
mysqli_select_db($con,"gables");
$query = "SELECT * FROM residents WHERE $criteria = '$value'";
$result = mysqli_query($con,$query);
echo "<table border='1'>
<tr>
<th>Id</th>
<th>Name</th>
<th>Last Name</th>
<th>Apartment</th>
<th>Parking</th>
<th>Phone1</th>
<th>Phone2</th>
<th>image</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['0'] . "</td>";
echo "<td>" . $row['1'] . "</td>";
echo "<td>" . $row['2'] . "</td>";
echo "<td>" . $row['3'] . "</td>";
echo "<td>" . $row['4'] . "</td>";
echo "<td>" . $row['5'] . "</td>";
echo "<td>" . $row['6'] . "</td>";
echo "<td><img src=get_image.php?id=".$row['0']." width=160 height=120/></td>";
echo "</tr>";
}
echo "</table>";
?>
***Here the get_image.php****
<?php
$con = mysqli_connect("localhost","root","");
mysqli_select_db($con,"gables");
$id = $_GET['id'];
$query = "SELECT * FROM residents WHERE id='$id'";
$result = mysqli_query($con,$query);
if($result)
$picture = mysqli_fetch_array($result);
header('Content-Type: image/jpg');
echo $picture['11'];
?>
You can chage the get_image.php file as this. Then this work will work.
<?php
function get_image($id){
$con = mysqli_connect("localhost","root","");
mysqli_select_db($con,"gables");
$query = "SELECT * FROM residents WHERE id='$id'";
$result = mysqli_query($con,$query);
if($result)
$picture = mysqli_fetch_array($result);
return $picture['11'];
}
?>
Then use require_once(); function and in image src like this.echo "<td><img src='".get_image($row['0'])."' width=160 height=120/></td>";. In your code check how the src path will print and it will be print like as echo string, not as a execute the file.
echo
"<table border='1'>
<tr>
<th>Id</th>
<th>Name</th>
<th>Last Name</th>
<th>Apartment</th>
<th>Parking</th>
<th>Phone1</th>
<th>Phone2</th>
<th>image</th>
</tr>";
require_once('search_r.php');
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['0'] . "</td>";
echo "<td>" . $row['1'] . "</td>";
echo "<td>" . $row['2'] . "</td>";
echo "<td>" . $row['3'] . "</td>";
echo "<td>" . $row['4'] . "</td>";
echo "<td>" . $row['5'] . "</td>";
echo "<td>" . $row['6'] . "</td>";
echo "<td><img src='".get_image($row['0'])."' width=160 height=120/></td>";
echo "</tr>";
}
echo "</table>";

Deleting rows from table on web retrieved from database

I am having issue with deleting rows from a database that I echoed onto my website, I have used tick check boxes and when multiples are selected they should be deleted. But it's just NOT HAPPENING! Nothing is getting deleted from the database! please help!
<form method="" action="tester.php">
<?php
include 'connect_to_mysql.php';
$count=0;
$count=mysql_num_rows($result);
$result = mysql_query("SELECT * FROM booking ORDER BY ID ASC");
echo "<table border='1'>
<tr>
<th>DEL</th>
<th>Name</th>
<th>Email ID</th>
<th>Phone Number</th>
<th>Collection Address</th>
<th>Collection Date</th>
<th>Collection Time</th>
<th>Make & Model</th>
<th>Message</th>
</tr>";
while($row = mysql_fetch_array($result))
{
?>
<td align="center" bgcolor="#FFFFFF"><input name="delete_these[]" type="checkbox" id="checkbox[]" value="<?php echo $row['ID']; ?>"></td>
<?php
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td>" . $row['phonenumber'] . "</td>";
echo "<td>" . $row['collectionaddress'] . "</td>";
echo "<td>" . $row['collectiondate'] . "</td>";
echo "<td>" . $row['collectiontime'] . "</td>";
echo "<td>" . $row['makemodel'] . "</td>";
echo "<td>" . $row['message'] . "</td>";
echo "</tr>";
}
echo "</table>";
?> <br>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
</tr>
<?php
// Check if delete button active, start this
if(isset($_GET['delete'])) {
for($i=0;$i<$count;$i++){
$id =(int)$_POST['delete_these'][$i];
$sql = "DELETE FROM booking WHERE ID='$id'";
print_r($_GET['delete_these[]']);
$sql = "DELETE FROM booking WHERE id IN($ids)";
echo "<br />SQL: $sql<br />";
$result = mysql_query($sql);
}
if($result){
}
}
mysql_close();
?>
</form>
First off you can just implode() all the gathered ids from the form and from there build the query.
Sample code:
<form method="POST" action="index.php">
<table>
<?php while($row = mysql_fetch_array($result)): ?>
<tr>
<td><input type="checkbox" name="delete_these[]" value="<?php echo $row['id']; ?>" /></td>
<td><?php echo $row['name']; ?></td>
</tr>
<?php endwhile; ?>
</table>
<input type="submit" name="delete" value="Delete Selected" />
</form>
<?php
$selected_values = array();
if(isset($_POST['delete'])) {
$selected_values = $_POST['delete_these'];
$ids = implode(',', $selected_values);
$query = mysql_query("DELETE FROM booking WHERE id IN($ids)");
// this becomes -> delete from booking where id in (1, 2, 3, ...)
}
?>
and while you still can, use mysqli or PDO, its free anyway

Getting null values in database during insertion

I have a drop down list box with Veg,Non Veg and Senior Veg mess options.As soon as i select one type, the corresponding Hostel Admissionnumber,Student Name and Number of Days ( Text field which has to be enterd by the User) are displayed(From registration table). I want the displayed fields to Inserted into a table called 'student_month'.When i tried to insert, i get null values inserted into student_month table.I have pasted my code below, pls have a look.Any help would be appreciated. Thanks a lot.
<?php
$q=$_GET['messtype'];
echo "<html>
<head>
<form action='testbill.php' method='GET'>
<p><select name='messtype' id='Select1'>
<option value='1'>VEG</option>
<option value='2'>NON-VEG</option>
<option value='3'>SENIOR VEG MESS</option>
<p></select> </b>
<p><input type='submit'style='width:100;height:35' name='submit' value='Display details'>
</form>";
print "<div id='txtHint' align='center'><b>List of Student Details</b></div>";
if($q=='1')
{
$a=$_POST['hosteadmissionno'];
$b=$_POST['student_name'];
$con = mysql_connect('localhost', 'root', '');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("hostel", $con);
$a1="SELECT hosteladmissionno,student_name,semester FROM registration WHERE mess_type = 'VEG' AND status_flag=1";
$a2="INSERT into student_month(hosteladmissionno,student_name) values('$a','$b')";
$result = mysql_query($a1,$con);
$final=mysql_query($a2,$con);
echo "<table border='1' width=80%>
<tr>
<th width=5%> S.No</th>
<th width=10%>H.Admin No</th>
<th width=10%>Student Name</th>
<th width=5%>No of Days</th>
</tr>";
$i=0;
while($row = mysql_fetch_array($result))
{
$i=$i+1;
echo "<form action='testbill.php' method='POST'>";
echo "<tr>";
echo "<td align=center>" .$i."</td>";
echo "<td size=10 align=center>" . $row['hosteladmissionno'] . "</td>";
echo "<td size=35 align=center>" . $row['student_name'] . "</td>";
echo "<td align=center>"."<input type='text' name='days_mess' size=2>".$row['days_mess']. "</td> ";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
}
if($q=='2')
{
$con = mysql_connect('localhost', 'root', '');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("hostel", $con);
$a1="SELECT hosteladmissionno,student_name,semester FROM registration WHERE mess_type = 'NON-VEG' AND status_flag=1";
$a2="INSERT into student_month(hosteladmissionno,student_name) values('$a','$b')";
$result = mysql_query($a1,$con);
$final=mysql_query($a2,$con);
echo "<table border='1' width=80%>
<tr>
<th width=5%> S.No</th>
<th width=10%>H.Admin No</th>
<th width=10%>Student Name</th>
<th width=5%>No of Days</th>
</tr>";
$i=0;
while($row = mysql_fetch_array($result))
{
$i=$i+1;
echo "<form action='testbill.php' method='POST'>";
echo "<tr>";
echo "<td align=center>" .$i."</td>";
echo "<td size=10 align=center>" . $row['hosteladmissionno'] . "</td>";
echo "<td size=35 align=center>" . $row['student_name'] . "</td>";
echo "<td align=center>"."<input type='text' name='days_mess' size=2>".$row['days_mess']. "</td> ";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
}
if($q=='3')
{
$con = mysql_connect('localhost', 'root', '');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("hostel", $con);
$a1="SELECT hosteladmissionno,student_name,semester FROM registration WHERE mess_type = 'SENIOR-VEG-MESS' AND status_flag=1";
$a2="INSERT into student_month(hosteladmissionno,student_name) values('$a','$b')";
$result = mysql_query($a1,$con);
$final=mysql_query($a2,$con);
echo "<table border='1' width=80%>
<tr>
<th width=5%> S.No</th>
<th width=10%>H.Admin No</th>
<th width=10%>Student Name</th>
<th width=5%>No of Days</th>
</tr>";
$i=0;
while($row = mysql_fetch_array($result))
{
$i=$i+1;
echo "<form action='testbill.php' method='POST'>";
echo "<tr>";
echo "<td align=center>" .$i."</td>";
echo "<td size=10 align=center>" . $row['hosteladmissionno'] . "</td>";
echo "<td size=35 align=center>" . $row['student_name'] . "</td>";
echo "<td align=center>"."<input type='text' name='days_mess' size=2>".$row['days_mess']. "</td> ";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
}
if($result!='')
{
echo "<form action='testbill.php'>
<input type='submit' style='width:100;height:35'name='submit' value='calculate' /> </form>";
}
print "</html>";
?>
The variables '$a' and '$b' aren't escaped from the query string:
$a2="INSERT into student_month(hosteladmissionno,student_name) values('$a','$b')";
should be:
$a2="INSERT into student_month(hosteladmissionno,student_name) values('".$a."','".$b."')";
Use your IDE's syntax highlighting to help you (see difference above). ;)

Categories