I have a database of mosques with the following fields: mosque_id, mosque_name, prefecture, address, postal_code, website, email1, phone1 . What I want to do is to display the information of the first mosque (mosque_id=1) in a form and when I click "Next", the information of the second mosque (mosque_id=2) will be displayed.
So far, I've been able to use mosque_id to select an entire row and display the information, but I've been struggling to find a way to switch to the next one. I'm very new to PHP and MYSQL, any help would be appreciated.
<?
$mosque_id = 1;
$result = mysql_query("SELECT * FROM Mosque WHERE mosque_id='$mosque_id'");
$row = mysql_fetch_assoc($result);
?>
<form action="insert.php" method="post">
Mosque Name: <?php print('<input type="text" name="firstname" value="' . $row['mosque_name'] . '"/><br>')?>
Prefecture: <?php print('<input type="text" name="firstname" value="' . $row['prefecture'] . '"/><br>')?>
Address: <?php print('<input type="text" value="' . $row['address'] . '"/><br>')?>
Postal Code: <?php print('<input type="text" value="' . $row['postal_code'] . '"/><br>')?>
Website: <?php print('<input type="text" value="' . $row['website'] . '"/><br>')?>
Email 1: <?php print('<input type="text" value="' . $row['email1'] . '"/><br>')?>
Contact number 1: <?php print('<input type="text" value="' . $row['phone1'] . '"/><br>')?>
<input type="button" value="Next Mosque" onclick=""/>
</form>
<?php
$mosque_id = (isset($_GET['mosque_id']) ? intval($_GET['mosque_id']) : 1); // suppose you know that '1' is the first id in your table
$result = mysql_query("SELECT * FROM Mosque WHERE mosque_id>='$mosque_id' limit 2");
$row = mysql_fetch_assoc($result); // this is the row you're gonna display in the form
$row2 = mysql_fetch_assoc($result); // this will tell us about the next row
?>
<form action="insert.php" method="post">
Mosque Name: <?php print('<input type="text" name="firstname" value="' . $row['mosque_name'] . '"/><br>')?>
Prefecture: <?php print('<input type="text" name="firstname" value="' . $row['prefecture'] . '"/><br>')?>
Address: <?php print('<input type="text" value="' . $row['address'] . '"/><br>')?>
Postal Code: <?php print('<input type="text" value="' . $row['postal_code'] . '"/><br>')?>
Website: <?php print('<input type="text" value="' . $row['website'] . '"/><br>')?>
Email 1: <?php print('<input type="text" value="' . $row['email1'] . '"/><br>')?>
Contact number 1: <?php print('<input type="text" value="' . $row['phone1'] . '"/><br>')?>
<input type="button" value="Next Mosque" onclick="window.location = 'path-to-your-page?mosque_id=<?php echo $row2['mosque_id'];"/>
</form>
First: please use mysqli functions or PDO class, not mysql functions, they are deprecated.
Secondly: use long PHP tags
But You are on right track:
Replace Your first row with:
$mosque_id = (isset($_GET['mosque_id']) ? intval($_GET['mosque_id']) : 1);
And in the end
<input type="button" value="Next Mosque" onclick="javascript: document.location = '?mosque_id=<?php echo ($mosque_id + 1);?>"/>
You also need to write some checks after query, if such id exists and actually, if you develop further, you need to check for next mosque_id also, because if you delete some rows, id-s may not be in correct order like 1, 2, 3, 4 but 1, 3, 7, 9
(hint: for next id use WHERE mosque_id > current_id ORDER BY mosque_id DESC LIMIT 1 in MySQL)
Just have a hidden input holding the ID, and increment it when the page loads. Then, when you submit, the ID will be submitted too.
<?
if (isset($_POST['id']) {
$id = $_POST['id'];
}
else {
$id = 1;
}
$result = mysql_query("SELECT * FROM Mosque WHERE mosque_id='$id'");
...
?>
<form action="insert.php" method="post">
...
<input type='hidden' name='id' value='<?php echo $mosque_id++; ?> />
<input value="Next Mosque" />
</form>
And by the way, use PDO. It's very easy to learn and adds a huge level of security to your application.
Related
I have a form where you need to be able to select the mentor, and add a name. The selecting a mentor is working fine, apart from one part: It only shows the last name ('achternaam') and it needs to show the first name ('voornaam') too. What did I do wrong?
<form action="index.php" method="post">
<p>
<label for="mentor">Mentor:</label>
<select name="mentor" id="mentor">
<?php
if ($result->num_rows > 0) {
//output data of each row
while ($mentors = $result->fetch_assoc()) {
echo '<option value="' . $$mentors['voornaam'] . '">' . $mentors['achternaam'] . '</option>';
}
} else {
echo "0 results";
}
?>
</select> <br>
<label for="klasnaam">Klasnaam:</label>
<input type="text" id="klasnaam" name="klasnaam"> <br>
<input type="hidden" name="action" value="addClass" />
<input type="submit" value="Submit">
<?php
?>
</p>
</form>
The <option> element displays what is written between the opening and closing tag. In your current version you only write the last name between the tags.
To fix this, add the first name:
echo '<option value="' . $mentors['voornaam'] . '">' . $mentors['voornaam'] . ' ' . $mentors['achternaam'] . '</option>';
The value attribute is not used for displaying. It is the representation of the data when submitting the form.
Note that I also changed $$mentors['voornaam'] to $mentors['voornaam']. There was a $ too much.
I have two little bits of code doing the work here:
HTML:
<form action="http://mypage.php">
<input type="text" name="AnswerArea" ID="AnswerArea" value="' . $this_answer . '" />
<input type="hidden" name="Q" value="' . $NextQ . '" />
<input type="hidden" name="P" value="' . $quest . '" />
<input type="hidden" name="ref" value="' . $ref . '" /><br><br>
<input class="button" name="submit" type="submit" value="Next" />
</form>
And the PHP:
<?php if ($AStyle != 'None'){
if(array_key_exists('AnswerArea', $_GET)) {
include 'connection.php';
$newanswer = $_GET['AnswerArea'];
$newSQL = 'UPDATE answers SET Q' . $prev . '="' . $newanswer . '" WHERE strRef="' . $ref . '"';
if (mysqli_query($mysqli, $newSQL)) {
echo 'Successful, comment out when ready';
}
mysqli_close($mysqli);
}
}
?>
The intention is that when submit is clicked the Q, P & ref values are in the URL and the new value of $this_answer is written to the database. However, the input submit and AnswerArea variables are appearing in there as well. I know you can't have GET and POST in the same form, so how should I go about doing this?
The AnswerArea variable could be longer than the permissible URL length so I want to avoid it getting passed as an address.
Any help gratefully received!
I am creating a database front end that will populate a table with the records in a the db, putting all values into input boxes so that they can be changed. I want to be able to make changes and have it update just that specific record.
This is what I currently have.
{
if($row["order_number"]===NULL)
{
$on='<input type="number" name="orderNo" />';
} else {$on='<input type="number" name="orderNo" value="'. $row["order_number"] . '" />';}
if($row["name"]===NULL)
{
$cn='<input type="text" name="cusName" />';
}else {$cn='<input type="text" name="cusName" value="'. $row["name"] . '" />';}
if($row["product_type"]===NULL)
{
$pt='<input type="text" name="prodType" />';
} else {$pt='<input type="text" name="prodType" value="'. $row["product_type"] . '" />';}
$id='<input type="number" name="orderID" value="'.$row["order_id"].'"/>';
$tableCode.='<tr><td>' . $on . '</td><td>' . $cn . '</td><td>' . $pt . '</td><td><input type="submit" value="Update" name="'. $row["order_id"]. '" /></td><td hidden>'.$id.'</td></tr>';
}
This code is looped for all results in the table. samples.html.php simply sticks the results in some table tags on a webpage, along with the headers. The order_id is the primary key of the table (actually 2 tables, but I know how to handle that side of it)
Any help in this would be appreciated! Thanks!
OK, as promised, here is what I came up with. Mignt not be the best way, but it seems to work for my relatively small application.
while($row = mysqli_fetch_assoc($result))
{
$on='';
$cn='';
$pt='';
$id='';
if($row["order_number"]===NULL)
{
$on='<input type="number" name="orderNo" />';
} else {$on='<input type="number" name="orderNo" value="'. $row["order_number"] . '" />';}
if($row["name"]===NULL)
{
$cn='<input type="text" name="cusName" />';
}else {$cn='<input type="text" name="cusName" value="'. $row["name"] . '" />';}
if($row["product_type"]===NULL)
{
$pt='<input type="text" name="prodType" />';
} else {$pt='<input type="text" name="prodType" value="'. $row["product_type"] . '" />';}
$id='<input type="number" name="orderID" value="'.$row["order_id"].'"/>';
$tableCode.='<form class="tr" action="?" method="post"><span class="td">' . $on . '</span><span class="td">' . $cn . '</span><span class="td">' . $pt . '</span><span class="td"><input type="submit" value="Update" name="Update" /></span><span hidden class="td_hidden">'.$id.'</span></form>';
}
This way, only the values from the form that I used get submitted, so only that row is updated. I used CSS display:table styling to get it to look like a table, similar to the third answer of this question.
Thanks everyone for their willingness to help! If anyone has ideas for improvements, I am open to them.
I have an ideal search-box which searches item from a given listing. My searching is working fine, the problem is when I enter something in search box to search, it gives me result but my search box gets empty.
For eg If I search "Electronics" in search box, it gives me result of electronics but my search box gets empty. It should be written with "Electronic" when it gives me result.
Probably, I should be using GET method instead is it so?
Here is my code for searching:
<form action="" method="post">
Search: <input type="text" name="term" /><br />
<input type="submit" value="Submit" />
</form>
if (!empty($_REQUEST['term']))
{
$term = mysql_real_escape_string($_REQUEST['term']);
$sql = "SELECT * FROM category WHERE cat_name LIKE '%" . $term . "%' or parent LIKE '%" . $term . "' or cat_status LIKE '%" . $term . "'";
}
$r_query = mysql_query($sql);
if ($r_query > 1)
{
$dynamicList="";
while ($row = mysql_fetch_array($r_query))
{
// $cat_id=;
/*$dynamicList .= '
<img style="border:#666 1px solid;" src="../storeadmin/category/thumbs/' . $row['cat_id'] . '.jpg" width="77" />';*/
echo "<tr bgcolor=''>";
echo "<td>" . $row['cat_id'] . "</td>";
echo "<td><img style='border:#666 1px solid;' width='70' src='http://localhost/jaymin/My%20Store/storeadmin/category/thumbs/". $row['cat_id'].".jpg' /></td>";
//echo "<td>".$dynamicList."</td>";
echo "<td>" . $row['cat_name'] . "</td>";
echo "<td>" . $row['parent'] . "</td>";
echo "<td>" . $row['cat_status'] . "</td>";
echo "<td><a href='categoryylisting_edit.php?id=" . $row['cat_id'] . "'>Edit</a></td>";
echo "<td><a name='delete' href='categoryylisting_edit.php?id=" . $row['cat_id'] . "'>Delete</a></td><tr>";
echo "</tr>";
}
}
else {
echo "Nothing should be displayed";
}
?>
</table>
change your code to
Search: <input type="text" name="term" value="<?php echo #$_REQUEST['term']; ?>" /><br />
instead of
Search: <input type="text" name="term" /><br />
Try this,
<input type="text" name="term" value="<?php if(isset($_POST['term'])){ echo $_POST['term']; } ?>"/>
If you want this in your url use GET instead of POST like,
<form action="" method="get">
Search: <input type="text" name="term" value="<?php if(isset($_GET['term'])){ echo $_GET['term']; } ?>" /><br />
<input type="submit" value="Submit" />
</form>
This is because you are submitting the form and after form submit textbox values disappears. To overcome this try:
<input type="text" name="term" value="<?php if(isset($_POST['term'])){ echo $_POST['term']; } ?>"/>
I don't understand why I can't use my last form in this code. I generated a form using a SELECT list to select the member that I want to update and it works, but I don't know why I can't use datas from this form. Actually, I can't even echo something (see the echo "TEST"; at the end, nothing happens when I submit the form).
<?php $mysqli = new Mysqli("localhost", "root", "", "repertoire"); ?>
<form method="post" action="">
<label>Modifier</label>
<select name='id_modif'>
<?php
$resultat = $mysqli->query("SELECT * FROM annuaire");
while($select = $resultat->fetch_assoc()){
echo "<option value=". $select['id_annuaire'] . ">" . $select['prenom'] . " " . $select['nom'] . "</option>";
}
?>
</select>
<input type ="submit" name="modifier">
</form>
<br>
<?php
if (isset($_POST['modifier'])){
//print_r($_POST);
$resultat = $mysqli->query("SELECT * FROM annuaire WHERE id_annuaire = '$_POST[id_modif]'");
while ($modif = $resultat->fetch_assoc()) {
echo '<form method="post" action="">
<label for="nom">Nom *</label><br>
<input type="text" name="nom" value="' . $modif['nom'] . '"> <br>';
echo '<label for="prenom">prenom *</label><br>
<input type="text" name="prenom" value="' . $modif['prenom'] . '"> <br>';
echo '<label for="telephone">telephone *</label><br>
<input type="text" name="telephone" value="' . $modif['telephone'] . '"> <br>';
echo '<label for="profession">profession *</label><br>
<input type="text" name="profession" value="' . $modif['profession'] . '"> <br>';
echo '<label for="ville">ville *</label><br>
<input type="text" name="ville" value="' . $modif['ville'] . '"> <br>';
echo '<label for="codepostal">codepostal *</label><br>
<input type="text" name="codepostal" value="' . $modif['codepostal'] . '"> <br>';
echo '<label for="adresse">adresse *</label><br>
<textarea name="adresse">' . $modif['adresse'] . '</textarea> <br>';
echo '<label for="date_de_naissance">Date de naissance</label><br>
<input type="date" name="date_de_naissance" value="' . $modif['date_de_naissance'] . '"><br>';
echo '<label for="sexe">sexe</label><br>
<input type="radio" name="sexe" class="sexe" value="m" checked>Homme
<input type="radio" name="sexe" classe="sexe" value="f">Femme<br>';
echo '<label for="description">description *</label><br>
<textarea name="description">' . $modif['description'] . '</textarea> <br>';
echo '<input type="submit" name="valider_modif" value="Modifier"> <br>';
}
if (isset($_POST['valider_modif'])){
echo "TEST";
}
}
?>
Your second if check is inside the other one, so it will only run when both $_POST['modifier'] and $_POST['valider_modif'] are set. But you second form does not send modifier anywhere.
You could add a hidden field to your second form:
<input type="hidden" name="modifier" value="1" />
Or if you don't want to show the second form again, move the second if outside the other.
Also, you should not use $_POST values in SQL queries directly to be safe from SQL injection. A function like mysqli_real_escape_string should be used to escape the value first.
You have 2 forms and you are not closing the 2nd with </form>
Took me a long time to go through your code. Okay. First thing. Try not to echo so much html markup. It just makes you code the clunkiest in the world. From what I gathered, the Modifier button comes up when you click on the first button. What you need to do if you want to see the TEST message is to take it out of the if statement because the Buttons are like an XOR gate. Setting the other unsets the other