I'm a beginner in PHP so may be asking dumb questions. I researched on my question for couple of days prior to bugging you guys. I've two scenarios.
a) MySQL database has 3 fields. description, weblink and header. "weblink" field stores weblinks in the database. Using PHP I'm trying to show weblinks on my webpage upon the user clicking the field "header".Its not working- webpage comes blank with the code below.
while($row = mysqli_fetch_array($query)){
echo $row['description'];
echo "<br>";
echo "<br>";
echo "<a href = $row['weblink'] > $row['Header']</a>";
echo "<br>";
echo "<hr>";
}
b) Scenario 2: Same example from above but trying to show the links from my database as a button.
while($row = mysqli_fetch_array($query)){
echo $row['description'];
echo "<br>";
echo "<br>";
echo $row['weblink'];
echo "<br>";
<a href = "$row['weblink']"<button>click me</button></a>;
echo "<hr>";
}
Please help.
Consider the humble printf() instead and apply proper output escaping:
while ($row = mysqli_fetch_array($query)) {
printf('%s<br><br>%s<br><hr>',
htmlspecialchars($row['description'], ENT_QUOTES, 'UTF-8'),
htmlspecialchars($row['weblink'], ENT_QUOTES, 'UTF-8'),
htmlspecialchars($row['Header'], ENT_QUOTES, 'UTF-8')
);
}
for Scenario 1, You should do something like this (note the braces):
echo "<a href='{$row['weblink']}'>{$row['Header']}</a>";
For scenario 2, you should do something like this (again note braces):
echo "<button>click me</button>";
Remember that Braces work on variables that are within double quotes, and not apostrophes.
Check the below code:
while($row = mysqli_fetch_array($query)){
echo $row['description'];
echo "<br><br>";
echo $row['weblink'];
echo "<br>";
echo 'click me';
echo "<hr>";
}
The brackets in the echo are treated as a string not as an array key So use { } these brackets.Also your quotes are not proper. Use the code below
while($row = mysqli_fetch_array($query)){
echo $row['description'];
echo "<br>";
echo "<br>";
echo "<a href =' {$row['weblink']}' > {$row['Header']}</a>";
echo "<br>";
echo "<hr>";}
Scenario 2
while($row = mysqli_fetch_array($query)){
echo $row['description'];
echo "<br>";
echo "<br>";
echo $row['weblink'];
echo "<br>";
echo '<button>click me</button>';
echo "<hr>";}
Hope this helps you
Related
I have below code which creates drop-down from php. I want to achieve 2 things here.
1. I want to set one of the option to be set default. It may be hard coded or selected from query.
2. When hit button, it should retain selected option. I could retrieve selected option using session data with this: echo $print_version1[array_keys($print_version1)[0]];
Drop-down code:
$result = $conn->query("SELECT DISTINCT nx_version FROM workflow1 ORDER BY id DESC");
echo "<form action='http://localhost/w_5aug/process.php' method='get'>";
echo "<html>";
echo "<body>";
echo "<p></p>";
echo "<center>";
echo "<strong> Select Base Verison To Compare With : </strong>";
echo "<select name='nx_version' id='nx_version'>";
while ($row = $result->fetch_assoc()) {
$nx_version = $row['nx_version'];
echo '<option>'.$nx_version.'</option>';
}
echo "</select>";
echo " <button type='submit'><b>Add Base Verison</b></button>";
echo "</center>";
echo "</body>";
echo "</html>";
echo "<p></p>";
$array_select = $_SESSION['data'];
print_r($array_select);
echo "<form>";
i assume that the option that should be selected is$print_version1[array_keys($print_version1)[0]
while ($row = $result->fetch_assoc()) {
$nx_version = $row['nx_version'];
if($_SESSION["id"]) {
if($nx_version == "the hardcode value you want to be selected"){
echo '<option selected="selected">'.$nx_version.'</option>';
}else{
echo '<option>'.$nx_version.'</option>';
}
}else{
if($print_version1[array_keys($print_version1)[0]] == $nx_version){
echo '<option selected="selected">'.$nx_version.'</option>';
}else{
echo '<option>'.$nx_version.'</option>';
}
}
}
came across a problem for my coursework when trying to place selected items from my database into a table, here is my code:
echo '<br>';
$describeQuery='Select Distinct current_location From Current_Location';
$results = sqlsrv_query($conn, $describeQuery);
echo '<br>';
echo '<br>';
echo "<table border='1', width='15%'><tr><th>Locations</th></tr>";"
while($row = sqlsrv_fetch_array($results, SQLSRV_FETCH_ASSOC))
{
echo "<tr><td>" "<option value=\"Location1\">" . $row['current_location'] . </option>" "</td></tr>";
}
echo "</table>";
Can anyone see where I am going wrong? The error mentions that I have no ',' or ';' tags, therefore it never ends, however I cannot find this error through messing with the program a bit.
Sorry for any formatting issues, still adjusting to the website.
There is an extra double-quotes at the end of the 6th line and then you should modify the while loop's echo like this:
echo '<br>';
$describeQuery='Select Distinct current_location From Current_Location';
$results = sqlsrv_query($conn, $describeQuery);
echo '<br>';
echo '<br>';
echo "<table border='1', width='15%'><tr><th>Locations</th></tr>";
while($row = sqlsrv_fetch_array($results, SQLSRV_FETCH_ASSOC))
{
echo "<tr><td><option value='Location1'>" . $row['current_location'] . "</option></td></tr>";
}
echo "</table>";
Hey all I am stuck on displaying a table on my webapge using php. Everytime I try something new, I seem to get a new error. Im really stuck here I am trying to display a table Client which has Columns ID,Name,Phone,Email. I can't seem to get the data into the table. Can anyone help using mysqli?
<?php
include 'connect.php';
include 'form.php';
echo "<table border=1>";
echo "<tr><th>Id</th><th>Name</th><th>Phone</th><th>Email</th></tr>";
if($result = $mysqli_query("SELECT * from Client")){
while($row = $result->fetch_array(MYSQLI_ASSOC)) {
echo "<tr><td>";
echo $row["ID"];
echo "</td><td>";
echo $row["name"];
echo "</td><td>";
echo $row["email"];
echo "</td><td><a href=delclient.php?id=";
echo $row["id"];
echo ">DEL</a> ";
echo "<a href=addclient.php?id=";
echo $row["id"];
echo ">EDIT</a>";
echo "</td></tr>";
}
echo "</table>";
}
?>
Instead of $mysqli_query, it should be mysqli_query:
if($result = mysqli_query("SELECT * FROM Client")) {
As Tristan points out, your call $mysqli_query() is wrong; it should be either
$mysqli->query("SELECT * from Client")
or
mysqli_query($conn, "SELECT * from Client")
where $conn is the database connection created in connect.php. Given your use of object syntax to reference the result fields, I suspect the first of the above to be the correct one.
I am currently working on a PHP and SQLite application. It does have HTML for the webpages being created. I am currently using PDO to connect the database to my online server. When I get to the webpage that allows me to type in what I am searching for then it will display what I have found in the echo statements below. I want to be able to have just the item name, that acts as a hyperlink; when it is clicked on it will go to another webpage (I believe) that will display the item's name, amount, and a short description. Is there a way to use PHP for this action or should I go with the HTML tagging?
if($_POST && isset($_POST['search'])) {
echo "<br>\n";
$query = $mysql->prepare('SELECT * FROM Items WHERE Name = :partname');
$subst = array ('partname' => $_POST['search']);
$query->execute($subst);
echo "<TABLE>";
echo "<tr>";
echo "<td>Name</td>";
echo "<td>Amount</td>";
echo "<td>Detail</td>";
echo "</tr>";
while ($row = $query->fetch()) {
//print_r($row);
echo "<tr>";
echo "<td>$row[Name]</td>";
echo "<td>$row[Amount]</td>";
echo "<td>$row[Detail]</td>";
echo "</tr>";
}
echo "</TABLE>";
} else echo "Item searched for was not found.";
from what i understand
if($_POST && isset($_POST['search'])) {
echo "<br>\n";
$query = $mysql->prepare('SELECT * FROM Items WHERE Name = :partname');
$subst = array ('partname' => $_POST['search']);
$query->execute($subst);
echo "<TABLE>";
echo "<tr>";
echo "<td>Name</td>";
echo "</tr>";
while ($row = $query->fetch()) {
//print_r($row);
$name = $row['Name'];
echo "<tr>";
echo "<td><a href='details.php?name={$name}'>{$name}</a></td>";
echo "</tr>";
}
echo "</TABLE>";
} else echo "Item searched for was not found.";
To use Associative Arrays within double quotes, you need to use curly braces:
echo "<td>{$row['Name']}</td><td>{$row['Amount']}</td><td>{$row['Detail']}</td>";
I wrote the php code below to fetch data from mysql to html table, but no matter how I tried, it always show Internal Server Error, could there anyone can help me to take a look at the code?
And I tested the query in mysql, it works, I don't know why when I add it to php, it just crashed.
$StartDate = date( 'Y-m-d' strtotime($_POST['StartDate']));
$EndDate = date( 'Y-m-d' strtotime($_POST['EndDate']));
$link = mysql_connect('****', '****', '****');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_select_db(****);
// Search by date query
$SearchByDate = "
SELECT Order_Info.OrderID, Order_Info.Date, Client_Info.Manager, Material_Info.MateRefNum, Order_Info.CustomMateName,
Order_Info.Quantity, Order_Info.Weight, Order_Info.TechRequire, Order_Info.UniPrice, Order_Info.TotPrice, Order_Info.OtherNote
FROM Order_Info, Client_Info, Material_Info
WHERE Order_Info.MateID = Material_Info.MateID
AND Order_Info.ClientID = Client_Info.ClientID
AND Order_Info.Date > '$StartDate'
AND Order_Info.Date < '$EndDate'
ORDER BY Order_Info.Date DESC;
";
$query = mysql_query($SearchByDate,$link);
echo "<div><table>";
echo "<tr><td>OrderID</td><td>Date</td><td>Client</td><td>Material Number</td><td>Material Name</td><td>Quantity</td><td>Weight</td><td>Technical Requirement</td><td>Unit Price</td><td>Total Price</td><td>Notes</td></tr>";
while($row = mysql_fetch_array($query)){
echo "<tr>";
echo "<td>".row['Order_Info.OrderID']."</td>";
echo "<td>".row['Order_Info.Date']."</td>";
echo "<td>".row['Client_Info.Manager']."</td>";
echo "<td>".row['Material_Info.MateRefNum']."</td>";
echo "<td>".row['Order_info.CustomMateName']."</td>";
echo "<td>".row['Order_Info.Quantity']."</td>";
echo "<td>".row['Order_Info.Weight']."</td>";
echo "<td>".row['Order_Info.TechRequire']."</td>";
echo "<td>".row['Order_Info.UniPrice']."</td>";
echo "<td>".row['Order_Info.TotPrice']."</td>";
echo "<td>".row['Order_Info.OtherNote']."</td>";
echo "</tr>";
}
echo "</table></div>";
?>
your variable must be using symbol '$', but you did not
you can use var_dump($row) to help you to know how the structure of the array
the code:
while($row = mysql_fetch_array($query)){
echo "<tr>";
echo "<td>".$row['OrderID']."</td>";
echo "<td>".$row['Date']."</td>";
echo "<td>".$row['Manager']."</td>";
echo "<td>".$row['MateRefNum']."</td>";
echo "<td>".$row['CustomMateName']."</td>";
echo "<td>".$row['Quantity']."</td>";
echo "<td>".$row['Weight']."</td>";
echo "<td>".$row['TechRequire']."</td>";
echo "<td>".$row['UniPrice']."</td>";
echo "<td>".$row['TotPrice']."</td>";
echo "<td>".$row['OtherNote']."</td>";
echo "</tr>";
}