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). ;)
Related
I am new to PHP coding and don't understand where I have gone wrong, I'm trying to display data from a MySQL database onto an HTML table but doesn't register the column fields, keeps coming up with Notice: Undefined index: First, Notice: Undefined index: last etc. How do I define my column fields?
Edited: here are links to pictures of my database and the error codes I'm receiving: http://imgur.com/E5uohjr
http://imgur.com/BGDn9SQ
Here is my code:
</head>
$con = mysqli_connect('localhost', 'root', '', 'form_database') or die("Can not connect: " . mysqli_error());
$result = mysqli_query($con,"SELECT first, last, phone, class FROM form_submissions");
echo "<table border=1>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Phone Number</th>
<th>Class interested in</th>
</tr>";
while($row = mysqli_fetch_assoc($results)){
echo "<tr>";
echo "<td>" . $row['first'] . "</td>";
echo "<td>" . $row['last'] . "</td>";
echo "<td>" . $row['phone'] . "</td>";
echo "<td>" . $row['class'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
</body>
check if you do have results before displaying, them.
<?php
// Create connection
$con = mysqli_connect('localhost', 'root', '', 'form_database');
// Check connection
if (!$con) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * FROM form_submissions";
$result = mysqli_query($con, $sql);
//check if you get any results
if (mysqli_num_rows($result) > 0) {
echo "<table border=1>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Phone Number</th>
<th>Class interested in</th>
</tr>";
// output data of each row
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td>" . $row['first'] . "</td>";
echo "<td>" . $row['last'] . "</td>";
echo "<td>" . $row['phone'] . "</td>";
echo "<td>" . $row['class'] . "</td>";
echo "</tr>";
}
echo "</table>";
} else {
echo "No results found";
}
mysqli_close($con);
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>
I have a page that display all job from MySQL database.
this is the code:
<?php
$result = mysqli_query($conn,"SELECT * FROM job ORDER BY `CreatedTime` DESC");
echo "<table border='0' cellpadding='0' cellspacing='0' class='table-fill'>
<tr>
<th width='250px' position='fixed'>Job Title</th>
<th width='150px'>Company Name</th>
<th width='100px'>Location</th>
<th>Closing Date</th>
</tr>";
while($row = mysqli_fetch_array($result) ) {
echo "<tr>";
echo "<td>" . $row['positiontitle'] . "</td>";
echo "<td>" . $row['companyname'] . "</td>";
echo "<td>" . $row['location'] . "</td>";
echo "<td>" . $row['closingdate'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($conn);
?>
I want when i click on one of the job from the table then it should display the whole information in new page.
thanks
Add one more row to your table
<th>View Job</th>
Your code :
$result = mysqli_query($conn,"SELECT * FROM job ORDER BY `CreatedTime` DESC");
echo "<table border='0' cellpadding='0' cellspacing='0' class='table-fill'>
<tr>
<th width='250px' position='fixed'>Job Title</th>
<th width='150px'>Company Name</th>
<th width='100px'>Location</th>
<th>Closing Date</th>
<th>View Job</th>
</tr>";
while($row = mysqli_fetch_array($result) ) {
echo "<tr>";
echo "<td>" . $row['positiontitle'] . "</td>";
echo "<td>" . $row['companyname'] . "</td>";
echo "<td>" . $row['location'] . "</td>";
echo "<td>" . $row['closingdate'] . "</td>";
echo "<td><a href='job_details.php?id=".$row['job_id']."'>View Job</td>";
echo "</tr>";
}
And in job_details.php fetch the details against the id and display the result on that page.
jobdetails.php :
<?php
$result = mysqli_query($conn,"SELECT * FROM job WHERE job_id = '".$_GET['id']."' ORDER BY `CreatedTime` DESC");
$jobdetails = mysqli_fetch_assoc($result);
echo 'Position : '.$jobdetails['positiontitle'].'<br>';
echo 'Company Name: '.$jobdetails['companyname'].'<br>';
echo 'Location: '.$jobdetails['location'].'<br>';
echo 'Closing Date: '.$jobdetails['closingdate'].'<br>';
?>
you can also use this id= instead of id=".$row['job_id']."
I am stuck in one of my application module. I have to set multiple delete options in my application.
I have used the below code. The designing is perfectly fine. All what I want to add multiple delete options in it.
Below is the code which I have tried out:
<html>
<head>
<title>Strad Hosting Limited -Web Hosting</title>
<script language="javascript">
function validate()
{
var chks = document.getElementsByName('checkbox[]');
var hasChecked = false;
for (var i = 0; i < chks.length; i++)
{
if (chks[i].checked)
{
hasChecked = true;
break;
}
}
if (hasChecked == false)
{
alert("Please select at least one.");
return false;
}
return true;
}
</script>
</head>
<body >
<?php
$con=mysqli_connect("localhost","stradsol","D#v,b5TnQ!D!","stradsol_sales");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM Persons");
echo "<form name='form1' method='post' action='' onSubmit='return validate();'>";
echo "<table border='1' style='
background-color: white;'>
<tr>
<th></th>
<th>id</th>
<th style='padding-left: 11px;'>Lead Generated Date </th>
<th>name</th>
<th>email</th>
<th>contacts</th>
<th>requirement</th>
<th style='display:none;'>Company name</th>
<th style='display:none;'>Address</th>
<th>Next Follow-Up date</th>
<th>Final_Details</th>
<th style='display:none;'>Status</th>
<th style='padding-left: 12px; display:none;'>Lead Closed Date</th>
<th>EDIT</th>
<th>Follow-up History</th>
<th>Add Follow-up</th>
<th>Delete Record</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td><input name='checkbox[]' type='checkbox' id='checkbox[]' value='".$rows['id']."'></td>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td><a href='config_info.php?id=" . $row['id'] . "'>".$row['name']."</a></td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td>" . $row['contacts'] . "</td>";
echo "<td>" . $row['requirement'] . "</td>";
echo "<td style='display:none;'>" . $row['company_name'] . "</td>";
echo "<td style='display:none;'>" . $row['address'] . "</td>";
echo "<td>" . $row['startdate'] . "</td>";
echo "<td>" . $row['final_details'] . "</td>";
echo "<td style='display:none;'>" . $row['status'] . "</td>";
echo "<td style='display:none;'>" . $row['lead_close'] . "</td>";
echo "<td><a href='edit_eg1.php?id=" . $row['id'] . "'>Edit</a></td>";
echo "<td><a href='Follow_history.php?id=" . $row['id'] . "'>Follow_history</a></td>";
echo "<td><a href='add_follow.php?id=" . $row['id'] . "'>Followup</a></td>";
echo "<td><a href='delete.php?id=" . $row['id'] . "'>Delete</a></td>";
echo "</tr>";
}
echo "<tr><td><input name='delete' type='submit' id='delete' value='Delete'></td></tr>";
$count=mysqli_num_rows($result);
if(isset($_POST['delete'])){
for($i=0;$i<count($_POST['checkbox']);$i++){
$del_id=$_POST['checkbox'][$i];
$sql = "DELETE FROM Persons WHERE id='$del_id'";
echo $sql;
$result2 = mysqli_query($con,$sql);
}
// if successful redirect to delete_multiple.php
if($result2)
{
echo "<meta http-equiv=\"refresh\" content=\"0;URL=edit_table_del.php\">";
}
}
mysqli_close($con);
echo "</table>";
echo "</form>";
?>
<br><br>
[ Back To Home ]
</body>
</html>
I am stuck, the query is not working I think. I don't know what I am missing.
Try replacing
$sql = "DELETE FROM Persons WHERE id='$del_id'";
With
$sql = "DELETE FROM Persons WHERE id=$del_id";
I'm assuming the ID is a number so the single quotes aren't needed.
I am trying to retrieve records from db and put them into a table. I want to have an update button which can do some processing. Here is the code. It outputs properly. However the update button (image) doesn't work. Please help
<?php
echo "</br>";
echo "<table border='1'>
<tr>
<th>Order ID</th>
<th>Customer Number</th>
<th>Order Items</th>
<th>Transaction Time</th>
<th>Amount</th>
</tr>";
$con = mysql_connect('localhost', 'root', '');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("kaka", $con);
$sql="SELECT * FROM orders WHERE status = 'NRDY'";
$result2 = mysql_query($sql);
while($row1 = mysql_fetch_array($result2))
{
echo "<div class=\"update\"><form method='post' action=\"test.php\">\n";
echo "<tr>";
echo "<td><input type='hidden' name='order_id' value='".$row1['order_id']."'>" .$row1['order_id']. "</td>";
echo "<td><input type='hidden' name='cust_num' value='".$row1['cust_num']."'>" .$row1['cust_num']. "</td>";
echo "<td><input type='hidden' name='items' value='".$row1['items']."'>" .$row1['items']. "</td>";
echo "<td><input type='hidden' name='order_time' value='".$row1['order_time']."'>" .$row1['order_time']. "</td>";
echo "<td><input type='hidden' name='order_id' value='".$row1['amount']."'>" .$row1['amount']. "</td>";
//echo "<td>" . $row1['order_id'] . "</td>";
//echo "<td>" . $row1['cust_num'] . "</td>";
//echo "<td>" . $row1['items'] . "</td>";
//echo "<td>" . $row1['order_time'] . "</td>";
//echo "<td>" . $row1['amount'] . "</td>";
echo "<td>" . "<input type=\"image\" src=\"images/update.png\" alt=\"Update Row\" class=\"update\" title=\"Update Row\">\n" . "</td>";
echo "</tr>";
echo "</form></div>";
}
echo "</table>";
?>
Here is test.php
<?php
echo $_POST['order_id'];
?>
Use a validator. You aren't allowed to wrap divs or forms around table rows. Some browsers recover from this error by moving the div/form so it is outside the table (leaving the content behind). Your inputs are therefore not inside a form so shouldn't be expected to do anything.