how to write text in textbox from database in php - php

I have a form where I am getting values from previous page in $GET. I want to fetch around 3 different values from database and display them in textbox. How will I do it? Following is my code for getting data from database.
<?php
require_once('config.php');
$id = $_GET['id'];
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link) {
die('Failed to connect to server: ' . mysql_error());
}
//Select database
$db = mysql_select_db(DB_DATABASE);
if(!$db) {
die("Unable to select database");
}
$query = "select question,price,sequence from questions where status = 1 and qid =".$id;
//echo $query;
$result=mysql_query($query);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
$num_rows = mysql_num_rows($result);
if($num_rows==0) {
echo '<center><font color="red"><b>No record found!!</b></font></center>';
}
else {
$row = mysql_fetch_array($result);
echo $row['question'];
echo $row['sequence'];
echo $row['price'];
}
?>
Thanks
Pankaj

<textarea><?php echo htmlentites($row['question']);?></textarea>
or
<input type="text" value="<?php echo htmlentites($row['question']);?>" />
Depending on your fancy.

Related

I am not getting the result properly when conneting to MySQL

For the code below added, i am not getting any result printed.
$con = #mysqli_connect("localhost","root","","temp");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$query="SELECT * FROM `login`";
echo $query;
$result=#mysqli_query($query) or die(mysql_error());
while($row=mysqli_fetch_array($result))
{
echo $row["username"];
}
Try the below code it will work
//conection:
$con = mysqli_connect("localhost","root","","temp") or die("Error " . mysqli_error($con));
//consultation:
$query = "SELECT * FROM login" or die("Error in the consult.." . mysqli_error($con));
//execute the query.
$result = $con->query($query);
//display information:
while($row = mysqli_fetch_array($result)) {
echo $row["username"] . "<br>";
}
Use this code as it is.
$con=mysqli_connect("localhost","root","","temp");
$result = mysqli_query($con,"SELECT * FROM login");
while($row = mysqli_fetch_array($result))
{
echo $row["username"];
}
// use this code and plz check your db name
$host='localhost';
$user='root';
$pass='';
$db_name='temp';
$con=mysqli_connect($host,$user,$pass,$db_name);
if($con)
{
echo "db connect succecssfully";
}
$slt="select * from login";
$query=mysqli_query($slt,$con);
while($row=mysqli_fetch_array($query))
{
echo $row["username"];
}
<?php
$con=mysqli_connect("localhost","root","","temp");
// Here localhost is host name, root is username, password is empty and temp is database name.
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// Perform queries
$result = mysqli_query($con,"SELECT * FROM login");
while($row = mysqli_fetch_array($result)) {
echo $row["username"] . "<br>";
}
mysqli_close($con);
?>
Use this. it may solve your problem.
//connection
$con = mysqli_connect("localhost","root","","my_db");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

PHP-mysql_fetch_array return nothing

I've trying to display values from mysql but it return any empty page. The connection is fine but it does not fetch the data from mysql. I tried all the answers from the similar questions asked. But nothing helped. Can somebody please help me? This is the code
$con= mysql_connect($host, $username, $pwd);
if(!$con)
die("not connected". mysql_errno());
echo(Connected);
mysql_select_db("info",$con);
$query="select * from people";
$result= mysql_query($query,$con) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
echo $row['id']. " - ". $row['people_name'];
echo "<br />";
}
Try to check if your db user,password are correct! I test the code above :
<?php $con=mysqli_connect("localhost","root","","test"); // Check connection
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM people");
while($row = mysqli_fetch_array($result)) {
echo $row['id'] . " -- " . $row['people_name']; echo "<br>";
}
?>
and give me the result without error: 10 -- JOHN 11 -- PRADEEP
I just change mysql_connect to mysqli_connect add in $con= mysql_connect($host, $username, $pwd); a dbname. and $con become $con= mysqli_connect($host, $username, $pwd,$dbname); I use mysqli_query instead of mysql_query. Here is a stackQuestion for the mysql vs mysqli in php which can explain you the difference.
Try this
<?php
$con= mysql_connect('hostname', 'username', 'password');
if(!$con)
die("not connected". mysql_errno());
echo("Connected");
mysql_select_db("test",$con);
$query="select * from tabale_name";
$result= mysql_query($query,$con) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
echo $row['id']. " - ". $row['name'];
echo "<br />";
}
?>
check this
<?php
$con=mysqli_connect("hostname","username","password","info");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM people");
while($row = mysqli_fetch_array($result))
{
echo $row['id'] . " " . $row['people_name'];
echo "<br>";
}
?>
OR
<?php
$con=mysqli_connect("hostname","username","password");
// Check connection
if ($con)
{
echo "connected to db";
}
else
{
echo "not connected to db";
}
$db_selected = mysql_select_db("info", $con);
if (!$db_selected)
{
die ("Can\'t use info: " . mysql_error());
}
$result = mysqli_query("SELECT * FROM people");
while($row = mysqli_fetch_array($result))
{
echo $row['id'] . " " . $row['people_name'];
echo "<br>";
}
?>

