Joomla 1.5 Database Query example needed - mysql

I have made a query, using mysql_* way, that select's some values from a table, where row's id is 1. Then I put all those values in variables so I can echo them in to my form. How can I do the same thing using Joomla's database query way ?
Here is an example of my code that working, using mysql_*:
<?php // DATABASE QUERY
$query="SELECT countdown_module, hometeam_header
FROM jos_gm_nextmatch
WHERE id = 1";
$result=mysql_query($query);
// DATABASE VARIABLES
$countdown_module = mysql_result($result,$i,"countdown_module"); ?>
$hometeam_header = mysql_result($result,$i,"hometeam_header"); ?>
<form action="" method="post" name="form">
<input name="countdown_module" value="<?php echo $countdown_module ?>" type="text" />
<input name="hometeam_header" value="<?php echo $hometeam_header ?>" type="text" />
<input name="submit" type="submit" value="UPDATE" />
</form>

OK I found it!!! Here is an example...
<?php // DATABASE QUERY
$db = JFactory::getDBO();
$query="SELECT countdown_module, hometeam_header
FROM jos_gm_nextmatch
WHERE id = 1";
$db->setQuery($query);
$rows = $db->loadObjectList();
$itemrow = $rows[0];
// DATABASE VARIABLES
$countdown_module = $itemrow->countdown_module;
$hometeam_header = $itemrow->hometeam_header; ?>
<form action="" method="post" name="form">
<input name="countdown_module" value="<?php echo $countdown_module ?>" type="text" />
<input name="hometeam_header" value="<?php echo $hometeam_header ?>" type="text" />
<input name="submit" type="submit" value="UPDATE" />
</form>

Related

How to show double quote in input tag?

How to show double quote in input tag ?
my $test is i test "hello"
and input like this
<input name="test" type="text" value="<?PHP echo $test; ?>">
When test code in input show only i test
How can i do for show i test "hello" in input tag ?
Try to use htmlspecialchars or use htmlentities
<input name="test" type="text" value="<?php echo htmlspecialchars($test); ?>">
<input name="test" type="text" value="<?php echo htmlentities($test); ?>">
Or by using PHP echo you can do it
<?php echo '<input name="test" type="text" value="'.$test.'">'; ?>
use html_entity_decode since you're using php like so:
<input name="test" type="text" value="<?PHP echo html_entity_decode($test); ?>">

How can i store data from two page in same id in PHP

I have two pages sample4.php and sample5.php where different data is been passed. I need to store both page value in same id currently it stores in different id.
sample4.php
<form action = "sample5.php" method = "GET">
firstname: <input type = "text" name = "firstname" value="<?php echo #$firstname; ?>" />
lastname: <input type = "text" name = "lastname" value="<?php echo #$lastname; ?>" />
nationality: <input type = "text" name = "nationality" value="<?php echo #$nationality; ?>" />
<input type = "submit" value="next" />
</form>
Here i am passing firstname,lastname and nationality to next page sample5.php where extra two fields is added i need to store all value in same id but it actually stores first page value in one id and second page value in other id.Please help me to store both in same. Thanks in Advance
sample5.php
<?php
$conn = mysql_connect("localhost","root","");
$db = mysql_select_db("registration",$conn);
$sql = "INSERT INTO sampletable (firstname,lastname,nationality,mobileno,gender) VALUES ('". #$_GET['firstname']."','". #$_GET['lastname']. "' ,'". #$_GET['nationality']. "','". #$_POST['mobileno']. "','". #$_POST['gender']. "')";
$rep = mysql_query($sql);
?>
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<form action="sample5.php" method="POST">
<?php if(isset($_GET['id'])) { ?>
<input type="hidden" name="act" value="edit"/>
<input type="hidden" name="id" value="<?php echo $id; ?>">
<?php } else{ ?>
<input type="hidden" name="act" value="add"/>
<?php } ?>
<label> Mobile No </label>
<input type="text" name="mobileno" value="<?php echo #$mobileno; ?>"><br>
<label> Gender</label>
<input type="text" name="gender" value="<?php echo #$gender; ?>"><br>
<input type = "submit" />
</form>
</body>
</html>

Append Select List value to URL

I have a form that is nothing more than a select list that is displaying records from a database and a button. All I want to do is when they select an option and click Submit, it takes them to a page that deletes the record in question. To delete it - I need the tech_id (from the select list) to be appended to the URL. I've done it the way I would with a text field, but that didn't work. Any suggestions?
<form method="post" id="form1" name="form1" action="delete-tech.php?tech_id=<?php echo $_POST['technician']; ?>">
<p>Choose Technician to Delete:
<select name="technician" id="technician" title="technician">
<?php
do {
?>
<option value="<?php echo $row_getTechs['tech_id']?>"><?php echo $row_getTechs['tech_name']?></option>
<?php
} while ($row_getTechs = mysqli_fetch_assoc($getTechs));
$rows = mysqli_num_rows($getTechs);
if($rows > 0) {
mysqli_data_seek($getTechs, 0);
$row_getTechs = mysqli_fetch_assoc($getTechs);
}
?>
</select>
</p>
<p>
<input name="submit" type="submit" id="submit" value="Delete Technician">
</p>
</form>
The error lies in the coding construct.
I have slightly changed the position of a code segment following while statement as follows(i.e. placed it before the do phrase):
<form method="GET" id="form1" name="form1" action="delete-tech.php">
<p>Choose Technician to Delete:
<select name="technician" id="technician" title="technician">
<?php
$rows = mysqli_num_rows($getTechs);
if($rows > 0) {
mysqli_data_seek($getTechs, 0);
$row_getTechs = mysqli_fetch_assoc($getTechs);
do {
?>
<option value="<?php echo $row_getTechs['tech_id']?>"><?php echo $row_getTechs['tech_name']?></option>
<?php
}while ($row_getTechs = mysqli_fetch_assoc($getTechs));
}
?>
</select>
</p>
<p>
<input name="submit" type="submit" id="submit" value="Delete Technician">
</p>
</form>
It should append the information(list-value) from the form to the URL as you desired. Check it out.
As you say it's not working there is another way using JavaScript:
<form method="post" id="form1" name="form1" action="delete-tech.php">
<p>Choose Technician to Delete:
<select name="technician" id="technician" title="technician">
<?php
$rows = mysqli_num_rows($getTechs);
if($rows > 0) {
mysqli_data_seek($getTechs, 0);
$row_getTechs = mysqli_fetch_assoc($getTechs);
do {
?>
<option value="<?php echo $row_getTechs['tech_id']?>"><?php echo $row_getTechs['tech_name']?></option>
<?php
}while ($row_getTechs = mysqli_fetch_assoc($getTechs));
}
?>
</select>
</p>
<p>
<input name="submit" type="button" id="submit" value="Delete Technician" onclick="location.href='delete-tech.php?tech_id='+document.getElementById('technician').value">
</p>
</form>

