How to take input of full line from mysql in html php - php

My database field contains a sentence as a varchar suppose "Hello I am online" I want to show that field as a input placeholder. I write the following code but it shows only "Hello". but if I place echo command then if shows the full sentence. I think i am making some silly mistakes but could not find what is that. please help.
<?php
$servername = "localhost:3306";
$username = "admin";
$password = "pass";
$dbname = "test_db";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Database Connection failed: " . $conn->connect_error);
}
$sql = "select * from test_table";
$sqldata = $conn->query($sql);
if(! $sqldata)
{
die ("error opening database");
}
?>
<form method="post" action="<?php $_PHP_SELF ?>">
<div class="form_layout">
<?php while( $row = $sqldata->fetch_assoc()) : ?>
<div >
<label> Name :</label>
<input type="text" id="iname" name="institutename" placeholder=<?php echo $row['msg']; ?>>
<?php echo $row['msg']; ?>
</div>
<?php endwhile ?>
</div>
</form>
it showing "hello" in the input section but"Hello I am online" on the next echo command.

Your mistakes is here
placeholder=<?php echo $row['msg']; ?>
Replace this
placeholder="<?php echo $row['msg']; ?>"

Related

How make query and show data

I try to do input number form and make search in page grapht.php. For example I write code:
<form action="grapht.php" method="post">
Name: <input type="text" name="number" autocomplete="off"><br>
<input type="submit">
</td>
</form>
<? $number= $_POST["number"]; ?>
And then make query my MySQL table:
$shipmin = 1;
$shipmax=$number;
$uzklausimas ="SELECT * FROM MyGuests WHERE id >= '$shipmin' AND id<= '$shipmax'";
$minmax=mysqli_query($conn,$uzklausimas);
while($ru=mysqli_fetch_assoc($minmax)){
echo "$ru[id] <br>";
}
But my code doesn't work. Maybe somebody could give me advice how need solve this my problem?
Do you initialize your db connection ($conn) before this line ?
$minmax=mysqli_query($conn,$uzklausimas);
if I write line $shipmax=45; it is working well, but I have static date from 1 to 45. If I write $shipmax=$number; It is not work.
all my code
<?php
$servername = "localhost";
$username = "xxxxx";
$password = "xxxx";
$dbname = "xxxxx";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$shipmin = 1;
$shipmax=$number;
$uzklausimas ="SELECT * FROM MyGuests WHERE id >= '$shipmin' AND id<='$number'";
$minmax=mysqli_query($conn,$uzklausimas);
while($ru=mysqli_fetch_assoc($minmax)){
echo "$ru[id] <br>";
}
mysqli_close($conn);
?>
<form action="forum.php" method="post">
Name: <input type="text" name="name" autocomplete="off"><br>
<input type="submit">
</td></form>
<? $number= (int)$_POST["name"]; ?>
<?php echo $number; ?>
<br>
Page where you could see it is http://ortex.lt/forum.php

Wordpress PHP FIle not running

I have a custom search form on my website with this code:
<center>
<form action="orderstatus.php" method="POST">
<input type="text" name="OrderLineNumber" value="">
<br>
<input type="submit" value="Submit">
</form>
</center>
And although I call orderstatus.php to action . When I press the submit button I get this Url : blablabla.de/action_page.php?OrderLineNumber=+23563623
This means instead of running orderstatus.php it's running action_page.php
My orderstatus.php is trying to print One Select Row from database like this :
<!DOCTYPE html>
<html>
<body>
<?php
$servername = "*******";
$username = "*******";
$password = "*******";
$dbname = "********";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM OrderStatus";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc() & fetch_assoc() = $_POST["name"] ) {
echo "<br> Order Line Number: ". $row["OrderLineNumber"]. " - Date Started: ". $row["Date Started"]. " " . $row["Status"] . "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
</body>
</html>

how to return values from mysql table with ajax and php