populate dropdown list with database value

I want to populate a drop down list with data from a specific field in the database. Here is my sample code
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("disertation ", $con);
$results = mysql_query("SELECT name FROM user_parent;");
?>
<select name="name">
<option value="name">Select one</option>
<?php
while($row=mysql_fetch_array($results))
{ echo '<option value=" ' . $row['name'] . ' ">' . $row['name'] . '</option>'; }
?>
</select>
It's currently displaying nothing from db, any help?
Try this, some white space in your code mysql_select_db("disertation", $con);
mysql_select_db("disertation", $con);
$results = mysql_query("SELECT name FROM user_parent") or die (mysql_error());
Make your mysql_fetch_array call read:
mysql_fetch_array($results, MYSQL_ASSOC)
Without MYSQL_ASSOC you can't refer to column names in $row.
Also, consider using MYSQLI or PDO. MYSQL is considerably outdated.
I would recommend you to use mysqli instead of mysql
<?php
$con=mysqli_connect("localhost","root","","disertation ");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$resource= mysqli_query($con,"SELECT * FROM user_parent");
echo "<select class="name"><option value="name">Select one</option>";
while($result = mysqli_fetch_array($resource)){
echo '<option value="'.$result["name"].'">'.$result["name"].'</option>';
}
echo "</select>";
mysqli_close($con);
?>
To answer your question, directly, you should be first checking if there are any errors (mysql_error()) and then checking there are some results (mysql_num_rows) - these make for easier debugging of your code, since it will tell you what is wrong.
Try this;
<?php
// Connect with user
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
// Select database
mysql_select_db("disertation", $con);
// Run query
$results = mysql_query("SELECT `name` FROM `user_parent`;") or die (mysql_error());
// Check for no results
if (mysql_num_rows($results) == 0)
{
echo 'There are no options for you to select.';
}
else
{
// If results, loop them.
// If the names are user input, make sure they're displayed in non-raw form
echo '<select name="name">
<option value="name">Select one</option>';
while($row = mysql_fetch_assoc($results))
{
$name = htmlentities($row['name'], ENT_QUOTES, "UTF-8");
echo '<option value=" ' . $name . ' ">' . $name . '</option>';
}
echo '</select>';
}
Will edit with a mysqli_ solution, if that is an option for you, since mysql_ is deprecated and will be dropped from PHP support, sooner or later.
MySQLi solution;
<?php
// Connect to database;
$mysqli = new mysqli("localhost", "my_user", "my_password", "data_base");
if (mysqli_connect_errno())
{
die("Connect failed: " . mysqli_connect_error());
}
$result = $mysqli->query("SELECT `name` FROM `user_parent`");
if ($result->num_rows > 0)
{
echo '<select name="name">
<option value="name">Select one</option>';
while($row = $result->fetch_assoc)
{
$name = htmlentities($row['name'], ENT_QUOTES, "UTF-8");
echo '<option value=" ' . $name . ' ">' . $name . '</option>';
}
echo '</select>';
$result->close();
}
else
{
echo 'There are no options for you to select.';
}

How to display database value in form list

I've created drop down list with value name from the database. When I select the value from the drop down list, other data will appear in other textfield based on the database. The submit process was doing fine except when I check on the list. The drop down list value didn't appear in the list but other data did.
This is my adding form:
<tr><td width="116">Medicine name</td><td width="221">
<center>:
<select name="name" id="name" >
<option>--- Choose Medicine ---</option>
<?php
mysql_connect("localhost", "root", "");
mysql_select_db("arie");
$sql = mysql_query("SELECT * FROM tabelmedicine ORDER BY name ASC ");
if(mysql_num_rows($sql) != 0){
while($row = mysql_fetch_assoc($sql)){
$option_value = $row['priceperunit'] . ',' . $row['stock'];
echo '<option value="'.$option_value.'">'.$row['name'].'</option>';
}
}
?>
</select ></center>
This is a script to display other database value in other textfield when the drop down list is selected:
<script>
var select = document.getElementById('name');
var priceperunit = document.getElementById('priceperunit');
var stock = document.getElementById('stock');
select.onchange = function()
{
var priceperunit_stock = select.value.split(',');
priceperunit.value = priceperunit_stock[0];
stock.value = priceperunit_stock[1];
}
</script>
This is my inserted data into database process:
<?php
$host = "localhost";
$user = "root";
$pass = "";
$db = "arie";
$connect = mysql_connect($host, $user, $pass) or die ('Failed to connect! ');
mysql_select_db($db);
$name=$_POST['name'];
if ($name === "")
{
echo "Please fill all the data";
}
else
{
$query="INSERT INTO `tabelout`(`name`)
VALUES ('$name');";
$result = mysql_query($query) OR die (mysql_error());
echo "You have successfully added new medicine to the database.";
}
?>
This is my list page, where the name didn't show up:
<?php
$con=mysqli_connect("localhost","root","","arie");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM tabelout");
echo "<table border='1'>
<th>name</th>";
while($row = mysqli_fetch_array($result))
{
echo "<td><center>" . $row['name'] . "</center></td>";
}
echo "</table>";
mysqli_close($con);
?>
Make sure your database table has records, If it has records, then change the table structure, Add tr tags where required.
echo "<table border='1'>
<tr><th>name</th></tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr><td><center>" . $row['name'] . "</center></td></tr>";
}
echo "</table>";

