Inserting dynamic multiple values in a single column in a table - php

i wish to insert multiple entries in a single column..
inserting requires taking the values from the form in text field..
help me with retrieval..
here is my code for page1..
$result = mysqli_query($con, "SELECT * FROM cse");
while ($row = mysqli_fetch_array($result))
{
echo "<tr><td>" . $row["name"] . "</td>" . "<td>" . $row["bupin"] . "</td>";
echo "<td>" . "<input type='text' name='1quiz1[".$row["bupin"]."]'>". "</td></tr>";
}
echo "</table>";
here is the code for final11.php
if (isset($_POST["1quiz1"]))
{
foreach ($presence as $key => $val)
{
mysqli_query($con,"Insert into cse ");
}
}
echo "Entered successfully..";

if (isset($_POST["1quiz1"])) {
$keys = "";
$values = "";
foreach ($presence as $key => $val) {
$keys .= $key+",";
$values .= $val+",";
}
// remove last commas
$keys = substr($keys, 0,-1);
$values = substr($values, 0,-1);
//insert
mysqli_query($con,"Insert into cse (".$keys.") values (".$values.")");
// you can here check the errors then if you need
}
echo "Entered successfully..";

You don't need to insert anything inside your input name attribute brackets. in other words, you have to make it something like the following:
echo "<td>" . "<input type='text' name='1quiz1[]'>". "</td></tr>";

Related

PHP notice, returning "array array array" instead of values

Finally jumping into some PHP for the first time and I've written this program and i'm stuck. I've searched all over the place for about 2 hours to find a solution.
Basically I'm connecting to my local database and trying to grab all the rows from my 'songs' table, and display them by their names. Instead of getting their names, i'm getting a notice that says "Notice: Array to string conversion in C:\xampp\htdocs\musiclibrary\index.php on line 47"
My current output looks like this:
Title Artist Genre
Array Array Array
Array Array Array
Array
And then my code is...
<?php
// Require configuration file
require_once 'config.php';
// Connect to the database
$db_server = mysqli_connect($db_hostname, $db_username, $db_password);
// Check for database connection error
if(!$db_server)
{
die("Unable to connect to MySQL: " . mysql_error());
}
// Select a database
// The mysqli_select_db() function is used to change the default database for the connection.
mysqli_select_db($db_server, $db_database);
$prompt = array('Story title', 'Time', 'Person');
$prompt = array('Story title', 'Time', 'Person');
// Page title
echo "<h1>My Music Collection</h1>";
// Get music collection
$query = "SELECT * FROM songs";
$result = mysqli_query($db_server, $query);
$rows = mysqli_num_rows($result);
// If rows exist
if($rows > 0)
{
// Create HTML table
echo "<table>";
echo "<tr><th>Title</th><th>Artist</th><th>Genre</th></tr>";
// Loop through each row in the database table
for($j = 0; $j < $rows; $j++)
{
// Build HTML table row
//PROBLEM LIES HERE ON THESE MYSQL_FETCH_ASSOC PARTS
echo "<tr>";
echo "<td>" . mysqli_fetch_assoc($result,$j,'title') . "</td>";
echo "<td>" . mysqli_fetch_assoc($result,$j,'artist') . "</td>";
echo "<td>" . mysqli_fetch_assoc($result,$j,'genre') . "</td>";
echo "</tr>";
}
echo "</table>";
}
// If there are no songs in the database table
else
{
echo "There are currently no songs on file.";
}
?>
Any solutions to output the names of the rows in my database? Thanks!
Use the code below to replace your code
extract the values to an array first
show the values in table row
$values = mysqli_fetch_assoc($result);
echo "<tr>";
echo "<td>" . $values ['title']. "</td>";
echo "<td>" . $values ['artist'] . "</td>";
echo "<td>" . $values ['genre')]. "</td>";
You need to loop the mysqli_fetch_assoc function to count row then loop the row result to get value
Here is the code :
if($rows > 0){
echo "<table>";
echo "<tr><th>Title</th><th>Artist</th><th>Genre</th></tr>";
// Loop through each row in the database table
while($row = $result->mysqli_fetch_assoc()){
echo "<tr>";
foreach($row as $key => $value){
echo "<td>" . $row['title'] . "</td>";
echo "<td>" . $row['artist'] . "</td>";
echo "<td>" . $row['genre'] . "</td>";
}
echo "</tr>";
}
echo "</table>";
}
Hope it helps!
mysqli_fetch_assoc() function accepts only one parameter.
array mysqli_fetch_assoc ( mysqli_result $result )
Correct way to do this would be:
$query = "SELECT * FROM songs";
if ($result = mysqli_query($db_server, $query)) {
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td>" . $row['title'] . "</td>";
echo "<td>" . $row['artist'] . "</td>";
echo "<td>" . $row['genre'] . "</td>";
echo "</tr>";
}
mysqli_free_result($result);
}

how to merge a foreach loop with a while loop to build an html table?

