heloo, i want to serarch particular row from mysql database and same should be displayed on html table. I have tried the below code but it only gives the single row which is result of current query, previous row get overwritten. I am fetching a row for particular id,next time when i give another id,another row should be fetched and it should be added to the table next to previous one..
<html>
<body>
<form action="<?php $_PHP_SELF ?>" method="POST">
<tr>
ProductID: <input type="number" name="ProductID">
<input type="submit" value ="Go">
</form>
</tr>
</body>
</html>
<?php
$con=mysqli_connect("localhost","root","m70830807","Inventory");
// Check connection
if (!$con)
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_select_db("Inventory",$con);
$ID=$_POST['ProductID'];
//echo $ID;
$result = mysqli_query($con,"SELECT ProductID, ProductName, UnitPrice FROm Products_Sold where ProductID =" .$ID);
echo"<table border = '1'>
<tr>
<th>ProductID</th>
<th>ProductName</th>
<th>UnitPrice</th>
</tr> ";
//$row=mysqli_fetch_array($result);
//$c1= $row['ProductID'];
//$c2=$row['ProductName'];
//$c3=$row['UnitPrice'];
//echo $c1;
//echo $c2;
//echo $c3;
//$ins= mysqli_query($con,"insert into Temp (ProductID,ProductName,UnitPrice) values ('%d','%s','%f')", $c1,$c2,$c3);
//$fetch=mysqli_query($con,"select ProductId,ProductName,UnitPrice from Temp");
while( $row = mysqli_fetch_array($result))
{ echo "<tr>";
echo "<td>". $row['ProductID'] . "</td>";
echo "<td>" . $row['ProductName'] ." </td>";
echo "<td>" . $row['UnitPrice'] . "</td>";
echo "</tr> ";
}
echo "</table>";
mysqli_close($con);
?>
If you need to do this within a user session (one user has one list. Another got different) so you need to use session
In your example -
<?php
session_start();
$con=mysqli_connect("localhost","root","m70830807","Inventory");
// Check connection
if (!$con)
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_select_db("Inventory",$con);
$ID=$_POST['ProductID'];
$_SESSION['ids'][mysql_escape_string($ID)] = array();
//echo $ID;
$result = mysqli_query($con,"SELECT ProductID, ProductName, UnitPrice FROm Products_Sold where ProductID IN (\"" . implode('","', usort(array_keys( $_SESSION['ids']))). '");');
echo"<table border = '1'>
<tr>
<th>
ProductID
</th>
<th>
ProductName
</th>
<th>
UnitPrice
</th>
</tr>
";
while( $row = mysqli_fetch_array($result)) { echo "
<tr>
"; echo "
<td>
". $row['ProductID'] . "
</td>
"; echo "
<td>
" . $row['ProductName'] ."
</td>
"; echo "
<td>
" . $row['UnitPrice'] . "
</td>
"; echo "
</tr>
"; } echo "
</table>
";
mysqli_close($con);
?>
You need to use ajax and jquery to do this.
send request via ajax and get response as your row. Manipulate the table using jquery to append in the existing set of records
Related
I would like to know how can i put the table from mysql in a new file.php.
I want the MySql table to be on the page.
This is my code that inserts data in MySql.
<?php
// Create connection
$con = mysqli_connect("host", "id_", "password", "xxxxxx");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$Task = $_POST['Task'];
$Date = $_POST['Date'];
$Desc = $_POST['Desc'];
$sql = "INSERT INTO tasklist (Task, Date, Description)
VALUES ('$Task', '$Date', '$Desc')";
if (!mysqli_query($con, $sql)) {
die('Error: ' . mysqli_error($con));
}
echo "1 record added";
mysqli_close($con);
?>
<html>
<body>
<form action="addtask.php" method="post">
Task: <input type="text" name="Task">
Date: <input type="text" id="datepicker" name="Date">
Decrption:<textarea type="text" name="Desc"></textarea>
<input type="submit" value="submit">
</form>
</body>
</html>
Try this code:
<?php
$con=mysqli_connect("example.com","peter","abc123","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM Persons");
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
also u can try w3schools sample code :
Display the Result in an HTML Table
The following example selects the same data as the example above, but will display the data in an HTML table:
<?php
$con=mysqli_connect("example.com","peter","abc123","my_db");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM Persons");
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
The output of the code above will be:
first code comes from "Jonnny" in this article
I am new to PHP and I made a simple program where you can apply your name and age, it will take the data to the database and the table will be added with a new row.
I want to add a new column where you can click "change", only the data from that particular row will show up in a few textboxes and can be changed. when pressing submit I want to use the UPDATE function to update the records.
example/plot:
Mike Towards 23 Change
Tyler Frankenstein 24
Change Sophie Baker 22
Change
I want to change the age of Sophie Baker to 24 so I press Change on that row.
Now I only want to get the data from that row and make some changes.
The code I have this far:
Drawing the table above the input fields and the input:
$result = mysqli_query($con,"SELECT * FROM Persons");
echo "<table border='2'> <tr> <th>Voornaam</th> <th>Achternaam</th> <th>Leeftijd</th></tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "<td>" . $row['Age'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
<html>
<body>
<br />
<form action="insert.php" method="post"><br />
<input type="text" name="firstname"> Firstname <br />
<input type="text" name="lastname"> Lastname <br />
<input type="text" name="age"> Age
<p><input type="submit"></p>
</form>
</body>
</html>
Parser:
<?php
$con = mysqli_connect("localhost", "user" , "", "personInfo");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="INSERT INTO Persons (FirstName, LastName, Age) VALUES ('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "1 record added to the database";
echo "<p><a href=sql2.php>Back to form</a></p>";
mysqli_close($con);
?>
I have tried a few things, but I cant figure out how to show the content on the row I want to select.
Change the actual data with the update function won't be the problem, so I only need help to get the actual data from the correct row.
you'd need to select with the primary key of that table if any exists. if not you should create one. I assume you have a primary key named PersonID:
$query = "SELECT * FROM Persons WHERE PersonID = '" . ($_GET['PersonID']) . "'";
to add the edit button:
echo "<table border='2'> <tr> <th>Voornaam</th> <th>Achternaam</th> <th>Leeftijd</th><th>Action</th></tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "<td>" . $row['Age'] . "</td>";
echo "<td><a href = '?PersonID=" . $row['PersonID'] . "'>Edit</a></td>";
echo "</tr>";
}
echo "</table>";
I assume you have a column named "id".
you can do the following:
<?php
$con = mysqli_connect("localhost", "user" , "", "personInfo");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// when you are in "edit mode" just display the row you will edit row
if (isset($_GET['id'])
$result = mysqli_query($con,"SELECT * FROM Persons where id = ".(int)$_GET['id']);
else
$result = mysqli_query($con,"SELECT * FROM Persons");
echo "<table border='2'> <tr> <th>Voornaam</th> <th>Achternaam</th> <th>Leeftijd</th></tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "<td>" . $row['Age'] . "</td>";
echo "<td><a href='?id=" . $row['id'] . "'>change</a></td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
<html>
<body>
<br />
<form action="update.php" method="post"><br />
<input type="hidden" name="id" value="<?php echo isset($_GET['id']?$_GET['id']:'') ?>" />
<input type="text" name="firstname" value="<?php echo isset($row['FirstName'])?$row['FirstName']:'' ?>"/> Firstname <br />
<input type="text" name="lastname" value="<?php echo isset($row['LastName'])?$row['LastName']:'' ?>"/> Lastname <br />
<input type="text" name="age" value="<?php echo isset($row['Age'])?$row['Age']:'' ?>"/> Age
<p><input type="submit"></p>
</form>
</body>
</html>
update.php (handle both insertion and update):
<?php
$con = mysqli_connect("localhost", "user" , "", "personInfo");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if (isset($_POST['id'])
$sql="UPDATE Persons set FirstName = ?, LastName = ?, Age = ?
WHERE id = ".(int)$_POST['id'];
else
$sql="INSERT INTO Persons (FirstName, LastName, Age) VALUES (?, ?, ?)";
$sth = mysqli_prepare($con, $sql);
$sth->bind_param($_POST[firstname],$_POST[lastname],$_POST[age]);
if (!$sth->execute())
{
die('Error: ' . mysqli_error($con));
}
echo "1 record ".(isset($_POST['id']?'modified':'added')." to the database";
echo "<p><a href=sql2.php>Back to form</a></p>";
hey guys i am working on a small project for my college. it consists of a table which is dynamic(linked to mysql table) and one cell in every record is a dynamic drop down(all are linked to mysql table). and the user has to choose from a list of drop down values for each drop down generated and it can also be left blank. here is the php code for that:
<form class="appnitro" method="post" action="">
<div class="form_description">
<center><h2>NOMINATE ENTRY</h2></center>
<p><center><font size='3'>
<?php
$con=mysqli_connect("localhost","user",pass","db");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$dept = $_POST['department'];
$class = $_POST['class'];
$result = mysqli_query($con,"SELECT * FROM prizemaster");
$result1 = mysqli_query($con,"SELECT * FROM studentmaster WHERE dept='$dept' and class='$class'");
echo "<table border='1'>
<tr>
<th>Prize ID        </th>
<th>Prize Name         </th>
<th>Name             </th>
</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['prizeid'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td><select name='name'>";
echo "<option></option>";
while($drop = mysqli_fetch_array($result1))
{
echo "<option value='".$drop['name']."'>" . $drop['name'] . "</option>";
}
mysqli_data_seek($result1, 0);
echo "</select></td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?></center></font></div>
<p>
<center><button type="submit" formaction="stnomins.php">Nominate</button></center>
</form>
the above is the form and when the nominate button is clicked i want this code to be executed for each and every drop down value selected:
<?php
$con=mysqli_connect("localhost","user","pass","db");
$myname = $_POST['name'];
$sql2="SELECT * FROM studentmaster WHERE name='$myname'";
$result = mysqli_query($con,$sql2)or die(mysqli_error());
$row = mysqli_fetch_array($result);
$mydept=$row['dept'];
$myclass=$row['class'];
$myregno=$row['regno'];
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql1="INSERT INTO studenttransaction (`transid`, `date`, `prizeid`, `regno`, `name`, `class`, `department`, `status`) VALUES ('' , CURRENT_TIMESTAMP() , '', '$myregno', '$myname', '$myclass', '$mydept', '1')";
if (!mysqli_query($con,$sql1))
{
die('Error: ' . mysqli_error($con));
}
mysqli_close($con);
?>
kindly help me out i need to submit it in 2 days. please explain with code because i am new to php and not fully known to it
I have updated both of your html & php code, please have a look :
In form file:
echo "<td><select name='name[]'>"; // added [] here to make it array
echo "<option value=''>Select student</option>"; // added value & option item
These above lines of code have been modified accordingly in the file below
<form class="appnitro" method="post" action="">
<div class="form_description">
<center><h2>NOMINATE ENTRY</h2></center>
<p><center><font size='3'>
<?php
$con=mysqli_connect("localhost","user",pass","db");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$dept = $_POST['department'];
$class = $_POST['class'];
$result = mysqli_query($con,"SELECT * FROM prizemaster");
$result1 = mysqli_query($con,"SELECT * FROM studentmaster WHERE dept='$dept' and class='$class'");
echo "<table border='1'>
<tr>
<th>Prize ID        </th>
<th>Prize Name         </th>
<th>Name             </th>
</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['prizeid'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td><select name='name[]'>";
echo "<option value=''>Select student</option>";
while($drop = mysqli_fetch_array($result1))
{
echo "<option value='".$drop['name']."'>" . $drop['name'] . "</option>";
}
mysqli_data_seek($result1, 0);
echo "</select></td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?></center></font></div>
<p>
<center><button type="submit" name="nominate" formaction="stnomins.php">Nominate</button></center>
</form>
Slightly updated your PHP code. Started a loop for each drop-down of names if button is clicked/submitted.
<?php
$con=mysqli_connect("localhost","user","pass","db");
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if(isset($_POST['nominate'])){
foreach($_POST['name'] as $key => $myname){
//$myname = $_POST['name'];
$sql2="SELECT * FROM studentmaster WHERE name='$myname'";
$result = mysqli_query($con,$sql2)or die(mysqli_error());
if($row = mysqli_fetch_array($result)){
$mydept=$row['dept'];
$myclass=$row['class'];
$myregno=$row['regno'];
$sql1="INSERT INTO studenttransaction (`transid`, `date`, `prizeid`, `regno`, `name`, `class`, `department`, `status`) VALUES ('' , CURRENT_TIMESTAMP() , '', '$myregno', '$myname', '$myclass', '$mydept', '1')";
if (!mysqli_query($con,$sql1)){
die('Error: ' . mysqli_error($con));
}
}//if mysqli_fetch_array condition closed
}// for loop closed
}// if submit button(nominate) closed
mysqli_close($con);
?>
Hope this will work for you.
its working fine. I have another doubt is that how to echo the value of drop down when a drop down value is selected.Anybody who knows the answer of it, kindly provide the solution of it.
I'm still making the orderform, I've been working on the "save changes function".
By this I mean, if people make changes to the table, they can click on a button and it will update the database (where I get my data from).
Link to the image.
"Bestelling toevoegen" is the adding form, if I fill in the form, and click on "Toevoegen" it will display a new record to the table above.
The problem is, I can't seem to save the changes (Wijzigigen opslaan) of "Status".
Here's the full code:
$con=mysqli_connect("localhost","root","","bestelformulier");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM overzicht");
echo "<form method='post'>";
echo "<table align='center' width='700px' border='2'>
<tr>
<th>Ordernr</th>
<th>Klantnaam</th>
<th>Productnaam</th>
<th>ProductID</th>
<th>Status</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
$status = $row['status'];
$ordernr = $row['ordernr'];
echo "<tr>";
echo "<td>" . $row['ordernr'] . "</td>";
echo "<td width='150px'>" . $row['klantnaam'] . "</td>";
echo "<td width='200px'>" . $row['productnaam'] . "</td>";
echo "<td>" . $row['productid'] . "</td>";
echo "<td><select>
<option>" . $row['status'] . "</option>";
if($row['status'] != "Niet besteld")
echo "<option>Niet besteld</option>";
if($row['status'] != "Besteld")
echo "<option>Besteld</option>";
if($row['status'] != "Onderweg naar hoofdlocatie")
echo "<option>Onderweg naar hoofdlocatie</option>";
if($row['status'] != "Onderweg naar vestiging")
echo "<option>Onderweg naar vestiging</option>";
if($row['status'] != "Ontvangen")
echo "<option>Ontvangen</option>";
echo "</select></td>";
echo "</tr>";
}
echo "<tr>";
echo "<td></td><td></td><td></td><td></td>";
echo "<td><input type='submit' name='wijzigen' value='Wijzigingen Opslaan'/></td>";
echo "</tr>";
echo "</table>";
echo "</form>";
if(isset($_POST['wijzigen'])) {
$query = mysqli_query("UPDATE overzicht SET status=$status WHERE ordernr=$ordernr");
}
mysqli_close($con);
//Table Toevoegen
$con=mysqli_connect("localhost","root","","bestelformulier");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
echo "<br/><br/><br/>";
echo "<h5>Bestelling Toevoegen</h5>";
echo "<form method='post'>";
echo "<table width='700px' border='1'>
<tr>
<th>klantnaam</th>
<td><input type='text' name='klantnaam'/></td>
</tr>
<tr>
<th>Productnaam</th>
<td><input type='text' name='productnaam'/></td>
</tr>
<tr>
<th>productid</th>
<td><input type='text' name='productid'/></td>
</tr>
<tr>
<th>Status</th>
<td>
<select name='status'>
<option value='Niet besteld'>Niet besteld</option>
<option value='Besteld'>Besteld</option>
<option value='Onderweg naar hoofdlocatie'>Onderweg naar hoofdlocatie</option>
<option value='Onderweg naar vestiging'>Onderweg naar vestiging</option>
<option value='Ontvangen'>Ontvangen</option>
<select>
</td>
</tr>
<tr>
<td></td>
<td><input type='submit' name='toevoegen' value='Toevoegen'/></td>
</tr>";
echo "</table>";
$klantnaam = $_POST['klantnaam'];
$productnaam = $_POST['productnaam'];
$productid = $_POST['productid'];
$status= $_POST['status'];
if(isset($_POST['toevoegen'])) {
$query = mysqli_query($con,"INSERT INTO overzicht (klantnaam, productnaam, productid, status)
VALUES ('$klantnaam', '$productnaam', '$productid', '$status')");
$current_url = (empty($_SERVER['HTTPS']) ? "http://" : "https://") . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
header ('Location: ' . $current_url);
exit ();
}
echo "</form>";
mysqli_close($con);
Hope I made it all clear for you, if not I will reply as soon as possible.
Thanks in advance!
You are not reading the status from the $_POST variable.
You are reading $status from the database:
$status = $row['status'];
and then you are setting it again
$query = mysqli_query("UPDATE overzicht SET status=$status WHERE ordernr=$ordernr");
So you set it to the same value as it already is.
Add a $status = $_POST['status']; before the query. Also, you are not reading $ordernr from $_POST, so you are always setting the status to the last item read from your database.
You are also missing single quotes in the query, it should be status='$status'. Last, but not least, take a look at prepared statements. Right now your code is vulnarable to SQL injections.
Edit:
You have to rethink the form of your list. Your select elements don't have a name, so it can't be transmitted to the server at all. Also, you have to provide the ID of the order number for each order ... either you make a form for each order (I'd recommend that), or you use arrays for your form element names (for example: <select name="status[$ordernr]">, simplified).
Why in the universe you have assigned $status = $row['status']; and not using $status in your while Loop.
Second you are using single quotes around variables in the insert query, you don't have to do this, else mysqli will take them as string instead of variables, put the variables in double quotes and query in single.
If this doesn't help, try printing the post data. Also you haven't given name to your forms. Please give forms some name and to your form elements as well.
This may help.
Right now I have a table that displays menu items and I want an Admin to be able to delete and edit a particular line item. My delete.php code works correctly if I have the id equal to a particular number, but I want the id to be equal to the id of whichever row the delete button is in. So my question is how do I get that id number? Because what I'm doing now is not correct.
Here is my delete.php file:
<?php
$con = mysql_connect("localhost", "root", "");
if(!$con)
{
die('Could not connect: ' .mysql_error());
}
mysql_select_db("bics_place", $con);
echo "mysql connected";
$myid = $_POST['id'];
$sql = "DELETE FROM menu WHERE id='$myid'";
echo "sql = $sql";
if(!mysql_query($sql, $con))
{
die('Error: ' .mysql_Error());
}
echo "1 record deleted";
header("location:admin_menu.php");
mysql_close($con);
?>
This is the table being made in admin_menu.php
$result = mysql_query("SELECT * FROM menu");
echo "<table border='1' id='menu'>
<form method='post' action='delete.php'>
<tr>
<th> Id </th>
<th> Type </th>
<th> Product Name </th>
<th> Price </th>
<th></th>
<th></th>
</tr>";
while($row = mysql_fetch_assoc($result))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['type'] . "</td>";
echo "<td>" . $row['productName'] . "</td>";
echo "<td>" . $row['price'] . "</td>";
echo "<td>" . '<input type="submit" name="delete" value="Delete">' . "</td>";
echo "<td>" . '<input type="submit" name="edit" value="Edit">' . "</td>";
echo "</tr>";
}
echo "</form>";
echo "</table>";
Take on hidden field in your form.Then button onclick event set id into hidden field before submit.
<script>
function setId(idValue){
document.getElementById('myid').value=idValue;
}
</script>
echo "<table border='1' id='menu'>
<form method='post' action='delete.php'>
<input type="hidden" name="id" id="myid" value="" />
<tr>
<th> Id </th>
<th> Type </th>
<th> Product Name </th>
<th> Price </th>
<th></th>
<th></th>
</tr>";
while($row = mysql_fetch_assoc($result))
{
$myID = $row["id"];
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['type'] . "</td>";
echo "<td>" . $row['productName'] . "</td>";
echo "<td>" . $row['price'] . "</td>";
echo "<td>" . '<input type="submit" name="delete" value="Delete" onClick="setId('.$myID.');">' . "</td>";
echo "<td>" . '<input type="submit" name="edit" value="Edit">' . "</td>";
echo "</tr>";
}
echo "</form>";
echo "</table>";
Add a hidden field - id in the form :
while($row = mysql_fetch_assoc($result)) {
//your <td>'s here
echo '<input type="hidden" name="id" value="{$row[id]}">';
// echoes for form submit
}
Note : mysql_* functions are deprecated. You should use mysqli_ functions instead. Read here