This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 6 years ago.
Trying to get values from mysql into <select> tag.
I have used while loop & getting data from 2 different tables.
I have tried the given code but not able to get the data.
Please Help!
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
// Connect to server and select databse.
$conn=mysqli_connect($host,$username,$password) or die("cannot connect");
mysqli_select_db($conn,$db_name);
$result = mysqli_query($conn,"SELECT * from dummy");
$uresult=mysqli_query($conn,"SELECT name from user where role='Support'");
$uname=array();
while ($rows=mysqli_fetch_array($uresult))
{
$uname[]=$rows['name'];
}
echo "<table border='1' width='100%' >
<tr><th colspan='7' ><h2 align='center'>Details</h2></th></tr>
<tr bgcolor='grey'>
<th width='10%' class='text-center'>Emp No.</th>
<th width='15%' class='text-center'>Name</th>
<th width='30%' class='text-center'>Task</th>
<th width='20%' class='text-center'>Assigned To</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td align='center'>" . $row['emp_no']. "</td>";
echo "<td align='center'>" . $row['emp_name']. "</td>";
echo "<td align='center'>" . $row['task'] . "</td>";
echo "<td align='center'> <select name='select1'> <?php while ($rows=mysqli_fetch_array($uresult)) { ?><option><?php echo $rows['name'];?> </option><?php } ?> </select> </td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($conn);
?>
Error :
Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING)
Error is with below line
you have php code inside the string
echo "<td align='center'> <select name='select1'> <?php while ($rows=mysqli_fetch_array($uresult)) { ?><option><?php echo $rows['name'];?> </option><?php } ?> </select> </td>";
No need to fetch it over again. You've already done that here:
$uname = array();
while ($rows = mysqli_fetch_array($uresult)) {
$uname[] = $rows['name'];
}
So inside the other while block, just use that names that already gathered:
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td align='center'>" . $row['emp_no']. "</td>";
echo "<td align='center'>" . $row['emp_name']. "</td>";
echo "<td align='center'>" . $row['task'] . "</td>";
echo "
<td align='center'>
<select name='select1'>
";
foreach($uname as $names) {
echo '<option>' . $names . '</option>';
}
echo "
</select>
</td>";
echo "</tr>";
}
Or just create an HTML string of <options> with the $uname, then just echo it inside the while:
$uname = '';
while ($rows = mysqli_fetch_array($uresult)) {
$uname .= '<option>' . $rows['name'] . '</option>';
}
Then on the while block:
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td align='center'>" . $row['emp_no']. "</td>";
echo "<td align='center'>" . $row['emp_name']. "</td>";
echo "<td align='center'>" . $row['task'] . "</td>";
echo "
<td align='center'>
<select name='select1'>
";
echo $uname; // collection of HTML string options
echo "
</select>
</td>";
echo "</tr>";
}
Try like this, embedding php in html
while($row = mysqli_fetch_array($result))
{
?>
<tr>
<td align='center'><?php echo $row['emp_no'] ?></td>
<td align='center'><?php echo $row['emp_name'] ?></td>
<td align='center'><?php echo $row['task'] ?></td>
<td align='center'> <select name='select1'>
<?php while ($rows=mysqli_fetch_array($uresult)) { ?>
<option><?php echo $rows['name'];?> </option><?php } ?>
</select> </td>
</tr>";
<?php
}
Related
here the problem is i need to view the whole row data in view details page when the button is clicked but when i click the last row data is sent through session variable and last row is displayed but i need particular row to display fully when the name is selected of the employee. please help me out in this .
thank you
index.php
<?php
include('connection.php');
$sql = "SELECT empname,salary,contact_no,department,empdesg FROM add_emp ORDER BY empname ASC ";
$result = $con->query($sql);
if ($result->num_rows > 0)
{
// output data of each row
?>
<center>
<table cellpadding="20px" cellspacing="40px;" border="2px" align="center" width="device">
<tr>
<th>Name</th>
<th>Department</th>
<th>Designation</th>
<th> </th>
<?php
while($row = $result->fetch_assoc())
{
$name='';
$name=$row["empname"];
$department=$row["department"];
$designation=$row["empdesg"];
?>
</tr>
<td>
<?php echo $name?>
</td>
<td>
<?php echo $department?>
</td>
<td>
<?php echo $designation?>
</td>
<td><a href="viewdetails.php">
<button>Details</button>
</a>
<?php
$_SESSION['name']= $name;
?>
</td>
<?php
}
}
else {
echo "0 results";
}
?>
viewdetails.php
<?php
session_start();
include('connection.php');
$sql="select * from add_emp where empname='$_SESSION[name]'";
$result = mysqli_query($con, $sql); // First parameter is just return of "mysqli_connect()" function
echo "<br>";
echo " <table cellpadding='5px' cellspacing='10px' border='1px' align='center'>";
while ($row = mysqli_fetch_assoc($result))
{ // Important line !!! Check summary get row on array ..
echo "<tr>";
echo "<th>" ."Name". "</th>";
echo "<td>" .$row['empname']. "</td>";
echo "</tr>";
echo "<tr>";
echo "<th>" ."DOB". "</th>";
echo "<td>" .$row['eage']. "</td>";
echo "</tr>";
echo "<tr>";
echo "<th>" ."Adhaar Number". "</th>";
echo "<td>" .$row['adhaar']. "</td>";
echo "</tr>";
echo "<tr>";
echo "<th>" ."Address". "</th>";
echo "<td>" .$row['address']. "</td>";
echo "</tr>";
echo "<tr>";
echo "<th>" ."Salary". "</th>";
echo "<td>" .$row['salary']. "</td>";
echo "</tr>";
echo "<tr>";
echo "<th>" ."Phone Number". "</th>";
echo "<td>" .$row['contact_no']. "</td>";
echo "</tr>";
echo "<tr>";
echo "<th>" ."Department". "</th>";
echo "<td>" .$row['department']. "</td>";
echo "</tr>";
echo "<tr>";
echo "<th>" ."Designation". "</th>";
echo "<td>" .$row['empdesg']. "</td>";
echo "</tr>";
echo "<tr>";
}
echo "</table>";
?>
I'm trying to create one table that will loop through all the records from my database but it seems to be create individual tables for each record instead.
$query = 'select * from images';
$result = mysqli_query($connection,$query);
$row_count = mysqli_num_rows($result);
for($i=0; $i<$row_count; $i++){
$row[] = mysqli_fetch_array($result);
}
echo '<header><h1 class="gallerytitle">Photo Gallery</h1></header>';
foreach($row as $next) {
{
echo "<table border='1'>
<tr>
<th>Imageid</th>
<th>Image name</th>
<th>Description</th>
<th>Image</th>
<th>Caption</th>
</tr>";
echo "<tr>";
echo "<td>" . $next['imageid'] . "</td>";
echo "<td>" . $next['imagename'] . "</td>";
echo "<td>" . $next['description'] . "</td>";
echo "<td><img src='".$next['image']."' width='20%' height='auto'></td>";
echo "<td>" . $next['caption'] . "</td>";
echo "<td> <a class='readmore' href='delete_confirm.php?imageid={$next['imageid']}'>Delete</a></td>";
echo "<td> <a class='readmore' href='update_form.php?imageid={$next['imageid']}'>Update</a></td>";
echo "</tr>";
}
echo "</table>";
echo '</section>';
}
Ideally, you would loop around the tr
echo "<table border='1'>
<tr>
<th>Imageid</th>
<th>Image name</th>
<th>Description</th>
<th>Image</th>
<th>Caption</th>
</tr>";
foreach($row as $next) {
{
echo "<tr>";
echo "<td>" . $next['imageid'] . "</td>";
echo "<td>" . $next['imagename'] . "</td>";
echo "<td>" . $next['description'] . "</td>";
// all the other columns
echo "</tr>";
}
echo "</table>";
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>";
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.
t
I have data shown about a band that matches a id selected, im then using the band_id and getting it to use it else where.
I would like to do the same with Name field but I am having a few problems, its just getting the word array in to the var $name.
code
<?php
require 'core/init.php';
$Band_id = $_GET['id'];
$name = ['Name']; // trying to get the band name
$result = mysql_query("SELECT * FROM bands WHERE Band_id = $Band_id");
echo "<table border = '1'>
<tr>
<th>Band Name</th>
<th>Venue</th>
<th>Category</th>
<th>Stock</th>
<th>Buy Ticket</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr><form name=\"myform\" action=\" order.php\" method=\"post\">";
echo " <input name=\"band\" type=\"hidden\" value=\"". $Band_id."\" >";
echo " <input name=\"bandn\" type=\"hidden\" value=\"". $name."\" >";
echo "<td>" .$row['Name']. "</td>";
echo "<td>" .$row['Venue']. "</td>";
echo "<td>" .$row['Category']. "</td>";
echo "<td>" .$row['Stock']. "</td>";
echo "<td><button>Buy Ticket</button></td>";
echo "</tr> </form>";
}
echo "</table>";
?>
$name = ['Name'] wont work above the query, and generally wont work at all as its invalid syntax for what you're trying to do. Where you have used $name use $row['Name']