i have two arrays $_SESSION['fff'] which have the quantity and the other is $result which is extracted from mysql database including the name of the product, price and weight, i need to build a table which includes the data from the database and the quantity which is included in the session variable array
$wherein = implode(",", $_SESSION['cart']);
$sql = "select id, item_name, Price, Weight from items where id IN
($wherein)";
$result= mysqli_query($conn, $sql);
echo "<table style='width:100%' border='1' >";
echo "<tr>";
echo "<th> Product Name</th>";
echo "<th>Product Price </th>" ;
echo "<th>Weight </th>" ;
echo "<th>Quantity </th>" ;
echo "</tr>";
$sum = '';
$s= '';
while (($row = mysqli_fetch_array($result, MYSQLI_ASSOC))) &&
foreach($_SESSION['fff'] as $value){
$sum += $row['Price'];
$s += $row['Weight'];
echo "<tr>";
echo "<td>" . $row['item_name'] . "</td>";
echo "<td> $". $row['Price'] . "</td>" ;
echo "<td>". $row['Weight'] ;
echo "<td>".$value."</td>";
}
I have been trying for hours now searching online and trying different ways but couldn't get to a solution. Thank you.
allocate session value to variable like
$session_var = $_SESSION['fff'];
Don`t Use foreach()
Declare variable
$i=0;
while(($row = mysqli_fetch_array($result, MYSQLI_ASSOC)))
{
$sum += $row['Price'];
$s += $row['Weight'];
echo "<tr>";
echo "<td>" . $row['item_name'] . "</td>";
echo "<td> $". $row['Price'] . "</td>" ;
echo "<td>". $row['Weight'] ;
echo "<td>".$session_var[$i]."</td>";
$i++;
}
You can do it like so:
for($i = 0; $row = mysqli_fetch_array($result, MYSQLI_ASSOC); $i++) {
// access rows using $row
// access corresponding session data using $_SESSION['fff'][$i]
}
If your array keys in $_SESSION['fff'] are not numeric, add the following before the loop:
$session = array_values($_SESSION['fff']);
Then access values using $session[$i] within the loop.

Show values with column name in mysqli using php automatically

I need to show all row values in my db table with column names dynamically. I am not really getting a point to do that. :(
Here is what I have done so far- it works for around 100K data. but when the row increases, it shows blank screen
$result = mysqli_query($con, "SELECT *FROM `master_1m1`");
echo "<table border='1'>";
$i = 0;
$khalid == 0;
while ($row = $result->fetch_assoc()) {
$khalid++;
if ($i == 0) {
$i++;
echo "<tr>";
foreach ($row as $key => $value) {
echo "<th>" . $key . "</th>";
}
echo "</tr>";
}
echo "<tr>";
foreach ($row as $value) {
echo "<td>" . $khalid . "</td>";
echo "<td>" . $value . "</td>";
}
echo "</tr>";
}

PHP table creation from multiple SQL tables

I am trying to create PHP table from multiple table from SQL. I am unable to print the information for a record from multiple database table in one row.
Code:
while($row = mysql_fetch_array($info)){
foreach($field as $query){
echo "<td>" . $row[$query] . "</td>" ;
}
echo "<tr>";
}
while($row = mysql_fetch_array($standing)){
foreach($field as $query){
echo "<td>" . $row[$query] . "</td>" ;
}
}
Example my login is NT882, and I have information in "info" and "standing" table, this code prints all the information from "info" table, and then from next line prints the information from "standing" table.
I want to print first 4 columns from "info" table, and next 2 columns from "standing" table in that particular row.
If its, about HTML Table generation from PHP. then try this way.
echo "<table>";
while($row = mysql_fetch_array($info)){
echo "<tr>";
//get fields to $fields
foreach($field as $query){
echo "<td>" . $row[$query] . "</td>" ;
}
echo "</tr>";
}
echo "</table>";
echo "<table>";
while($row = mysql_fetch_array($standing)){
echo "<tr>";
//get fields to $fields
foreach($field as $query){
echo "<td>" . $row[$query] . "</td>" ;
}
echo "</tr>";
}
echo "</table>";

MYSQLi Select query using form input values

I am trying to create a search page that will allow a user to type values into a "Name" and "Location" field of a form, which will then be used in a MYSQLi query.
But the values are not being parsed into the query.
The $_POST is getting the values correctly from the form, and the query will work if a say WHERE Name = 'name1', so the problem seems to be the bind_param is not parsing the value correctly.
Why isn't it working?
$name = $_POST['name'];
$location = $_POST['location'];
if ($result = $mysqli->prepare("SELECT * FROM table WHERE Name = ?")) {
$result->bind_param("s", $name);
$result->execute;
if ($result->num_rows > 0) {
echo "<table>";
echo "<tr>";
echo "<th>Name </td>";
echo "<th>Location</td>";
echo "</tr>";
while ($row = $result->fetch_object()) {
echo "<tr>";
echo "<td>" . $row->Name . "</td>";
echo "<td>" . $row->Location . "</td>";
echo "</tr>";
}
echo "</table>";
}
}
$mysqli->close();

Categories