I have a question about an edit button and a MySQLi variable.
This is what i have:
Now i want the "edit" tekst to be redirected to a page, and this page is an overview of the reparation. So every "edit" needs to point to different details.
But what kind of HREF do i need? This is my code from the table:
$query = "SELECT reparatieid, klantid, model, merk, framenummer, verzekerd, garantie, gereed from reparatie ";
$result = $link->query($query);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
print("<table border= 1px >");
print("<tr>");
print("<td class=tdrp>" . $row ["reparatieid"] . "</td>" . " ");
print("<td class=TDID>" . $row ["klantid"] . "</td>" . " ");
print("<td class=tdmodel>" . $row ["model"] . "</td>" . " ");
print("<td class=tdmerk>" . $row ["merk"] . "</td>" . " ");
print("<td class=tdfrm>" . $row ["framenummer"] . "</td>" . " ");
print("<td class=tdverz>" . $row ["verzekerd"] . "</td>" . " ");
print("<td class=tdgara>" . $row ["garantie"] . "</td>" . " ");
print("<td class=tdgereed>" . $row ["gereed"] . "</td>" . " ");
echo '<td class=tdbewerk>Edit </td>';
print("</tr>");
So, the last echo is the "edit" button, and this line of href:
/reparatiebewerk.php?reparatieid=$_GET["reparatieid"]
But it does not work! Do you know what the problem is? When i do this in my browser, it points to the good page, so i only need to have the href voor the EDIT text,
http://127.0.0.1/reparatiebewerk.php?reparatieid=547c97bfe3a2c
Thanks!!
Use double quotation "xxx" when you need to detect $variable.
Use single quotation 'xxx' will output $variable as normal text.
In your case,
echo '<td class=tdbewerk>Edit </td>';
should be
echo "<td class=tdbewerk><a href='/reparatiebewerk.php?reparatieid=$_GET[reparatieid]'>Edit </a> </td>";
This should work!
At first you can try to replace your echo with:
echo '<td class=tdbewerk>Edit </td>';
And in your reparatiebewerk.php file read value from url with:
<?php
$id = $_GET["reparatieid"];
// Do whatever you need with your id
This should be the correct statement :
echo "<td class=tdbewerk><a href='/reparatiebewerk.php?reparatieid=" . $row['reparatieid'] . "'>Edit </a> </td>";
Related
I have read a lot of submissions, I haven't been able to find an answer to my exact question.
I am filling out an HTML form that is sent into mysql database - I am good there.
The results are displayed using php in a html table. I would like the website URL to show as a hyperlink. Everything I do results in an error.
Does anyone know how I can update my code to allow for the website url to show as a hyperlink?
[$dbname = "seller";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT website, url, price, name, email FROM listing";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table><tr><th>Website</th><th>URL</th><th>Price</th><th>Contact Name</th><th>Email</th></tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["website"]. "</td><td>" . $row["url"]. " </td><td>" . $row["price"]. "</td><td>" . $row["name"]. "</td><td> " . $row["email"]. "</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
$conn->close();
?>]
Thanks
Have you tried this...
echo "<tr><td></td><td><a href='" . $row['url']. "'>" . $row['website']. "</a> </td><td>" . $row["price"]. "</td><td>" . $row["name"]. "</td><td> " . $row["email"]. "</td></tr>";
You could also just change 'Link to website' to " . $row["website"]. " for it to display the link
If you use an a tag it should work.
URL
echo "<tr><td><a href=\"{$row['url']}>{$row['website']}</a></td>...etc
You should also consider closing the tags to get a cleaner code:
<?php if ($condition) { ?>
<div>HTML code!</a>
<?php } ?>
Add an anchor tag <a href=""> inside the table data tag <td> inside your while loop.
Change this line:
echo "<tr><td>".$row["website"]."</td><td>".$row["url"]." </td><td>".$row["price"]."</td><td>".$row["name"]."</td><td> ".$row["email"]."</td></tr>";
Into this:
echo "<tr><td><a href='".$row["website"]."'>".$row["website"]."</a></td><td>".$row["url"]." </td><td>".$row["price"]."</td><td>".$row["name"]."</td><td> ".$row["email"]."</td></tr>";
That will be great if you can show me the error otherwise
Try this:
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["website"]. "</td><td><a href='".$row["url"]."'>Link</a> </td><td>" . $row["price"]. "</td><td>" . $row["name"]. "</td><td> " . $row["email"]. "</td></tr>";
}
use this
echo '<tr><td>' . $row["website"]. '</td><td>'; echo $row["name"]; echo' </td><td>' . $row["price"]. '</td><td>' . '</td><td> ' . $row["email"]. '</td></tr>';
instead of
echo "<tr><td>" . $row["website"]. "</td><td>" . $row["url"]. " </td><td>" . $row["price"]. "</td><td>" . $row["name"]. "</td><td> " . $row["email"]. "</td></tr>";
use <a> tag, please see this link.
'' . $row['url'] . ''
I'm trying to display images from a SQL Database (using an URL in the DB) in a table.
I'm still pretty new to this type of implementation so I've been giving it my best shot and trying to do research but haven't found much on using URLs (opposed to using BLOBs or directly adding images to the Database). I definitely WANT to keep my images on a file system. Currently my code only displays the URL (obviously) but I have tried a few things..
I have tried:
-Passing $row["card_image"] to a PHP variable $image and using $image->load()
-Using "<img src="$row["card_image"]">" (I imagined since I was getting a link output still this could work...)
-I have also tried a few other solutions that I've found on Stack Overflow but I believe these have mostly been for use with BLOB data types and so they also failed to produce the output I desire (I.E.
"<img src="data:image/jpeg;base64,'.base64_encode($image->load())'" />"
Here is my current code:
SQL (I have condensed the fields to save space)
CREATE TABLE cards (
card_image VARCHAR(999)
);
insert into cards (card_image)
values ('http://localhost/dbztc/wp-content/uploads/2016/04/1.jpg');
PHP
// Create connection
$conn = new mysqli($servername, $username, $password, $db);
if(!$conn)
{
die("connection failed");
}
//Table
echo "
<table cellpadding=\"0\" cellspacing=\"2\" border=\"0\" width=\"100%\">
<tr bgcolor=\"#666666\">
<td colspan=\"5\" align=\"center\"><b><font color=\"#FFFFFF\">" . $table[0] . "</font></td>
</tr>
<tr>
<td>Image</td>
<td>Card Number</td>
<td>Card Rarity</td>
<td>Card Name</td>
<td>Card Type</td>
<td>Card Style</td>
<td>Card Text</td>
</tr>";
$sqlquery = "SELECT card_image, card_number, card_rarity, card_title, card_type, card_style, card_text FROM cards";
$result = mysqli_query($conn, $sqlquery);
//$image = $row["card_image"];
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "<td>" . $row["card_image"] . "</td>" . "<td>" . $row["card_number"]. "</td>" . "<td>" . $row["card_rarity"] . "</td>" . "<td>" . $row["card_title"] . "<td>" . $row["card_type"] . "</td>" . "<td>" . $row["card_style"] . "</td>" . "<td>" . $row["card_text"] . "</td></tr>";
}
} else {
echo "0 results";
}
echo "</table>";
mysqli_close($conn);
Instead of:
<td>" . $row["card_image"] . "</td>
Try:
$image = $row["card_image"]; // the image url
<td><img src='". $image . "'></td>
For anyone who stumbles upon this and may be having this same problem:
For starters, I took the entire URL out of the database entry and replaced it with the Filename only.
Afterwards, I edited my code like this:
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "<td>" . "<img src ='../images/" . $row["card_image"] . "'>" . "</td>" . "<td>" . $row["card_number"]. "</td>" . "<td>" . $row["card_rarity"] . "</td>" . "<td>" . $row["card_title"] . "<td>" . $row["card_type"] . "</td>" . "<td>" . $row["card_style"] . "</td>" . "<td>" . $row["card_text"] . "</td></tr>";
echo "<br />";
It now displays perfectly. Just as a note, this was done on a live server opposed to local so that could maybe also have an impact? Not sure, but hopefully this can help someone!
This is my delete page :
<?php
require('includes/config.php');
$id = $_GET['ID'];
$pdoConnect = new PDO($db);
$query='DELETE * FROM studentraspored WHERE ID = "' . $id . '" ';
$pdoResult = $db->prepare($query);
$pdoExec = $pdoResult->execute($query);
header('location:index.php');
?>
This is generated table in my “memberpage.php”:
if (count($rows)){
foreach ($rows as $row) {
$_SESSION['row'] = $rows;
$id = floatval($row['ID']);
echo "<tr>" .
'<form action="delete_raspored.php" method="post">'.
"<td>" . $row["ID"] . "</td>" .
"<td>" . $row["den"] . "</td>" .
"<td>" . $row["chas"] . "</td>" .
"<td>" . $row["predmet"] . "</td>" .
"<td>" . $row["profesor"] . "</td>" .
"<td>" . $row["prostorija"] . "</td>" .
"<td>" . $row["tip"] . "</td>" .
'<td><input type="submit" id="' . $id . '" value="Delete" ></td>'.
"</form>".
"</tr>"
This not working properly. I don't understand why maybe something i missed with floatval
Start by trying this:
<?php
require('includes/config.php');
$id = $_GET['ID'];
$query='DELETE FROM studentraspored WHERE ID = ?';
$pdoResult = $db->prepare($query);
$pdoResult->execute(array($id));
header('location:index.php');
exit();
Note the placeholder in place of the actual value, this will prevent SQL injections. The value is passed in in the execute, or you could bind it (http://php.net/manual/en/pdostatement.bindparam.php). http://php.net/manual/en/pdo.prepared-statements.php
The delete syntax was also off, delete deletes a whole row not specific columns, http://dev.mysql.com/doc/refman/5.7/en/delete.html.
In your form I also don't see an element named ID so that could be another issue and your form is submitting via POST, not GET.
I would like to know how I can use the GET-method in my .php file: forumdocument.php to get the ID of the href that is clicked in table.php
I hope this is the right way to begin it:
'<a class="formtitellink" href="forumdocument.php" id="$row['ID']">' . $row['Titel'] . '</a>' . "</td>";
table.php:
$query = "Select *
from forum";
$resultaat = mysql_query($query, $connectie);
echo "<table border='1'>
<tr>
<th>ID</th>
<th>Tijd</th>
<th>Gebruikersnaam</th>
<th>Titel</th>
</tr>";
while($row = mysql_fetch_array($resultaat))
{
$_SESSION['row'] = $row;
echo "<tr>";
echo "<td>" . $row['Tijd'] . "</td>";
echo "<td>" . $row['Gebruikersnaam'] . "</td>";
echo "<td>" . '<a class="formtitellink" href="forumdocument.php" id="$row['ID']">' . $row['Titel'] . '</a>' . "</td>";
echo "</tr>";
}
echo "</table>";
A GET method is used for name value pairs in a url.
Example: www.test.com?var1=var1&var2=var2
Here if we use $_GET['var1'] the expected value would be var1.
Just change your links to reflect the desired variable
href="forumdocument.php?rowid=".$row['ID']."
Then you can use retrieve the id using something like this on forumdocument.php
$rowid = $_GET['rowid'];
Change this part
a class="formtitellink" href="forumdocument.php" id="$row['ID']"
a class="formtitellink" href="forumdocument.php"?id="'.$row['ID'].'"
I have a form that posts to a PHP page. This is a snippet of the page. The goal is to read out a SQL table and then you click on the text values and they are editable. You then edit them and submit and it updates the SQL table. I basically need a way to strip the letter from the beginning of the id. but the letter is important because it defines in what column the new information goes into. Also if there is a different language or method to do this, I am open to suggestions.
<script type="text/javascript">
function exchange(id){
var ie=document.all&&!window.opera? document.all : 0
var frmObj=ie? ie[id] : document.getElementById(id)
var toObj=ie? ie[id+'b'] : document.getElementById(id+'b')
toObj.style.width=frmObj.offsetWidth+7+'px'
frmObj.style.display='none';
toObj.style.display='inline';
toObj.value=frmObj.innerHTML
}
</script>
<?php
$result = mysqli_query($con,"SELECT * FROM List") or die(mysql_error());
var_dump($_POST);
echo "<table id=list>
<tr id='list_header'>
<th class='list'>ID</th>
<th class='list'>Description</th>
<th class='list'>Winner</th>
</tr><form name='edit' action='inde.php?id=prizes' method='post'>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td class='list'><span id='n" . $row['Number'] . "' onclick='exchange(this.id)'>" . $row['Number'] . "</span><input name='n" . $row['Number'] . "b' id='n" . $row['Number'] . "b' class='replace' type='text' value='" . $row['Number'] . "' style='display:none;'></td>";
echo "<td class='list'><span id='d" . $row['Number'] . "' onclick='exchange(this.id)'>" . $row['Description'] . "</span><input name='d" . $row['Number'] . "b' id='d" . $row['Number'] . "b' class='replace' type='text' value='" . $row['Description'] . "' style='display:none;'></td>";
echo "<td class='list'><span id='w" . $row['Number'] . "' onclick='exchange(this.id)'>" . $row['Winner'] . "</span><input name='w" . $row['Number'] . "b' id='w" . $row['Number'] . "b' class='replace' type='text' value='" . $row['Winner'] . "' style='display:none;'></td>";
echo "<td></td>";
echo "</tr>";
}
echo "</table><input type='submit' value='Save Changes'></form>";
?>`
From what I understand you have a string id and you want to do the following:
if(strlen($id)>1){
$important=substr($id, 0, 1); //that gets the important stuff
$id = ($id, 1, strlen($id)); //that's id without the first char
}
else{
$important=$id;
$id=""; //empty string
}
You may want to try Ajax to send the modified values to the server one by one instead of all together at the end.