comparing two variables using IF/IF..ELSE statement for PHP and inserting into MYSQL

i am doing a count validation on two variables , $row2 with a value of 7 and $row3 with a value of 11. What i want to achieve is that the higher value can only be insert into the DB. The problem now is that $row3 value is bigger than $row2. however it always insert $row2 value into the DB. Is there any wrong with my validation codes?
function tweetCount($hashtag) {
$url = 'http://search.twitter.com/search.atom?q='.urlencode($hashtag).'&rpp=100&result_type=recent';
//echo "<p>Connecting to <strong>$url</strong> ...</p>";
$ch = curl_init($url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, TRUE);
$xml = curl_exec ($ch);
curl_close ($ch);
//If you want to see the response from Twitter, uncomment this next part out:
//echo "<p>Response:</p>";
//echo "<pre>".htmlspecialchars($xml)."</pre>";
$affected = 0;
$twelement = new SimpleXMLElement($xml);
foreach ($twelement->entry as $entry) {
$text = trim($entry->title);
$author = trim($entry->author->name);
$time = strtotime($entry->published);
$id = $entry->id;
//echo count($entry->text);
// echo "<em>Posted ".date('n/j/y g:i a',$time)."</em><p>Tweet from <b><u>".$author."</u></b>: <strong>".$text."</strong> </p>";
//echo "<br/>";
}
//echo count($twelemtnt);
//echo count($entry);
echo $number_of_tweets = count($twelement->entry);
}
on my html table , i echo the data out like this:
<?php echo tweetCount($row[2]); ?>
<input type="hidden" name="row2" value="<?php echo tweetCount($row2[2]);?>" />
<input type="hidden" name="row2Ini" value="<?php echo $row2[1];?>" />
<input type="hidden" name="row2Sch" value="<?php echo $row2[2];?>" />
using a POST form i post it to another page where i need to do a count validation to see if which variable $row2 or $row3 have a higher count value and then i will insert the higher value into the DB
admin.php page
$row2 = $_POST['row2'];
$row2Ini = $_POST['row2Ini'];
$row2Sch = $_POST['row2Sch'];
if ( $row2 > $row3 )
{
echo "<br>row2 is more than row 3";
$con = mysql_connect("localhost","root","password");
if (!$con)
{ die('Could not connect: ' . mysql_error());}
mysql_select_db("schoutweet", $con);
$sql2="INSERT INTO matchTable (schInitial, schName,position)VALUES
('$_POST[row2Ini]','$_POST[row2Sch]','top4')";
if (!mysql_query($sql2,$con)){die('Error: ' . mysql_error());}echo "ROW2 record added!<BR>";
mysql_close($con);
}
else if ($row2 < $row3)
{
echo "row3 count is more than row 2";
$con = mysql_connect("localhost","root","password");
if (!$con)
{ die('Could not connect: ' . mysql_error());}
mysql_select_db("schoutweet", $con);
$sql="INSERT INTO matchTable (schInitial, schName,position)VALUES('$_POST[row3Ini]','$_POST[row3Sch]','top4')";
if (!mysql_query($sql,$con)){die('Error: ' . mysql_error());}echo "ROW3 record added!<BR>";
mysql_close($con);
}
Never write connection twice... You should take connect part out of if..else statement...
Check below if it works...
$con = mysql_connect("localhost","root","password");
if (!$con)
{ die('Could not connect: ' . mysql_error());}
mysql_select_db("schoutweet", $con);
if ( $row2 > $row3 )
{
echo "<br>row2 is more than row 3";
$sql2="INSERT INTO matchTable (schInitial, schName,position)VALUES
('$_POST[row2Ini]','$_POST[row2Sch]','top4')";
if (!mysql_query($sql2,$con)){die('Error: ' . mysql_error());}echo "ROW2 record added!<BR>";
}
else
{
$sql="INSERT INTO matchTable (schInitial, schName,position)VALUES('$_POST[row3Ini]','$_POST[row3Sch]','top4')";
if (!mysql_query($sql,$con)){die('Error: ' . mysql_error());}echo "ROW3 record added!<BR>";
}
mysql_close($con);

Categories