I have retrieved the database details from a database to a
php page. i have actually retrieved a specific column of a query.
but i am not able to add the radio buttons to the retrieved values.
Following is my coding:
<?php
$query = "SELECT url FROM measurementurl";
$result = mysql_query($query);
while($row = mysql_fetch_row($result))
{
$url = $row[0];
echo "url :$url <br>" ;
}
?>
Try this:
<form action="">
<?php
$query = "SELECT url FROM measurementurl";
$result = mysql_query($query);
while($row = mysql_fetch_row($result))
{
//$url = $row[0]; removed cause not used in code
echo "<input type=\"radio\" name=\"url\" value=\"$row[0]\" />$row[0]<br />";
}
?>
</form>
Related
I am trying to create a functioning search field using ProductID, then using this to display the row in the product table where the ProductID exists. The problem I am having is that if I have the code as follows: The submit button below executes the $deleteprod and $deltesale code which all it should (from my understanding) is submit the ProductID variable.
<form action="delete.php" method="post">
<input value="" placeholder="Product ID" name="ProductID" type="text"/> <br>
<input type="submit" onclick="" value="Search"/
</form>
<?php
$ProductID = $_POST['ProductID'];
$db = mysql_connect("localhost:3307", "root", "usbw");
$deleteprod = mysql_query("DELETE FROM gameshop.product WHERE (product.ProductID = '$ProductID')");
$deletesale = mysql_query("DELETE FROM gameshop.sale WHERE (sale.ProductID = '$ProductID')");
$deletebutton = "<button onclick=\"$deletesale; $deleteprod; location.href='database.php'\" style='width:200px; background-color:red;'>Delete Row</button>";
mysql_select_db("gameshop",$db);
$result = mysql_query("SELECT product.*, sale.Price
FROM gameshop.product, gameshop.sale
WHERE (product.ProductID = '$ProductID' AND sale.ProductID = '$ProductID')",$db);
if (!$result){
print mysql_rror();
}
elseif ($myrow = mysql_fetch_array($result)){
echo "<table style='text-align:center;' border=1>\n";
echo "<tr><td>Product ID</td><td>Product Name</td>", "<td>Publisher</td><td>Developer</td>", "<td>Release Date</td><td>Stock</td>", "<td>Console</td> <td>Rank</td><td>Price</td><td>Delete?</td></tr>\n";
do{
printf("<tr><td>%4d</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%1d</td><td>%s</td><td>%1d</td><td>%2d</td><td>%s</td></tr>\n", // %s are syntax (string)
$myrow["ProductID"], $myrow["ProductName"], $myrow["Publisher"], $myrow["Developer"], $myrow["ReleaseDate"], $myrow["Stock"], $myrow["Console"], $myrow["Rank"], $myrow["Price"], $deletebutton);
}
while ($myrow = mysql_fetch_array($result));
echo "</table>\n";
}
mysql_close($db);
?>
If I were to move the $db = MySQL_connect("localhost:3307", "root", "usbw"); to below the $delete variables, this problem does not occur and it creates the single row table. However when attempting to click on the $deletebutton, the code does not execute and no row is deleted.
What am I doing wrong?
the problem is, when you click the button you call JavaScript, not PHP.
I think you could solve it making another PHP page with your deletesale and deleteprod which takes Product_ID with post.
so this file would have a php script like this:
$ProductID = $_POST['ProductID'];
$db = mysql_connect("localhost:3307", "root", "usbw");
mysql_select_db("gameshop",$db);
$result = mysql_query("SELECT product.*, sale.Price
FROM gameshop.product, gameshop.sale
WHERE (product.ProductID = '$ProductID' AND sale.ProductID = '$ProductID')",$db);
if (!$result){
print mysql_error();
}
elseif ($myrow = mysql_fetch_array($result)){
echo "<table style='text-align:center;' border=1>\n";
echo "<tr><td>Product ID</td><td>Product Name</td>", "<td>Publisher</td> <td>Developer</td>", "<td>Release Date</td><td>Stock</td>", "<td>Console</td> <td>Rank</td><td>Price</td><td>Delete?</td></tr>\n";
do{
printf("<tr><td>%4d</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%1d</td><td>%s</td><td>%1d</td><td>%2d</td><td>",$myrow["ProductID"], $myrow["ProductName"], $myrow["Publisher"], $myrow["Developer"], $myrow["ReleaseDate"], $myrow["Stock"], $myrow["Console"], $myrow["Rank"], $myrow["Price"]);
printf "<form method=\"post\" action=\"delete_page.php\"><input type=\"text\" style=\"display:none\" name=\"ProductID\" value=\"$ProductID\"/><input type=\"submit\" value=\"delete product\"/></form>"
printf "</td></tr>"
}
while ($myrow = mysql_fetch_array($result));
echo "</table>\n";
}
mysql_close($db);
and then you would have delete_page.php with
$ProductID = $_POST['ProductID'];
$db = mysql_connect("localhost:3307", "root", "usbw");
mysql_select_db("gameshop",$db);
mysql_query("DELETE FROM gameshop.product WHERE (product.ProductID = '$ProductID')");
mysql_query("DELETE FROM gameshop.sale WHERE (sale.ProductID = '$ProductID')");
mysql_close($db);
then you can add a link back to the main page.
Be sure to check for typos :)
I have managed to create dropdown from Mysql column and also get query result with get method but here webpage is directed to other page when hit button.
I am looking to get query output with some default option set in dropdown when page loads or want query result on same page reloading it again when user changes option.
Any help will be appreciated.
Code on main page for dropdown:
$result = $conn->query("SELECT DISTINCT nx_version FROM workflow1 ORDER BY id");
echo "<form action='process.php' method='get'>";
echo "<html>";
echo "<body>";
echo "<p></p>";
echo "<center>";
echo "<strong> Select Base Verison To Compare With : </strong>";
echo "<select name=nx_version>";
while ($row = $result->fetch_assoc()) {
unset($nx_version);
$nx_version = $row['nx_version'];
echo '<option value>'.$nx_version.'</option>';
}
echo "</select>";
echo " <button type='submit'>See items</button>";
echo "</center>";
echo "</body>";
echo "</html>";
echo "<p></p>";
echo "<form>";
Code I wrote when hit button and gives query output (in process.php):
$nx_version = $_GET['nx_version']; // The name attribute of the select
$query = "SELECT step1 FROM workflow1 WHERE nx_version = '$nx_version' ORDER BY id DESC";
$query1 = mysqli_query($conn, $query);
$array = Array();
while($result1 = $query1->fetch_assoc()) {
$array[] = $result1['step1'];
}
print_r($array);
process.php file should be like this -
<?php
session_start();
$nx_version = $_GET['nx_version']; // The name attribute of the select
$query = "SELECT step1 FROM workflow1 WHERE nx_version = '$nx_version' ORDER BY id DESC";
$query1 = mysqli_query($conn, $query);
$array = Array();
while($result1 = $query1->fetch_assoc()){
$array[] = $result1['step1'];
}
$_SESSION['data'] = $array;
// storing the data as session
header("location:main_page.php");
?>
Now get back the data from session in your main page by adding this-
$array = $_SESSION['data'];
I have a DB name askadoc , where people can ask about their problem. I want to show all question(or you can say the titles) together in a page. And then when user click on a question, the question will show in a different page with it's comments/details. To do this i have tried the below code and its working perfectly. but how can i do it without using button ?
<?php
$comment = "SELECT * FROM `askadoc` ";
$result = mysqli_query($conn, $comment);
$question = "";
$id="";
if(mysqli_num_rows($result)>0){
while($row = mysqli_fetch_assoc($result)) {
$id = $row["id"];
$question = $row["question"];
?>
<form action="post.php" method="post">
<?php
echo $id;
echo "<input type='hidden' name = 'id' value = ".$id.">";
echo "<button>".$question."</button>";
echo "<br>";
?>
</form>
<?php
}
}
?>
I think you don't need form this. You can user anchor like this.
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$id = $row["id"];
$question = $row["question"];
?>
<?php echo $question ?>
<?php
}
}
Now when you click this anchor, you get question id inside post.php, so easily you can display a particular question comments.
You can use anchor tag then pass the question id to post.php
if(mysqli_num_rows($result)>0){
while($row = mysqli_fetch_assoc($result)) {
$id = $row["id"];
$question = $row["question"];
?>
<?php
echo "<a href='post.php?question_id=".$id."' target='_blank'>".$question."</a>";
echo "<br>";
?>
<?php
}
}
Then to get the value of question_id use $_GET['question_id'];
$questionId = $_GET['question_id']
and its better to check if question_id does exist.
$questionID = isset($_GET['question_id'])? $_GET['question_id'] : '';
if(!empty($questionID)){
//Use $questionID here to query some data from database
}
The users of my website can subscribe some of their animals for a competition.
My code works perfectly but there is a big issue. When a user presses subscribe the page reloads but because the isset is after the echo of the button itself the page needs to be refreshed before the buttontext changes into "subscribed".
Change the order gives issues as you probably can see.
Who can help me out? I'm out of options myself. (I translated the variables etc.)
<form action="" method="post" name="frmSubscribe">
<?php
$Counter = 0;
$sql = "Select * from Animals where username='".$_SESSION['User']."' ";
$result = mysql_query($sql);
while($row=mysql_fetch_array($result)){
echo $row['Animalname'];
$Duiven[] = $row['AnimalID'];
$Username[] = $row['username'];
?>
<input type='submit' <?php echo "name= ".$Buttons[$Counter].""; ?> value='<?php
$sqlSubscribed = "SELECT * FROM Competitionresults WHERE AnimalID='".$Animals[$Counter]."'";
$resultSubscribed = mysql_query($sqlSubscribed);
if(mysql_num_rows($resultSubscribed) == 0){ echo "Subscribe";}
else {echo "deregister";}
?>'><br/><?php
$Teller++;
}
if (isset($_POST['btnSubscribe1'])){
$sqlCheck = "SELECT * FROM Competitionresults WHERE AnimalID='".$Animals[0]."'";
$resultCheck = mysql_query($sqlCheck);
if (mysql_num_rows($resultCheck) == 0){
$sql1 = "INSERT INTO Competitionresuls (AnimalID, username) VALUES ('".$Animals[0]."','".$Username[0]."')";
$result1 = mysql_query($sql1);
$row=mysql_fetch_array($result1);
?><?php
}
}
... and so on for the next animals.
You have to use output buffering I think. Put ob_start() at the beginning of your page, now output any placeholder instead of real input code:
[SUMBIT]
instead of:
<input type='submit' ....
At the end of page you get your buffered content:
$content = ob_get_clean()
And replace submit button with correct code:
$content = str_replace('[SUBMIT]', 'Your actual submit button code here...', $content);
And now output content:
echo $content;
quick fix can be javascript
echo '<span id="someid" >Subscribe</span>';
and when changing
echo "<script>document.getElementById('someid').innerHTML = 'Subscribed';</script>";
in future I STRONGLY suggest you to use PHP MVC Framework
You can use your post code above the page then loaded your page....
Please use like below code:
<?php
if (isset($_POST['btnSubscribe1'])){
$sqlCheck = "SELECT * FROM Competitionresults WHERE AnimalID='".$Animals[0]."'";
$resultCheck = mysql_query($sqlCheck);
if (mysql_num_rows($resultCheck) == 0){
$sql1 = "INSERT INTO Competitionresuls (AnimalID, username) VALUES ('".$Animals[0]."','".$Username[0]."')";
mysql_query($sql1);
header('Location: samefilename.php');
die;
}
}
?>
<form action="" method="post" name="frmSubscribe">
<?php
$Counter = 0;
$sql = "Select * from Animals where username='".$_SESSION['User']."' ";
$result = mysql_query($sql);
while($row=mysql_fetch_array($result)){
echo $row['Animalname'];
$Duiven[] = $row['AnimalID'];
$Username[] = $row['username'];
?>
<input type='submit' <?php echo "name= ".$Buttons[$Counter].""; ?> value='<?php
$sqlSubscribed = "SELECT * FROM Competitionresults WHERE AnimalID='".$Animals[$Counter]."'";
$resultSubscribed = mysql_query($sqlSubscribed);
if(mysql_num_rows($resultSubscribed) == 0){ echo "Subscribe";}
else {echo "deregister";}
?>'><br/><?php
$Teller++;
}
}
most easy solution move the isset up to the top. just so you know actions should always be the first things you handle after that you are gonna work on the view
<form action="" method="post" name="frmSubscribe">
<?php
if (isset($_POST['btnSubscribe1'])){
$sqlCheck = "SELECT * FROM Competitionresults WHERE AnimalID='".key($_GET)."'";
$resultCheck = mysql_query($sqlCheck);
if (mysql_num_rows($resultCheck) == 0){
$sql1 = "INSERT INTO Competitionresuls (AnimalID, username) VALUES ('".key($_GET)."','".$_SESSION['User']."')";
$result1 = mysql_query($sql1);
$row=mysql_fetch_array($result1);
}
}
$Counter = 0;
$sql = "Select * from Animals where username='".$_SESSION['User']."' ";
$result = mysql_query($sql);
while($row=mysql_fetch_array($result)){
echo $row['Animalname'];
$Duiven[] = $row['AnimalID'];
$Username[] = $row['username'];
?>
<input type='submit' <?php echo "name= ".$Buttons[$Counter].""; ?> value='<?php
$sqlSubscribed = "SELECT * FROM Competitionresults WHERE AnimalID='".$Animals[$Counter]."'";
$resultSubscribed = mysql_query($sqlSubscribed);
if(mysql_num_rows($resultSubscribed) == 0){ echo "Subscribe";}
else {echo "deregister";}
?>'><br/><?php
$Teller++;
}
I have a large database of venues - and I would like to display this data in one page that would only change in some sort of an attribute to the id: (Ex: venues.php?id=1, which would get all the data from row #1.)
Edit: Okay, I updated the code and this is what it looks like now:
<?php
mysql_connect("localhost", "", "") or die(mysql_error());
mysql_select_db("") or die(mysql_error());
$id = (int) $_GET['id'];
$data = mysql_query("SELECT * FROM venues WHERE id = ".(int)$id) ;
or die(mysql_error());
Print "<table border cellpadding=3>";
while($info = mysql_fetch_array( $data ))
{
Print "<tr>";
Print "<th>Name:</th> <td>".$info['VENUE_NAME'] . "</td> ";
Print "<th>Address:</th> <td>".$info['ADDRESS'] . " </td></tr>";
}
Print "</table>";
?>
And upon going to venues.php?id=1 I get this error:
Parse error: syntax error, unexpected T_LOGICAL_OR in
/home/nightl7/public_html/demos/venues/venues.php on line 8
Do you mean something like:
$id = (int) $_GET['id'];
$data = mysql_query("SELECT * FROM venues WHERE id = ".(int)$id) ;
In order to "pass" the id into your url "venues.php?id=1"
You need to use a hybrid html/php form with method=get.
You can see an example html form here: w3schools html forms
This is what I would do:
print '<form name="input" action="venues.php" method="get">';
print 'Venue: <select name = "id">';
$con = mysql_connect("","","");
mysql_select_db($dataBase);
if (!$con){die('Could not connect: ' . mysql_error());}
else {
$opt = array();
$optVal = array();
$i = 0;
$sql = "Select * from venues";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result))
{
$opt[$i] = $row['VenueName'];
$optVal[$i] = $row['VenueID'];
print "<option value='$optVal[$i]'>$opt[$i]</option>";
$i++;
}
}
mysql_close($con);
print '</select><br />';
print '<input type="submit" value="Submit" />';
print '</form>'
This will give you a form that will give you a drop down list of all your venues and once a venue is selected will direct you to the venues.php page with the respective id.
at the top of your venues,php page just use
$id = $_GET['id'];
This assigns the id number to the variable $id and then you can use this "select"
$data = mysql_query("SELECT * FROM venues WHERE id = ".$id) ;
To collect your venue name from your database using the id supplied in the form.
Good Luck :)
<?php
mysql_connect("localhost", "", "") or die(mysql_error());
mysql_select_db("") or die(mysql_error());
$id = (int) $_GET['id'];
$data = mysql_query("SELECT * FROM venues WHERE id = ".$id) or die(mysql_error());
Print "<table border cellpadding=3>";
while($info = mysql_fetch_array( $data ))
{
Print "<tr>";
Print "<th>Name:</th> <td>".$info['VENUE_NAME'] . "</td> ";
Print "<th>Address:</th> <td>".$info['ADDRESS'] . " </td></tr>";
}
Print "</table>";
?>