PHP data entry (SQL) - php

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.

Related

PHP Dropdown list maintain on reload

I am new to PHP and have just started the professional development in this language. I have created a dynamic dropdown list in php as under:
$sql="select DISTINCT State from branchinfo";
$result = $conn->query($sql);
if($result->num_rows > 0)
{
echo "< SELECT NAME='states'>";
while($row=$result->fetch_assoc())
{
echo "< OPTION NAME = '" . $row['State'] . "'" . " VALUE = '" . $row['State'] . "'>" . $row["State"];
}<br>
echo "< /SELECT>";
}
echo "< INPUT TYPE='submit' name='submit' value='submit'>";
Problem is when I select a state and click on submit, the list reloads and my selection is lost, the first option gets selected by default. I have tried to embed the script within OPTION but it didn't worked, I tried it as under:
echo "< OPTION NAME='" . $row['State'] . "'" . " VALUE='" . $row['State'] . "'" . if(isset($_POST["submit"])){ if($_POST["states"] == $row['State']) echo "selected";} . " >" . $row["State"];<br>
I am not using any javasrcipt / jquery till now on this page and not planning to use it either. plz provide a solution within this code. Please help.
Some additional information
The mthod i tried as mentioned above, works fine on hardcoded static drop downlist items written in html form. It stops working for dynamically generated list.
You have to add dropdown inside form tag, then it will work.
$sql = "select DISTINCT State from branchinfo";
$result = $conn->query($sql);
echo "<form method='POST'>";
if ($result->num_rows > 0) {
echo "<SELECT NAME='states'>";
while ($row = $result->fetch_assoc()) {
$sel = ( $_POST['states'] == $row['State'] ) ? "selected" : "";
echo "< OPTION NAME = '" . $row['State'] . "'" . " VALUE = '" . $row['State'] . "' " . $sel . ">" . $row["State"] . "</OPTION>";
}
echo "</SELECT>";
}
echo "<INPUT TYPE='submit' name='submit' value='submit'>";
echo "</form>";
Use session to remember your choice. And when the page is reloaded just retrieve your previous selected value.
like
while($row=$result->fetch_assoc())
{
if($sessionvalue==$row['State']){
echo "< OPTION SELECTED NAME = '" . $row['State'] . "'" . " VALUE = '" . $row['State'] . "'>" . $row["State"];
}else{
echo "< OPTION NAME = '" . $row['State'] . "'" . " VALUE = '" . $row['State'] . "'>" . $row["State"];
}
}

Displaying Image From URL Stored in SQL Databse Using PHP

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!

Delete specific row in database table and generatet html table

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.

Variables and HTML HREF

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>";

Accessing variable values from a function nested within a function running a mysqli_fetch_assoc() function