I am new to programing. I have one form to create Groups. It has two text fields Code Id and Code description. After submitting it showed me that the Code Id which i entered is already exist and if not it add one record in MySQL table. What I want that when I leave the Id text field at the same time with onchange event and Ajax to search table and alert if the Id already exit and at the same time fill name text box with description of that Code Id. How to do that? My code is
HTML file
...
</style>
<body>
<H1>Create Grup</h1>
<br>
<form action="creategrup.php" method="post">
<p>
<label for="codigo">Grup Id:</label>
<input type="text" required="required" autofocus="autofocus"
maxlength="4" name="codigo" id="codigo">
</p>
<p>
<label for="nombre">Grupo description:</label>
<input type="text" required="required" name="nombre" id="nombre"">
</p>
<input type="submit" value="Submit">
</form>
</body>
</html>
And PHP is
<?php
include 'connectdb.php';
$nombre=$_POST['nombre'];
$codigo=$_POST['codigo'];
$sql = "select codigogrupo,nombregrupo from grupo where
codigogrupo='$codigo'";
$query = mysqli_query($conn, $sql);
if (mysqli_num_rows($query) >0) {
echo "<p><h1><b>Grup Id $codigo allready exist....</h1></b></p><br>";
echo "<a href='creategrup.html'>Go Back</a>";
}
else {
mysqli_query($conn, "insert into grupo(codigogrupo,nombregrupo)
values('$codigo','$nombre')");
if(mysqli_affected_rows($conn)>0){
echo "<p><h1><b>Grup $nombre added</h1></b></p>";
echo "<a href='creategrup.html'>Go Back</a>";
} else {
echo "Grup not added<br>";
echo mysqli_error ($conn);
}
}
?>
Connectdb.php
<?php
$servername = "server name";
$username = "user name";
$password = "password";
$dbname = "database";
// Create connection
$conn = mysqli_connect($servername, $username, $password,$dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// echo "Connected successfully";
?>
Please help me.
You are getting already exist message so that may be confirm that there is another data with same id. For further assistance please knock me

i cant input values into a form directly from my data in the database

how do i get the value in the form input to be equal to the data fetched from my database . when i tried to retrieve the value from the post super global i got Array ( [john_doe] => ) i.e an empty value
<?php print_r($_POST); ?>
<form action="" method="post">
<?php while($subs = mysqli_fetch_assoc($result)) { ?>
<input type="" name="john_doe" value='<?php echo $subs["menu_name"]; ?>'>
<input type="submit" value="sumbit">
<?php } ?>
</form>
There will be some wrong with your database fetching, you can try this following code for your purpose
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "**YOUR QUERY**";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
<input type="text" name="john_doe" value='<?php echo $row["menu_name"] ;?>'>
<input type="submit" value="sumbit">
$conn->close();
?>

Exporting php in a same file as html to use css (PHP issue!)

my name is Luis, well Im trying to make a file in html, css and php which gets information from the database and outputs it. But it has to be in the same page as the html so I can edit with CSS... thank you all!
<html>
<head>
</head>
<body>
<script>
</script>
<form action="<?php include 'file.php';?>" method="POST">
<b>Busque Jรก</b>
<input type="text" name="prestador" placeholder="prestador">
<input type="text" name="cidade" placeholder="cidade">
<input type="submit">
</form>
</body>
</html>
File in PHP:
<?php
$servername = "localhost";
$username = "username";
$password = "";
$dbname = "test";
$namer = $_POST['prestador'];
$cities = $_POST['cidade'];
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT name,address,city FROM tb_prestadores WHERE name = '$namer' AND city = '$cities'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "name: " . $row['name']. " Endereço - : " . $row['address']." Cidade :".$row['city']."<br>";
}
} else {
echo "It is going to else";
}
$conn->close();
?>
You are including / inserting the file wrongly.
What you should do:
1) Copy your html code and paste it at the bottom of your php code i.e. after the ending '?>' tag. Your code should look like this:
<?php
$servername = "localhost";
$username = "username";
$password = "";
$dbname = "test";
$namer = $_POST['prestador'];
$cities = $_POST['cidade'];
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT name,address,city FROM tb_prestadores WHERE name = '$namer' AND city = '$cities'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "name: " . $row['name']. " Endereço - : " . $row['address']." Cidade :".$row['city']."<br>";
}
} else {
echo "It is going to else";
}
$conn->close();
?>
<html>
<head>
<!-- include your css here-->
</head>
<body>
<script>
</script>
<form action="" method="POST">
<b>Busque Jรก</b>
<input type="text" name="prestador" placeholder="prestador">
<input type="text" name="cidade" placeholder="cidade">
<input type="submit">
</form>
</body>
</html>
Btw, this is not the 'optimal' way to code applications. Please look into Smarty or some other templating language as well as MVC once you get the hang of php and html basics. Good luck!
To begin with, your <?php include 'file.php';?> is miss-placed and should not be in the action tag of your form rather it should be right before it, like so:
<html>
<head></head>
<body>
<?php include 'file.php';?>
<form action="" method="POST">
<b>Busque Jรก</b>
<input type="text" name="prestador" placeholder="prestador">
<input type="text" name="cidade" placeholder="cidade">
<input type="submit">
</form>
</body>
</html>

Categories