mySQL query returns only one result

I am trying to get each row in a table to appear as part of a survey. The following code is returning only the first row in the table (so users can see only one question). I've been over and over this and can't see what I'm doing wrong. Would much appreciate any input.
Thank you!
function getQuestions ($dbc) <!--$dbc=database connection--> {
$query = "SELECT * FROM survey_questions" <!--survey_questions=table--> ;
$result = #mysqli_query ($dbc, $query);
while ($row = mysqli_fetch_array ($result, MYSQLI_ASSOC) ) {
$body = $row ['question_body'] <!--question_body=row in table--> ;
echo '
<div class="entry"> <!--user entry form-->
<h3 class="qTitle">'.$body.'</h3>
<form action="index.php" method="post">
<input type="text" name="answer" size="85" />
<input type="submit" value="Submit" name="submit" />
<input type="hidden" name="questionid" value="questionid" />
<input type="hidden" name="submitted" value="1" />
</form>
</div>
';
}
}
First of all you don't need to echo so much using php...
You have to use // or /* */ to comment in PHP and not <!----> cuz that's for HTML
Secondly coming to your code..
Why you are using?
function getQuestions($dbc) //I dont know what this is doing here, why you are wrapping your code in a function???
you can simply write like this (use echo to print out your question):
<?php
$result = mysqli_query($dbc, "SELECT * FROM survey_questions");
while ($row = mysqli_fetch_array ($result) ) {
?>
<div class="entry"> <!--user entry form-->
<h3 class="qTitle"><?php echo $row['whatever']; ?></h3>
<form action="index.php" method="post">
<input type="text" name="answer" size="85" />
<input type="submit" value="Submit" name="submit" />
<input type="hidden" name="questionid" value="questionid" />
<input type="hidden" name="submitted" value="1" />
</form>
</div>
<?php
}
?>
The results are now two entry forms (an improvement) but still not displaying the text of the two rows from the database. Instead of the row text, I get the > character where the text should be. Here is the updated code, adapting the suggestion of #Mr. Alien:
function getQuestions($dbc) {
$result = mysqli_query($dbc, "SELECT * FROM survey_questions");
while ($row = mysqli_fetch_array ($result) ) {
echo '
<div class="entry">
<h3 class="qTitle">'. $row['survey_questions'].'></h3>
<form action="index.php" method="post">
<input type="text" name="answer" size="85" >
<input type="submit" value="Submit" name="submit" >
<input type="hidden" name="questionid" value="questionid" >
<input type="hidden" name="submitted" value="1" >
</form>
</div>
';
}
}

Getting results from database into textarea

I have an edit page that fills the content with the original content from the database, I am using inline php this populates the field with the title, and all the other fields work as well. When i try and fill the textarea using the same method it doesn't work.
All the other fields are varchar except the textarea which is text.
The php is in the value of the form.
require_once('includes/db.inc.php');
$stmt = $mysqli->prepare("SELECT
postID,title,content,author,image
FROM posts where postID = ?");
$stmt->bind_param("i",$_GET['postID']);
$stmt->execute();
$stmt->bind_result($postID,$title,$content,$author,$image);
$stmt->fetch();
$stmt->close();
?>
<section id="createPost">
<form method="post" action="editPost.php">
<fieldset>
<legend>Edit Post: <?php echo $title ?></legend>
<input name="postID" type="hidden" value="<?php echo $postID; ?>">
<label for="titleOfPost">Title of Post:</label><br />
<input type="text" name="titleOfPost" size="82" placeholder="Enter title of post" required value="<?php echo $title ?>"><br />
<label for="bodyOfPost">Content of Post:</label><br />
<textarea cols="60" name="postContent" rows="10" placeholder="HTML tags allowed" value="<?php echo $content ?>"></textarea><br />
<label for="authorOfPost">Author:</label><br />
<input type="text" name="authorOfPost" size="82" placeholder="Author name" required value="<?php echo $author ?>"><br />
<label for="imageOfPost">Image:</label><br />
<input type="text" name="imageOfPost" size="82" placeholder="image" value="<?php echo $image ?>"><br />
<input type="submit" name="newPostBtn" value="EditPost" id="newPostBtn"/>
</fieldset>
</form>
</section><!--end createPost-->
Textarea element doesn't a have property value. Use:
<textarea cols="60" name="postContent" rows="10" placeholder="HTML tags allowed"><?php echo $content ?></textarea>
Textareas aren't populated like other input types. The content goes between the tags (like an anchor tag) not within the opening tag (like an image tag).