I am trying to generate a form, which amounts to a delete button under a table that presents client profile information on a site admin page. The table appears fine. The form, however, that I want to generate using a function within the while loop upon each one of its iterations does not include the variables $id, $firstname, $lastname, despite not generating an error message about undefined variables (my error message configuration is set to do this and does in fact work). I simply get a button that says Delete, and the form processing function fails because the $_POST values are null (as are the variable values in the form itself). The problem, therefore, must be with the form function's access to the table's variables.
If I include the form within the loop as an echo statement rather than as a function, the button works as desired. However, I would like to keep these two aspects separated for (I suppose) neurotic and idiosyncratic organizational preferences. I have previously had this type of issue, which I resolved by better understanding variable scope. But, the variable within the form function is defined as global, which leaves me at a loss.
Is this a peculiarity of the while loop and its interaction with the mysqli_fetch_assoc()?
I sought some indication of what might cause this at the php.net manual's description of mysqli_fetch_assoc(), but I could find nothing to suggest that this isn't allowed.
Any help would be grand. Also, as a self-taught fellow, please try and be constructive in you comments and criticisms. I am sure I have a very great deal to learn, and would be very grateful for guidance from those who know more than I.
Here is my table generating function:
function table_delete_client() {
$client_set = find_all_clients();
while($client = mysqli_fetch_assoc($client_set)) {
$id = htmlentities($client['id']);
$firstname = htmlentities($client['firstname']);
$lastname = htmlentities($client['lastname']);
$position_en = htmlentities($client['position_en']);
$position_fr = htmlentities($client['position_fr']);
$division_en = htmlentities($client['division_en']);
$division_fr = htmlentities($client['division_fr']);
$organisation_en = htmlentities($client['organisation_en']);
$organisation_fr = htmlentities($client['organisation_fr']);
$telephone = htmlentities($client['telephone']);
$email = htmlentities($client['email']);
$email_list = htmlentities($client['email_list']);
$username = htmlentities($client['username']);
echo "<table>" .
"<tr>" .
"<th>ID</th>" .
"<td>$id</td>" .
"</tr>" .
"<tr>" .
"<th>First Name</th>" .
"<td>$firstname</td>" .
"</tr>" .
"<tr>" .
"<th>Last Name</th>" .
"<td>$lastname</td>" .
"</tr>" .
"<tr>" .
"<th rowspan='2'>Position</th>" .
"<td>$position_en</td>" .
"</tr>" .
"<tr>" .
"<td>$position_fr</td>" .
"</tr>" .
"<tr>" .
"<th rowspan='2'>Division</th>" .
"<td>$division_en</td>" .
"</tr>" .
"<tr>" .
"<td>$division_fr</td>" .
"</tr>" .
"<tr>" .
"<th rowspan='2'>Organisation</th>" .
"<td>$organisation_en</td>" .
"</tr>" .
"<tr>" .
"<td>$organisation_fr</td>" .
"</tr>" .
"<tr>" .
"<th>Telephone</th>" .
"<td>$telephone</td>" .
"</tr>" .
"<tr>" .
"<th>Email</th>" .
"<td>$email</td>" .
"</tr>" .
"<tr>" .
"<th>Email List</th>" .
"<td>$email_list</td>" .
"</tr>" .
"<tr>" .
"<th>Username</th>" .
"<td>$username</td>" .
"</tr>" .
"</table>" .
"<br />";
form_delete_client();
}
mysqli_free_result($client_set);
}
Here is my form generating function nested within the 'while' loop:
function form_delete_client() {
global $id;
global $firstname;
global $lastname;
echo $firstname;
echo "<form id=\"delete_client\" method=\"post\" action=\"" . $_SERVER['PHP_SELF'] . "?sub_page=2\">" .
"<input type=\"hidden\" name=\"id\" value=\"" . $id . "\" />" .
"<input type=\"hidden\" name=\"firstname\" value=\"" . $firstname . "\" />" .
"<input type=\"hidden\" name=\"lastname\" value=\"" . $lastname . "\" />" .
"<input type=\"submit\" id=\"delete_button\" class=\"button\" name=\"submit\" value=\"Delete " . $firstname . " " . $lastname . "\" />" .
"</form>" .
"<br>";
}
Interesting problem. I tried replicating your situation and it worked for me - calling a function from within a mysqli while loop.
<?php
$link = mysqli_connect('HOST', 'USER', 'PASS', 'DATABASE');
$sql = 'SELECT * FROM cds';
$result = mysqli_query($link, $sql);
while ($row = mysqli_fetch_array($result)) {
$firstname = $row['titel'];
form_delete_client();
}
function form_delete_client() {
global $firstname;
echo "<BR>".$firstname;
}
This outputs:
<BR>Beauty<BR>Goodbye Country (Hello Nightclub)<BR>Glee
Functions should work fine from within a loop. I'm not certain why, in your case, that the data is not being fed through. I suppose it could have something to do with you calling a global $firstname and the global is being set once and then not overwritten. But that's just a guess. What I would recommend doing instead is the following:
form_delete_client($id, $firstname, $lastname);
Then, your function would look like this:
function form_delete_client($id, $firstname, $lastname) {
echo $firstname;
echo "<form id=....";
}
You don't need to globalize anything then.

Categories