I want to show all of the data from my MySQL database to this PHP file. However, when I run the code, all of the data shows up, but the first data from the database is not showing up. I wonder is there any mistake in the code?
<?php
session_start();
include "db.php";
echo'<table class="table table-bordered" >';
echo"<tr>";
echo"<td>No.</td>";
echo"<td>Nama Masakan</td>";
echo"<td>Jumlah</td>";
echo"<td>Keterangan</td>";
echo"<td></td>";
echo"<td></td>";
echo"<td></td>";
echo"</tr>";
$query= mysql_query("SELECT * FROM `".$_SESSION["tabel"]."`");
if($row=mysql_fetch_row($query)>0){
$i=1;
while($row = mysql_fetch_array($query))
{
echo"<tr>";
echo"<td>";
echo $i;
echo"</td>";
echo"<td>{$row['nama_masakan']}</td>";
echo"<td><input type='text' name='jumlah[]' id='jumlah$i' disabled='true' value='{$row['jumlah']}'/> </td>";
echo"<td><input type='text' name='keterangan[]' id='keterangan$i' disabled='true' value='{$row['keterangan']}'/></td>";
echo"<td><button type='button' name='edit' id='edit$i' onClick='edit($i)'>Edit</button></td>";
echo"<td><button type='button' name='save' id='save$i' disabled='true' onClick='save($i)'>Save</button></td>";
echo"<td><button type='button' name='delete' id='del$i' onClick='del($i)'>Del</button></td>";
echo"</tr>";
$i++;
}
} else{
echo "Tidak ada Pesanan Makanan";
}
echo"</Table>";
?>
</div>
Obligatory chastisement: don't use the mysql API. It's deprecated and lacks numerous crucial features. Now on to my answer...
Each time you call mysql_fetch_* it retrieves the next row from the result. You call mysql_fetch_row once in your if (which fetches the first row) then again in your while (which fetches the second row) before you do anything with the row you've fetched.
In your if, instead of fetch you should be checking mysql_num_rows to determine whether you have any data:
if (mysql_num_rows($query) > 0)
{
$i=1;
while($row = mysql_fetch_array($query))
{
Once you have that change working, you would be well-served to read up on the mysqli API and begin converting your code.
Related
I have data in my PHPMYADMIN in a few columns and rows.
Via PHP I read the Data out of my SQL Table and print it into a table.
The last of my table is a action button. So the whole table is a echo and within the table there's a html form in the last , but only with a submit button (input type="hidden") but the value should be the "id" out of my SQL table.
Here's the problem. How can I get the id of one row into the value of an input field? . $row["id"]. doesn't work. How can I fix this problem?
This is for a Website where the user can vote a table row up and then with the html form it is sending via http post to another page where it overrides the current number in the database with +1
$sql = "SELECT * FROM votingpoll ORDER BY votecount DESC";
$result = $conn->query($sql);
echo "$id";
echo "<table>";
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr>
<td>
<form action='vote.php' method='post'>
<input type='text' name='id' value='$id'>
<input type='submit' value='VOTE'>
</form>
</td>
</tr>";
}
} else {
echo "0 results";
}
Thank you!!!
You can do $row[id] i.e. leave the quotes off when the array reference is used inside a double quoted string.
$sql = "SELECT * FROM votingpoll ORDER BY votecount DESC";
$result = $conn->query($sql);
// remove this it does not appear to do anything useful here
//echo "$id";
echo "<table>";
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr>
<td>
<form action='vote.php' method='post'>
<input type='text' name='id' value='$row[id]'>
<input type='submit' value='VOTE'>
</form>
</td>
</tr>";
}
} else {
echo "0 results";
}
Or if you prefer you could use
<input type='text' name='id' value='{$row['id']}'>
It looks like you aren't setting $id anywhere. Try setting it from the results like $id = $row["id"];
Full example:
while($row = $result->fetch_assoc()) {
$id = $row["id"];
echo "<tr>
<td>
<form action='vote.php' method='post'>
<input type='text' name='id' value='$id'>
<input type='submit' value='VOTE'>
</form>
</td>
</tr>";
}
My query is not working in deleteartikel.php, it doesn't delete anything out of the database.
In form.php I made a delete button but it doesn't delete the row itself. I can't find the mistake.
form.php
<?php
include 'dbconnect.php';
$sql = "SELECT * FROM artikel";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>".$row['Productcode']."</td>";
echo "<td>".$row['Product']."</td>";
echo "<td>".$row['Type']."</td>";
echo "<td>".$row['Fabriekcode']."</td>";
echo "<td>".$row['Inkoopprijs']."</td>";
echo "<td>".$row['Verkoopprijs']."</td>";
echo "<td><form action='deleteartikel.php' method='post'>
<input type='submit' name='delete' value='delete'>
</form></td>";
}
} else {
echo "0 results";
}
$conn->close();
?>
</tbody>
</table>
</div>
<!-- Footer navigatie -->
<footer>©ToolsForEver 2017 alle rechten voorbehouden.</footer>
</body>
</html>
deleteartikel.php
<?php
if(isset($_POST['delete'])) {
include('dbconnect.php');
$productcode = $_POST['Productcode'];
$conn->query("DELETE FROM artikel WHERE Productcode = '$productcode'");
header('Location: artikel.php');
}
?>
You never post the product code so the query fails, there is no value sent. You have to add a hidden field to your form:
echo "<form action='deleteartikel.php' method='post'>
<input type='hidden' name='Productcode' value='". $row['Productcode'] ."'>
<input type='submit' name='delete' value='delete'>
</form>";
Little Bobby says your script is at risk for SQL Injection Attacks. Learn about prepared statements for MySQLi. Even escaping the string is not safe! Don't believe it?
In addition, as Fred pointed out above, a form cannot be the child of a table cell, so you need to refactor your code such that you have a form containing a table for each row.
Error checking in deleteartikel.php would have revealed the issue with the missing value for your query. Without error checking in the script you can find the errors in your web server's error logs.
i've a situation where i've to show forms just like excel sheet and then against each row there is a submit button ,Once submit button is clicked data is saved against that row , row is disabled and focus is set to next line automatically
see my screenshot what i've
i'm creating form using this code
<table border="1" cellspacing="5">
<?php
for ($index = 0; $index <4; $index++) {
echo "<form method='post' action='' id='myform'>";
echo"<tr>";
echo"<td><input type='text' name='name$index'></td>";
echo"<td><input type='text' name='email$index'></td>";
echo"<td><input type='submit' name='submit$index'></td>";
echo"</tr>";
echo "</form>";
}
?>
</table>
when i enter data in first form i'm able to get it and save in database but i really not getting how can i go to second line and then process it , please help me about it as i am new and not sure if i am using right approach to do it
I think you should assign to your submit button for each row the same name and a value related to the id eg: the index
<table border="1" cellspacing="5">
<?php
for ($index = 0; $index <4; $index++) {
echo "<form method='post' action='' id='myform'>";
echo"<tr>";
echo"<td><input type='text' name='name$index'></td>";
echo"<td><input type='text' name='email$index'></td>";
echo"<td><input type='submit' name='my_action' value="$index"></td>";
echo"</tr>";
echo "</form>";
}
?>
</table>
In your Server myRelatedAction.php you can easily obtain the index submitted
$my_index = $_POST['my_action'] ;
heloo i have an ajx call function which brings information from a dropdown populated into a table with text inputs by ajax.
i was wondering if there was anyway that i could update the record in the database by using these text fields and the UPDATE function i am relativity new and the internet didnt bring much to light.
i have a button appearing in this table from a drop down but as far as i am aware you cannot use forms within php and the page this would have been submitted from is already submitting a php function and 2 can not be submitted at once.
i was wondering if it was possible that when the data in the textboxes below is changed when the user clicks the button those details are updated in the database?
im new to ajax and php so help would be amazing.
ps. i know this isnt secure i want it to be functional first and before it goes live i will secure it.
here is the code:
<?php
$q = $_GET['q'];
$con = mysqli_connect('server','uid','pwd','dbname');
if (!$con)
{
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"account.php");
$sql="SELECT * FROM account WHERE name = '".$q."'";
$result = mysqli_query($con,$sql);
echo "<table border='1'>
<tr>
<th>Your Name</th>
<th>Your Email</th>
<th>Your Password</th>
<th>Your User Level</th>
<th>Save Changes</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td> <input type='text' name='txt_yourname' id='txt_yourname' value='" .$row['name']."' required='required' /> </td>";
echo "<td> <input type='text' name='txt_email' id='txt_email' value='" .$row['email']."' required='required' /> </td>";
echo "<td> <input type='text' name='txt_password' id='txt_password' value='" .$row['password']."' required='required' /> </td>";
echo "<td> <input type='text' name='txt_userLevel' id='txt_userLevel' value='" .$row['user_level']."' required='required' /> </td>";
echo "<td> <input type='button' name='btn_user' id='txt_user' type='submit' value='cheese'/> </td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
These statements execute user input, opening you up to a SQL Injection attack. You'll want to not do this.
$q = $_GET['q'];
$sql="SELECT * FROM account WHERE name = '".$q."'";
$result = mysqli_query($con,$sql);
To answer your question, in order to update a row like you want you'll need a way to identify in the database the row that has changed. One way to do it would be to include the row's primary key as a field/attribute in the HTML table row, but I'll leave it to someone more well versed in this area to say whether that's a good idea.
You're also going to want to escape and check the type of all of the fields the user can input when you go to do the update.
Hey Guys, I have a question for you.
Imagine that I wanted to be able to keep track of how many miles I've ran every week, so that I could
compare it to the goals I've set for each week. So i've created this table by the use of mysql_fetch_row.
$result=mysql_query("SELECT * FROM randomtable ORDER BY week ASC");
echo "<Table id='result' cellspacing='0'>
<tr class='toprow'>
<th>Week</th>
<th>Goal</th>
<th>Actual Number of Miles</th>
</tr>";
while($row = mysql_fetch_row($result))
{
echo "<tr class='standardrow'>";
echo "<td>$row[0]</td>";
echo "<td>$row[1]</td>";
echo "<td><form><input method='post' type='number'></form></td>";
echo "</tr>";
}
echo "</table>";
This piece of code resultet in a table with 10 weeks with 10 goals - and a column for the actual number of miles. This column should include 10 input forms where the actual number of miles can be submitted. But how do I relate the input from the submit form to the row in which the submit form is positioned?
The primary key is the week - so this would be the one to relate to.
Hope you understand what my problem is:)
To do this you would use a hidden input field.
When you echo each row, and the form in that row, you would simply add an extra line:
`<input type="hidden" name="row_id" value="' . $row['id_column'] . '" />';
In full, your code would be:
$result=mysql_query("SELECT * FROM randomtable ORDER BY week ASC");
echo "<Table id='result' cellspacing='0'>
<tr class='toprow'>
<th>Week</th>
<th>Goal</th>
<th>Actual Number of Miles</th>
</tr>";
while($row = mysql_fetch_row($result))
{
echo "<tr class='standardrow'>";
echo "<td>$row[0]</td>";
echo "<td>$row[1]</td>";
echo "<td>
<form>
<input method='post' type='number'>
<input type='hidden' name='row_id' value='" . $row['id_column'] . "' />
</form>
</td>";
echo "</tr>";
}
echo "</table>";
I think there should be some modifications that has to be done in loop.
echo "<td><form method='post'><input type='number' value='".$rows['col_name']."'><input type='submit' ></form></td>";
This code adds a submit button to each row. But, this shouldn't be what I think.
It should be rather this way,
echo "<form method='post'> ";
while($row = mysql_fetch_row($result))
{
echo "<tr class='standardrow'>";
echo "<td>$row[0]</td>";
echo "<td>$row[1]</td>";
echo "<td><input type='number' value='".$rows['col_name']."'></td>";
echo "</tr>";
}
echo "<input type='submit' ></form>";
Or make the input field look like this.
'<input type="text" name="row['.$row['id_column'].'][miles]" />';
It will return you an array when you post it.
foreach($_POST['row'] as $key => $value){
// $key is your primary key
// $value['miles'] is your input value
}