Echoed data as a input value - php

I can't figure out how to get MySQL data echoed as a input value. It is for item edit purpose. I need to get data inside of input tag. Here is my code that doesn't work. It work just with textarea tag.
$ID = $_GET['id'];
$sql = "SELECT * FROM fm where ID = $ID";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
$title = $row['title'];}}
... etc.
<input type="text" name="title" vlaue="<? echo $title;?>">

You have a typo in your HTML:
<input type="text" name="title" vlaue="<? echo $title;?>">
change it to
<input type="text" name="title" value="<? echo $title;?>">
Also, make sure the query runs, the correct id is passed as GET parameter, it has a corresponding record in the database, the corresponding record has a title value and it is correctly returned.

Related

Updating multiple MYSQL rows with one submit button [duplicate]

This question already has an answer here:
How do I insert Data Into database with many Input which has same name?
(1 answer)
Closed 3 years ago.
i was trying to updating multiple MYSQL rows with one submit button,
before i used to create submit for each row, but since i have a lot of rows now i need to update them all together
index.php
<?php
if (mysqli_num_rows($row){
while($row1= mysqli_fetch_assoc($row){
id<input type="text" value="<?php echo $row["id"];?>" name='id' id="id" >
id<input type="text" value="<?php echo $row["name"];?>" name='name' id="name" >
}
<button type="submit" formaction="update.php">
submit
</button>
}
update.php
$id= $_POST['id'];
$name= $_POST['name'];
$sql = "UPDATE `$tabelname` SET
name='$name'
WHERE id='$id'";
its updating the first row only
Assuming that id is the primary key,
In your html, you need to use an array in the name. This allows the form to send it as an array instead of just taking the last value:
<?php while($row1= mysqli_fetch_assoc($row): ?>
<div>
<label>Name: </label>
<input type="text" value="<?=$row["name"]?>" name="name[<?= $row["id"] ?>]" id="name-"<?= $row["id"] ?> " />
</div>
<?php endwhile; ?>
The key here is name=“name[]”. The square brackets make it an array. I’m using the id as the index. (Note that <?= is just a much more concise way of writing <?php echo)
Then, in your php script, the easiest way to show you is to iterate through the array and do an update each time:
$row = $_POST[‘name’];
foreach($row as $id => $name) {
// This is a big No-No!
// $sql = "UPDATE `$tabelname` SET name='$name' WHERE id='$id'";
// use prepared statements. Always.
$sql = "UPDATE `$tabelname` SET name=? WHERE id=?"; // assuming “tabelname” is not user-provided
// database connection up to you
$stmt = $db->prepare($sql);
$stmt->execute( [$name, $id] );
}

Populate textbox with table entry plus one?

I am trying to increment project number based on the last entry. The the primary key PROJECTNOID auto-increments but is not the same format as the project number (Ex: PROJECTNOID = 1 and Project Number = 19000). I don't want this to be a dropdown box even though some of my code shows the opposite.
<?php
connect = mysqli_connect("**", "**", "**", "**");
$query4 = "SELECT PROJECTNOID, ProjectNumber FROM tblProjects ORDER BY
PROJECTNOID";
$result4 = mysqli_query($connect,$query4);
$options4 = "";
while($row4 = mysqli_fetch_row($result4);){
$options4 = $options4."<input value=$row4[0]$row4[1]</input>";
}
?>
Here is the html textbox:
<label for="txtfield">Project Number</label>
<!--<input type="text" id="reqtxtfield" name="projectnumber"
value="<?php ?>" readonly/>-->
<?php echo $options4;?>
But it would look like how you had it but instead of '1' inside the
box it would display '19000' and there would be nothing outside of the
box other than the label "Project Number". As far as i'm aware you can
assign a value to the text box, regardless of whatever the input is. I
would like it to display the value from one field name but actually
contain the value from a different field name. Both are in the same
table of course.
OK - gotcha. Unfortunately, you cannot do that. A textbox can only have one value and the user is always free to change that value, even if you make it read-only. You can test that out by using the developer toolbar in your browser. Probably a good time to mention that all user input should be considered dangerous and you should never trust it. Once they have submitted the form you need to verify it.
What I would recommend in your case is to use a hidden <input> which contains the value you actually want to submit; projectnoid. You can then display the Project Number in any manner you choose.
<form>
<h1>Project Number: 19000</h1>
<input type="hidden" name="projectnoid" value="1">
<input type="submit" name="submit">
</form>
To generate this, you would:
<?php
while($row4 = mysqli_fetch_row($result4)){
$projectnoid = $row[0];
$projectNumber = $row[1];
echo '<h1>' . $projectNumber . '</h1>';
echo '<input type="hidden" name="projectnoid" value="'. $projectnoid .'">
}
PHP:
<?php
$query_5 = "SELECT MAX(ProjectNumber) FROM tblProjects;";
$result_5 = mysqli_query($conn, $query_5);
$row_5 = mysqli_fetch_array($result_5);
$nextproject=$row_5['MAX(ProjectNumber)']+1;
?>
HTML:
<html>
<form class="myform" action="<?php echo htmlspecialchars($_SERVER[" PHP_SELF "]);?>" method="post">
<label for="txtfield">Project Number</label>
<input type="text" id="reqtxtfield" name="projectnumber" value="<?php echo $nextproject ?>" readonly/><br>
After running successful insert query:
echo "<meta http-equiv='refresh' content='0'>"; //REFRESH PAGE TO UPDATE PROJECT NUMBER

Editing data in multiple columns from front-end

I have a table in the database, I can edit a column of particular row at a time. but, i want to edit multiple columns of a single row at a time with an option of edit and then save it to the database ,we can do it from back-end by editing in the database itself. But, i want to do it from front-end .Now i am able to display the data in the table. I am new to coding , so can anyone help me where to start for this particular task of editing multiple columns at the same time.
Here is the code for displaying the data..
<?php
include('db.php');
include 'header.php';
include 'gobacktomenu.php';
$TND_ID = $_GET['TND_ID'];
$sql = "SELECT * FROM new_tnds WHERE TND_ID = ('".$TND_ID."')";
$result = $link->query($sql);
echo "<div style='overflow-x:auto;'><table border = '1'><font size = '2' face='verdana'>
<th>TND ID</th><th>Site name S1</th><th>Site name S2</th><th>Call sign S1</th><th>Idea ID S1</th><th>Call sign S2</th><th>Idea ID S2</th><th>Site to integrate</th><th>True azimuth (°) S1</th><th>True azimuth (°) S2</th><th>Pathlength (km)</th><th>TR Antenna diameter (m)</th><th>TR Antenna height (m) S1</th><th>TR Antenna height (m) S2</th><th>Channel ID S1</th><th>Channel ID S2</th><th>Design frequency S1</th><th>Design frequency S2</th><th>Polarization</th><th>Radio model</th><th>TX power (dBm)</th><th>Receive signal (dBm)</th><th>Planning Remarks</th><th>Projects Remarks Uploaded by</th><th>O & M Remarks Uploaded by</th><th>Planning Remarks Uploaded by</th><th>TXN NOC Remarks Uploaded by</th></tr>";
if ($result->num_rows > 0) {
// output data of each row
$row = $result->fetch_assoc();
echo "<tr><td>".$row["TND_ID"]."</td><td>".$row["Site_name_S1"]."</td><td>".$row["Site_name_S2"]."</td><td>".$row["Call_sign_S1"]."</td><td>".$row["Idea_ID_S1"]."</td><td>".$row["Call_sign_S2"]."</td><td>".$row["Idea_ID_S2"]."</td><td>".$row["Site_to_integrate"]."</td><td>".$row["True_azimuth_S1"]."</td><td>".$row["True_azimuth_S2"]."</td><td>".$row["Path_length_(km)"]."</td><td>".$row["TR_Antenna_diameter_(m)_S1"]."</td><td>".$row["TR_Antenna_height_(m)_S1"]."</td><td>".$row["TR_Antenna_height_(m)_S2"]."</td><td>".$row["#1_Channel_ID_S1"]."</td><td>".$row["#1_Channel_ID_S2"]."</td><td>".$row["#1_Design_frequency_S1"]."</td><td>".$row["#1_Design_frequency_S2"]."</td><td>".$row["Polarization"]."</td><td>".$row["Radio_model_S1"]."</td><td>".$row["TX_power_(dBm)_S1"]."</td><td>".$row["Receive_signal_(dBm)_S1"]."</td><td>".$row["Planning_Remarks"]."</td><td>".$row["Projects_remarksupdated_user_name"]."</td><td>".$row["O_M_remarksupdated_user_name"]."</td><td>".$row["Planning_remarksupdated_user_name"]."</td><td>".$row["txn_noc_remarksupdated_user_name"]."</td></tr>";
}
echo "</table></div>";
$link->close();
?>
you can use this structure ,this can be used for editing 2 tables and multiple columns table a has columns (aid,a1,a2) table b has columns (bid,b1,b2) i know code looks little rough but just wrote it in 5 min to give you an idea
<?php
//this only executes when button is clicked
if (isset($_POST["submit"] && $_POST["submit"]!=""){
$counta=$_POST["aid"];
//loops through the posts inside here do your query with the set command
//like "update a set a1='$a1',a2='$a2" where aid ='$_POST["aid'][$i]"
for($i=0;$i<$count;$i++){
$a1[$i]=$_POST["a1"][$i];
$a2[$i]=$_POST["a2"][$i];
//your query here
}
$countb=$_POST["bid"];
//loops through the posts inside here do your query with the set command
//like "update b set b1='$b1',b2='$b2" where aid ='$_POST["bid'][$i]"
for($i=0;$i<$count;$i++){
$a1[$i]=$_POST["a1"][$i];
$a2[$i]=$_POST["a2"][$i];
}
//now the part before the button gets clicked
//do your query to select data from database
?>
<html>
<body>
//creating loop inside html
<?php
$a=0;
while($rowa = mysql_fetch_assoc($resulta){
?>
//getting the input data
<input type="checkbox" name="aid[]" value="<?php echo $row["aid"]; ?>>
<label>a1</label>
<input type="text" name="a1[]" value="<?php echo $row["a1"]; ?>>
<label>a2</label>
<input type="text" name="a2[]" value="<?php echo $row["a2"]; ?>>
//ending the loop
<?php
$a++;
}
//b works the same as a
$b=0;
while($rowb = mysql_fetch_assoc($resultb){
?>
<input type="checkbox" name="bid[]" value="<?php echo $row["bid"]; ?>>
<label>b1</label>
<input type="text" name="b1[]" value="<?php echo $row["b1"]; ?>>
<label>b2</label>
<input type="text" name="b2[]" value="<?php echo $row["b2"]; ?>>
<?php
$b++;
}
?>
<input type="submit" name="submit" value="submit">
</body>
</html>
You need use form instead of table while editing and pass the fetched values on input of form and write update query on form submit.
if ($result->num_rows > 0) {
// output data of each row
$row = $result->fetch_assoc();
}
if(isset($_POST['submit']))
{
$query = "update details set Site_name_S1='$_POST['submit']',Site_name_S2='$_POST['Site_name_S2']',Call_sign_S1='$_POST['Call_sign_S1']',Idea_ID_S1='$_POST['Idea_ID_S1'] where TND_ID=$_POST['TND_ID']";
$res = mysql_query($query);
}
<form method="post">
<input type="text" name="id" value="<?php echo $row['TND_ID'] ;?>" >
<input type="text" name="Site_name_S1" value="<?php echo $row['Site_name_S1'] ;?>" >
<input type="text" name="Site_name_S2" value="<?php echo $row['Site_name_S2'] ;?>" >
<input type="text" name="Call_sign_S1" value="<?php echo $row['Call_sign_S1'] ;?>" >
<input type="text" name="Idea_ID_S1" value="<?php echo $row['Idea_ID_S1'] ;?>" >
<input type="submit" name="submit">
</form>
Just check the quotes while writing update query.
You can use Ajax
Just a fragment of code:
.............
<td><input type="text" row_id=".$some_row_num_from_table."
col-name=".$some_col_name_from_table." disabled class="some-val" value=".$some_from_base."></td>
.............
than little Ajax
$('.some-val').dblclick(function() {
$(this).attr('disabled', false);
});
$('.some-val').blur(function() {
$(this).attr('disabled', true);
var send_data = { 'row_id':$(this).attr('row_id'),
'col-name':$(this).attr('col-name'),
'myval':$(this).val()
};
$.ajax({
url:'your_url.php',
data:send_data,
method: 'post',
success: function(data){...}
});
});
your_url.php
if(isset($_POST['row_id'])){
$sql = "UPDATE mytablename SET `".$_POST['col-name']."` = ".$_POST['myval']." WHERE `row_id` = ".$_POST['row_id'];
$link->query($sql); // it s bad query without protect of sql injection JUST DEMO
}

php, data from table into a specific input inside a html doc

I need help getting the last row of a table into a html input tag.
I think an example will help you better understand my question, so here:
<input value=<?php $conn->query("SELECT id FROM members ORDER BY id DESC LIMIT 1"); ?> , disabled type="text" name="username" id="username" maxlength="10" required>
I need to put inside the value of the input box, the last id in my "members" table.
thanks!
Try to clean a bit your code and split it off in two pieces so you can understand that easier if you come back to your code in the future. Something like that:
<?php
$sql = "SELECT id FROM members ORDER BY id DESC LIMIT 1";
$result = $conn->query($sql);
$userID = "";
if($result->num_rows > 0){
$row = $result->fetch_assoc();
$userID = $row["id"];
}
?>
<input value="<?php echo $userID; ?>" disabled type="text" name="username" id="username" maxlength="10" required>
If you have just inserted that row you probably want to use mysqli_insert_id() instead. I suggest you to read more about that.
Anyway, print a user id is never a good idea. Please try to not do that, its a very bad practice.

Using a PHP variable as a form fields default value

I have a created an HTML form where users sign up and input there data into an SQL database. I have then retrieved that data in a webpage where they can view their profile. I have created a page where users can edit there profile by creating a form which updates the value in the SQL database for there user id.
I would like the form to use the current value of the SQL cell as the default for that user to make alterations easier. Example: currently user 7 has their city set as New York, when they visits the edit info page, the city field in the form already hase New York as the default value.
I have no problem getting the SQL info and assigning it to a variable, I just don't understand how to set it as the default value. I am aware of how you set default values for input fields though.
My code:
<?php
$id = $_SESSION["user_id"];
// Create a query for the database
$query = "SELECT full_name FROM users WHERE id = $id LIMIT 1";
// Get a response from the database by sending the connection
// and the query
$response = #mysqli_query($dbc, $query);
// If the query executed properly proceed
if($response){
while($row = mysqli_fetch_array($response)){
echo $row['full_name'];
echo mysqli_error();
}
}
?>
<input type="text" name="aboutme" defualt="<?php echo $row['aboutme'] ?>" >
There's no default value for html input.
Input can has value, using attribute value:
<input type="text" name="some_name" value="Some value" />
In your case it's
<input type="text" name="aboutme" value="<?php echo $row['aboutme']?> />
Input can also has placeholder - some value that is present in an input, but erased when user starts to edit input's content:
<input type="text" name="aboutme" value="<?php echo $row['aboutme']?> placeholder="some value" />
How about
<?php
$id = $_SESSION["user_id"];
// Create a query for the database
$query = "SELECT full_name FROM users WHERE id = $id LIMIT 1";
// Get a response from the database by sending the connection
// and the query
$response = #mysqli_query($dbc, $query);
// If the query executed properly proceed
if($response){
while($row = mysqli_fetch_array($response)){
echo $row['full_name'];
?>
<input type="text" name="aboutme" value="<?php echo $row['aboutme'] ?>" >
<?php
echo mysqli_error();
}
}
?>
And here is a good example http://www.w3schools.com/php/showphpfile.asp?filename=demo_db_select_pdo
Neither of the answers worked and upon further research and trial and error I created a solution.
I changed the value that was store in the array to just be a normal php variable:
$aboutme = $row['aboutme'];
I then called that variable using the following code:
<input type="text" name="aboutme" value="<?php echo htmlspecialchars($aboutme); ?>" >
Thanks for your help.
I hope you find my answer useful.
Why don't you try using it as a place holder? This will provide editable text.
<input type="text" name="aboutme" placeholder="<?php echo $row['aboutme'];" />

Categories