I am trying to post some table rows into a database. I have created a while loop and it keeps posting my first HTML table row to the database. The desired behaviour is that every HTML table row is posted to the DB.
<?PHP
$con=mysqli_connect("localhost","root","","freoplanner");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$count=1;
$shiftDate='dp'.$count;
$shiftTime='shiftTime'.$count;
$shiftAantal='shiftAantal'.$count;
while(isset($_POST[$shiftDate]) && !empty($_POST[$shiftDate])){
$sql="INSERT INTO shifts (datum, tijd, aantal)
VALUES
('$_POST[$shiftDate]','$_POST[$shiftTime]','$_POST[$shiftAantal]')";
$count++;
mysqli_query($con,$sql);
}
header("location:shiftstoevoegen.php");
?>
You need to reinitialise the following inside the loop:
$shiftDate='dp'.$count;
$shiftTime='shiftTime'.$count;
$shiftAantal='shiftAantal'.$count;
currently, they will always be using $count = 1
Related
I have two separate applications that are working with the same database in MYSQL. The first app in Nodejs and modify data in the database, when I want to read data from the second app coded in PHP, the data looks hidden (no result to select). I can see the data in the database, and I can get the result from the same query that I have in PHP. When I am adding data manually, it is accessible from PHP app.
Code in Node.js
const mysql = require('mysql')
const mysqlconnection = mysql.createConnection({
host:'172.16.X.X',
user:'XXX',
password:'XXXX',
database:'ramen'
});
var sql = "INSERT INTO challenge_usersolved (user, challenge_Id, username)
VALUES (?,?,?)";
var values = [challenge.name , challenge.difficulty, usname];
mysqlconnection.query(sql, values,(err, row)=>{
})
PHP side Code is:
<?php
$db = mysqli_connect('172.16.X.X', 'ramen', 'XXX', 'ramen', '3306');
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$query = "SELECT sum(challenge_Id)*100 as score, username FROM (SELECT
Distinct user, challenge_Id, username FROM challenge_usersolved) as T
GROUP BY username;";
$result = mysqli_query($db, $query);
if (!$result) {
echo "Could not successfully run query ($sql) from DB: " .
mysql_error();
}
$rs = mysqli_fetch_assoc($result);
echo "<html><head><title>Score Board</title></head><body>";
echo "<table align='center' border='1px' style='width:300px; line-height: 30px'>";
echo"<tr><th colspan='2'>Students Score</th></tr><t><th>Username</th> <th>Score</th></t>";
while($rows=mysqli_fetch_assoc($result) ){
echo "<tr><td>{$rows["username"]}</td><td>{$rows["score"]}</td></tr>";
}
echo "</body></html>";
?>
Your
$rs = mysqli_fetch_assoc($result);
line loads the first record and you do not do anything with it. When your code arrives to the while loop, the next (second) row is attempted to be loaded, but you have a single row as you described in the comment section. The solution seems to be to remove the
$rs = mysqli_fetch_assoc($result);
line.
I'am trying to insert data from a form into two different tables. Here's what I'am doing:-
<?php
$mysqli= new mysqli("localhost","root","","store_records");
if($mysqli->connect_error)
die("Database connection failed ".$mysqli->connect_error);
$query = "insert into bill_details(date,invoice_no,balance) values('".$_POST['p_date']."','".$_POST['invoice_no']."','".$_POST['balance']."')";
if($mysqli->query($query))
{
$cquery="";
for ( $i=0;$i<$_POST['row_numbers'];$i++)
{
$cquery .= "insert into bill_records(item_name,qty,pack,batch,expiry,mrp,rate,vat,discount,invoice_no) values('".$_POST['item_name'][$i]."','".$_POST['qty'][$i]."','".$_POST['pack'][$i]."','".$_POST['batch'][$i]."','".$_POST['expiry'][$i]."','".$_POST['mrp'][$i]."','".$_POST['rate'][$i]."','".$_POST['vat'][$i]."','".$_POST['discount'][$i]."','".$_POST['invoice_no']."');";
}
if($mysqli->multi_query($cquery))
echo "Records Saved";
else
echo "Failed to save product records";
}
else
{
echo "Failed To save Records";
}
?>
Now, data from the first query is getting stored into bill_details table. but the array values are not getting stored. I cant figure out what am I doing wrong with my code. I wanna know how can i solve this problem and use the invoice_no as reference key for both the tables.
Here are the structure of both the database tables..
bill_details table
bill_records table
Try this. Hope it works. :)
<?php
$mysqli= new mysqli("localhost","root","","store_records");
if($mysqli->connect_error)
die("Database connection failed ".$mysqli->connect_error);
$query = "insert into bill_details(date,invoice_no,balance) values('".$_POST['p_date']."','".$_POST['invoice_no']."','".$_POST['balance']."')";
if($mysqli->query($query))
{
$cquery="";
for ( $i=0;$i<$_POST['row_numbers'];$i++)
{
$cquery .= "insert into bill_records(item_name,qty,pack,batch,expiry,mrp,rate,vat,discount,invoice no) values('".$_POST['item_name'][$i]."','".$_POST['qty'][$i]."','".$_POST['pack'][$i]."','".$_POST['batch'][$i]."','".$_POST['expiry'][$i]."','".$_POST['mrp'][$i]."','".$_POST['rate'][$i]."','".$_POST['vat'][$i]."','".$_POST['discount'][$i]."','".$_POST['invoice_no']."');";
if(!($mysqli->query($cquery)))
die("failed to save");
}
}
else{
echo "Failed To save Records";
}
?>
I have a simple php code which enters value into MySql database and then retrieves it and displays it. However when retrieving it always return null and Empty Set is echoed everytime. Can someone please help.
I am using WAMP Server.
Database name is trial and name of table is People. It has 2 attributes: name and email id
Following is my code:
$con=mysqli_connect("localhost","root","");
if (mysqli_connect_errno())
echo "Failed to connect to MySQL: " . mysqli_connect_error();
mysqli_query($con,"INSERT INTO People VALUES ('xyz', 'abc#zzz.com')");
echo "Insertion Success";
$result = mysqli_query($con,"SELECT * FROM People");
if($result == null)
echo "Empty Set";
else
while($row = mysqli_fetch_array($result))
{
echo $row['name'] . " " . $row['emailid'];
echo "<br>";
}
mysqli_close($con);
?>
You should select a database after using mysqli_connect but before any queries are done:
$con=mysqli_connect("localhost","root","");
if (mysqli_connect_errno())
echo "Failed to connect to MySQL: " . mysqli_connect_error();
mysqli_select_db($con, "databasename");
//query processing here..
You should check if record is inserted change your code to
if( mysqli_query($con,"INSERT INTO People VALUES ('xyz', 'abc#zzz.com')") === FALSE){
echo mysqli_error();
}
first always check if you query executed write by executing it inside if():
if(!mysqli_query("your query"))
{
mysqli_error();
}
second i think your query is not executing because you cant name you table with a capital letter so it should be "people" not "People"
I am trying to select values from one DB. And insert and update the result into another. This is cronjob that needs to run everyday to replicate some data from one DB into another. I know I am missing steps / correct syntax, but I hope someone can help me out.
<?php
$con_1=mysqli_connect("host","user","pw","db");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$con_2=mysqli_connect("host","user","pw","db");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con_1,"SELECT id, name FROM table GROUP BY 1,2");
$mysqli->query($con_2, "INSERT INTO `table2`(`id`, `name`) VALUES ('".$result[1]."', ".$result[2].")
ON DUPLICATE KEY UPDATE name = ".$result[2]."");
}
mysqli_close($con_1);
mysqli_close($con_2);
?>
mysqli_query returns a query object, using $result[1] doesn't make sense, you need to fetch the rows in a loop:
while($row = $result->fetch_assoc()) {
// insert result in second database
}
For other access methods check the documentation.
I need to insert the searched results into a table
i already done with getting the names of any value but its hard for me to INSERT them in the table.
here is some code:
<?php
$con=mysqli_connect("localhost", "root", "", "library");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
//var_dump($_POST['genre']);
$genres = $_POST['genre'];
foreach ($genres as $k=>$v){
$rslt="INSERT INTO 'history'(id,name)
VALUES('NULL', '".$k."')";
}
If your id column is auto-increment you should not put NULL between quotes.
(That would mean you're inserting the string 'NULL' wich is not NULL).
You can find here the doc about inserting rows with an auto_increment value.
Try like this :
"INSERT INTO 'history'(id,name) VALUES(NULL, '".$k."')";
or this also works :
"INSERT INTO 'history'(name) VALUES('".$k."')";