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

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.

Related

Echoed data as a input value

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.

How do i do the right to use "NOT IN" in PHP MYSQL

how can i fix it this problem, because i want to find the ID lost and bring me back to use the ID lost, i did this code but doesn't work because it returns nothing.
PHP
$query = 'SELECT (folio+1) FROM detalles_devoluciones WHERE (folio+1) NOT IN (SELECT folio FROM detalles_devoluciones) limit 1;';
$resultado = mysql_query($query) or die (mysql_error());
$total = mysql_num_rows($resultado);
while ($c = mysql_fetch_array($resultado)){
$folio = $c['folio'];
echo $folio;
}
I test this:
print_r($folio);
and nothing too
UPDATE:
HTML
<label for="folio">Folio:</label>
<input type="text" value="<?=$c["folio"]?>" name="folio" disabled/>
<input type="hidden" value="<?=$c["folio"]?>" name="folio" />
or
<label for="folio">Folio:</label>
<input type="text" value="<?=$folio?>" name="folio" disabled/>
<input type="hidden" value="<?=$folio?>" name="folio" />
What are you trying to do? Why are you looking for folio+1?
The syntax of your sql-statement looks correct to me, have you tried to send it directly to the database via phpmyadmin or console?
btw you should not use the mysql_* extension as it is deprecated. Better use mysqli_* or PDOs functions to handle the database-operations.http://php.net/manual/de/mysqlinfo.api.choosing.php
edit:
To fetch the column it would be good to use an alias for the selected column.
SELECT (folio+1) AS folio FROM...
So you can access it over the index 'folio' after fetching the result

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'];" />

How To Retrieve Data From Database

I am trying to retrieve a value from database and put it in a textbox.
I tried:
<input type="text" name="Balance" value="<?php $Balance= $_GET["Balance"]; ?>" readonly = "true" />
and:
<input type="text" name="Balance" value="<?php echo $Balance; ?>" readonly = "true" />
But I am getting an error saying that Balance is undefined.
The php script is located in another page (connect.php) and I already put the
<form method="post" action="connect.php" >
What else should I do?
TIA
First of all your form method is POST, so:
... $Balance= $_POST["Balance"]; ...
I think you dont know anything about what you are currently doing.
$_GET is not used to fetch something from a Database, it is used to pass and fetch parameters via URL. For accessing a Database, you need mysqli or PDO. Look for some tutorials.
Example for the code you need:
$sql = "SELECT balance FROM mytable";
$result = $db->Execute($sql);
echo "<input type='text' name='Balance' value='" . $result . "' readonly/>"
Just for you to get an idea:
mysql_connect('127.0.0.1', 'root', '')
and mysql_select_db('accounting')
and ($res = mysql_query("SELECT `id`,`balance` FROM `accounts`"))
and ($row = mysql_fetch_object($res));
HTML:
<input type="text" name="Balance" value="<?= $row->balance ?>" >
It won't work on your computer, because I don't know the tables you're using and other things I need to know, and anyway, do a search, this topic probably has been covered a million times in the last 10-15 years since PHP exists, long before Stack Overflow existed.
PS: That's one of the oldest ways of doing it. There are other ways, like PDO, but I use the "old" way and am happy with it.

Php: updating database with values from dynamically generated form fields

How do I save the following form?
<form name="myform">
<input type="text" name="title" value="title" />
$result = mysql_query("SELECT id, text from details where parent='$parent'
order by id asc") or die('Error');
while(list($id,$ftext) = mysql_fetch_row($result)) {
?>
<textarea name="formfield<?php echo $id;?>" id="<?php echo $id;?>">
<?php echo $ftext;?>
</textarea>
<?php
}
</form>
As you can see above, the MySQL query will load data and dynamically create textareas:
<textarea name="formfield34" id="34">text</textarea>
<textarea name="formfield56" id="56">more text</textarea>
<textarea name="formfield78" id="78">anothet text</textarea>
I need to update database like this:
$result = mysql_query("UPDATE details SET text='$formfield34' WHERE id ='34'") or die('Error');
$result = mysql_query("UPDATE details SET text='$formfield56' WHERE id ='56'") or die('Error');
$result = mysql_query("UPDATE details SET text='$formfield78' WHERE id ='78'") or die('Error');
The problem is that each form will have a different number of textareas loaded dynamically and different $ids. I can not imagine hot to save this form.
Thanks.
What you probably need to do is to look at using array notation in your form field names like this:
<textarea name="formfield[34]" id="34">text</textarea>
<textarea name="formfield[56]" id="56">more text</textarea>
<textarea name="formfield[78]" id="78">anothet text</textarea>
PHP will automatically build an array in $_POST['formfield']. that you can access with all this data. This make it easy to loop through this array and generate SQL for your updates.
foreach($_POST['formfield'] as $key => $value) {
// write SQL here
}
Now, rather than a bunch of individual SQL queries, you might want to consider using REPLACE syntax so that you can insert/update your data with a single query.
in the "while" while creating the textarea save an array with the id of these textarea.
Then retrace the array to use ids when you need it

Categories