column doesnt match when its empty - php

How should i solve this? there is not a row 1 and i cant insert or update data. Unique keys are indeed ip and uid in my table.
My code is as follows:
//Retrive listeners per reload
echo 'Data Höganäs <br>';
$sc="http://USER:PWD#SUB.SERVER.se:10000/admin.cgi?sid=1&mode=viewxml&page=3";
$xml2 = simplexml_load_file($sc);
foreach ($xml2->LISTENERS->LISTENER as $listener2) {
// Create connection
$conn = new mysqli($host, $user, $pwd, $db);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = " insert INTO hoganaskey
(ip, uid, tid, starttid, date)
VALUES
('$listener2->HOSTNAME', '$listener2->UID', '$listener2->CONNECTTIME', '$listener2->USERAGENT', '$starttid', '$date')
on duplicate key
update tid='$listener2->CONNECTTIME' ";
//$sql = "INSERT INTO hoganaskey (ip, uid, tid, ua, starttid, date) VALUES('$listener2->HOSTNAME', '$listener2->UID', '$listener2->CONNECTTIME', '$listener2->USERAGENT' '$starttid', '$date') ON DUPLICATE KEY UPDATE tid='$listener2->CONNECTTIME' ";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
} //End foreach SHOUcast listener
Im returning this error in insert "Column count doesn't match value count at row 1".

In your insert query, you listed only 5 field names:
(ip, uid, tid, starttid, date)
but you are passing 6 values not 5:
('$listener2->HOSTNAME', '$listener2->UID', '$listener2->CONNECTTIME', '$listener2->USERAGENT', '$starttid', '$date')

Related

Insert into two table foreign key and primary key

I have two tables in my database namely reservations and guests. The rows are as follow:
Reservation:
Reservation id
Property Id
Checkin date
Checkout date
Price
Guest
Guest id
Reservation id
Guest name
Guest address
Using a form that contains the reservation details and the guest details at the same time.
How can I run the insert query so that the auto generated reservation id(auto-increment in database) can also be inserted into the guest table at the same time?
This is what I have so far. I changed my code using last_insert_id()
INSERT INTO reservations
SET Checkin date= '24/05/2018', checkout date = '29/05/2018';
INSERT INTO guests
SET reservation id = LAST_INSERT_ID(),
guest name = guest name
First insert to reservation table
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sqlR = "INSERT INTO Reservation(firstname, lastname, email)
VALUES ('John', 'Doe', 'john#example.com')";
$rStatus = $conn->query($sqlR);
$res_id = $conn->insert_id;
$sql = "INSERT INTO MyGuests (firstname, lastname, email,res_id)
VALUES ('John', 'Doe', 'john#example.com',$res_id)";
if ($conn->query($sql) === TRUE) {
$last_id = $conn->insert_id;
echo "New record created successfully. Last inserted ID is: " . $last_id;
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
Firstly you can insert the data into the Reservation table, based on the last inserted id, you can use the value for the column Reservation id for the table Guest. Also, please maintain foreign key constraints, so as on update and delete operations of Reservation, simultaneous operations are performed on the Guest table.
To get the last inserted id,
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO ...";
if ($conn->query($sql) === TRUE) {
$last_id = $conn->insert_id; //gives the last inserted id
}
Run the insert query first with the insert_id function and then use that insert_id to run the second insert query

Conditional insert or update in mysql

I have a table with input of type time and I want to check first if there is a row with the current date and the id of employee, if it was I update the value of the input if not I insert a new row. This is what I have tried but it always inserts a new row even if the condition exists:
<?php
$E1=$_POST['E1'];
$connect = mysqli_connect("localhost", "root", "ntr-ktb123", "absence");
$sql1="SELECT * FROM retards WHERE Date ='Curdate()' AND
IdEmpl='".$_POST["IdEmp"]."' ;";
$result1=mysqli_query($connect,$sql1);
if(!$result1){
die('ERREUR SQL ! <br>'.$sql.'<br>'.mysqli_error());}
if($dt=mysqli_fetch_array($result1,MYSQLI_ASSOC)){
$sql="update retards set E1='$E1' where IdEmpl='".$_POST["IdEmp"]."' AND
Date=CURDATE();";
}
else{
$sql="insert into retards(IdEmpl,Date,E1) values
('".$_POST["IdEmp"]."',CURDATE(),'$E1'); ";
}
$result = mysqli_query($connect, $sql);
if (!$result)
{
echo("Error description: " . mysqli_error($connect));
}
else {
$message ="Effectué avec succès!";
echo "<script type='text/javascript'>alert('$message'); </script>";
}
mysqli_close($connect);
?>
This is known as an upsert which can be done in mysql using the insert ... on duplicate key update syntax.
insert into t (a, b, c) values (?, ?, ?)
on duplicate key update b = ?
Your table should have an appropriate unique index or primary key defined on the column(s) of interest.

Mysql info in select tag

So I've been trying to figure out how to do this but I've had no luck.
I have a database that includes ID, name, date, and username
I have a simple submit form that generates an id and a user adds a name and an event date then it logs the username, However I would like to take that date and then have it appear in a select tag with the format of Name - Date and have it sorted by the closest date. I want to view info about that event when someone clicks it. But I can't figure out how to make it appear in the select tag. I don't even know where to start with this. all help appreciated.
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$id = uniqid(rand(), false);
$name=$_POST['name'];
$date=$_POST['date'];
$username = $_POST['username'];
// Insert data into database
$sql="INSERT INTO events (id, name, date, username)
VALUES
('$id', '$name', '$date', '$username')";
if ($conn->query($sql) === TRUE) {
echo "";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
error_log(implode("-",$_POST));
$conn->close();
?>

Update values in a row if there is a same name in a column (with PHP in MySQL)

I have a database with three columns: ID, userName, feedback
I want the feedback value to be updated when the userName is same.
my php code:
<?php
conn = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$value = $_POST['name'];
$value2 = $_POST['message'];
$sql = "INSERT INTO js (userName, feedback) VALUES('$value', '$value2');
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
};
$conn->close();
?>
If you want to do it with mysql you first of should define a unique index on the userNamecolumn.
ALTER TABLE js
ADD CONSTRAINT u_userName UNIQUE (userName)
The edit your insert query like this:
INSERT INTO js (userName, feedback) VALUES ('$value', '$value2')
ON DUPLICATE KEY UPDATE feedback=VALUES(feedback);
Now if the duplicate key violation is hit by mysql, the value for feedback will be updated while inside the row where your username equals.
Or you implement a update logic inside php and detect if there already is an entry inside the table with the same username.

Modify insert into to update

I want to modify my code so instead of just inserting a new row in the MySQL table, it can check to see if there is one with the same item number, and update it.
php code
<?php
$con=mysqli_connect("localhost","root","root","inventory");
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql = "INSERT INTO `current stock` (ItemNumber, Stock)
VALUES
('".$_POST['ItemNumber']."', '".$_POST['Stock']."' )";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "1 record added";
mysqli_close($con);
?>
You can use ON DUPLICATE KEY UPDATE syntax,
$sql = "
INSERT INTO `current stock` (ItemNumber, Stock)
VALUES ('$_POST[ItemNumber]', '$_POST[Stock]' )
ON DUPLICATE KEY UPDATE
Stock = '$_POST[Stock]'
";
ItemNumber should be primary/unique key in this case

Categories