Jump to content

The data records are always re-entered instead of updated


Elton

Recommended Posts

<?php
error_reporting(E_ALL); 
ini_set('display_errors', true);
$dbname='in_dein_fall_test';
$json_datei='https://api.truckyapp.com/v2/traffic/servers';
$mysqli=mysqli_connect('localhost', 'root', '', 'test');
if (mysqli_connect_errno())  die ("Connect failed: " . mysqli_connect_error());	 
mysqli_set_charset($mysqli, "utf8");  
//	 /* von hier auskommentieren nach ersten aufruf


//   bis hier auskkommentieren oder löschen*/
$query = "INSERT INTO `$dbname` (`name`, `url`, `short`, `game`)
		  VALUES ('%s','%s','%s', '%s')";
$json = json_decode(file_get_contents($json_datei),true);
$queries = array();
foreach ($json['response'] as $data){
	$name=mysqli_real_escape_string($mysqli, $data['name']);
	$url=mysqli_real_escape_string($mysqli, $data['url']);
	$short=mysqli_real_escape_string($mysqli, $data['short']);
	$game=mysqli_real_escape_string($mysqli, $data['game']);
	$queries[] = sprintf($query, $name, $url, $short, $game);
}
$menge=count($queries);
if (mysqli_multi_query($mysqli, implode(";", $queries))){
	echo  "$menge Datensätze erfolgreich importiert<br>";
}else{
	echo "Irgendwelche errors<br>";
} 
echo "";
$mysqli->close();
?>
Who knows what is wrong here, that the roofs are always re-entered instead of updated
Link to comment
Share on other sites

$query = "INSERT INTO `$dbname` (`name`, `url`, `short`, `game`) VALUES ('%s','%s','%s', '%s') ON DUPLICATE KEY UPDATE 
  `name` = '%s', 
  `url` = '%s',
  `short` = '%s',
  `game` = '%s'";  

I have tried it with this code but it does not work

 

Link to comment
Share on other sites

If you don't have a unique key, it won't find a duplicate, so it will not update any of the records and it will create a new one instead.

After adding the ON DUPLICATE KEY UPDATE section to your query, the code now has 8 "%s" placeholders, but you are only passing in four values to the sprintf() function.

Link to comment
Share on other sites

if I do it so then come still error https://prnt.sc/splbgj

 

$query = "INSERT INTO `$dbname` (`name`, `url`, `short`, `game`)  ON DUPLICATE KEY UPDATE 
  `name` = '%s', 
  `url` = '%s',
  `short` = '%s',
  `game` = '%s'";  
$json = json_decode(file_get_contents($json_datei),true);
$queries = array();
foreach ($json['response'] as $data){
	$name=mysqli_real_escape_string($mysqli, $data['name']);
	$url=mysqli_real_escape_string($mysqli, $data['url']);
	$short=mysqli_real_escape_string($mysqli, $data['short']);
	$game=mysqli_real_escape_string($mysqli, $data['game']);
	$queries[] = sprintf($query, $name, $url, $short, $game);
}
$menge=count($queries);
if (mysqli_multi_query($mysqli, implode(";", $queries))){
	echo  "$menge Datensätze erfolgreich importiert<br>";
}else{
	echo "Irgendwelche errors<br>";
} 
echo "";
$mysqli->close();
?>

 

Edited by Elton
Link to comment
Share on other sites

Without seeing your code I can't tell what's wrong but it looks like you're probably mixing up the procedural and object-oriented versions of the functions.

Link to comment
Share on other sites

<?php
error_reporting(E_ALL); 
ini_set('display_errors', true);
$dbname='in_dein_fall_test';
$json_datei='https://api.truckyapp.com/v2/traffic/servers';
$mysqli=mysqli_connect('localhost', 'root', '', 'db');
if (mysqli_connect_errno())  die ("Connect failed: " . mysqli_connect_error());	 
mysqli_set_charset($mysqli, "utf8");  
//	 /* von hier auskommentieren nach ersten aufruf

//   bis hier auskkommentieren oder löschen*/
$query = "INSERT INTO `$dbname` (`name`, `url`, `short`, `game`)  ON DUPLICATE KEY UPDATE 
  `name` = '%s', 
  `url` = '%s',
  `short` = '%s',
  `game` = '%s'";  
$json = json_decode(file_get_contents($json_datei),true);
$queries = array();
foreach ($json['response'] as $data){
	$name=mysqli_real_escape_string($mysqli, $data['name']);
	$url=mysqli_real_escape_string($mysqli, $data['url']);
	$short=mysqli_real_escape_string($mysqli, $data['short']);
	$game=mysqli_real_escape_string($mysqli, $data['game']);
	$queries[] = sprintf($query, $name, $url, $short, $game);
}
$menge=count($queries);
if (mysqli_multi_query($mysqli, implode(";", $queries))){
	echo  "$menge Datensätze erfolgreich importiert<br>";
}else{
	echo "Irgendwelche errors<br>";
} 
echo "";
$mysqli->close();
if ($mysqli -> connect_errno) {
  echo "Failed to connect to MySQL: " . $mysqli -> connect_error;
  exit();
}

// Perform a query, check for error
if (!$mysqli -> query("INSERT INTO `in_dein_fall_test` (name) VALUES ('Glenn')")) {
  echo("Error description: " . $mysqli -> error);
}
?>

Thats the code

Link to comment
Share on other sites

Could they put me in the code I just do not know exactly how I do it 

 

so? $query = "INSERT INTO `$dbname` VALUES (`name`, `url`, `short`, `game`)  ON DUPLICATE KEY UPDATE 
  `name` = '%s', 
  `url` = '%s',
  `short` = '%s',
  `game` = '%s'";  

Link to comment
Share on other sites

It's best you go through the MySQL section of the PHP tutorial and learn how to do it. You won't learn if you don't go through the effort and next time you run into a problem you'll just be asking other people to write the code for you again.

Link to comment
Share on other sites

That code looks valid, but it is different than the code you posted in this topic.

It is an INSERT query, so it will only ever create new records and not change existing ones.

Link to comment
Share on other sites

<?php
error_reporting(E_ALL); 
ini_set('display_errors', true);
$dbname='in_dein_fall_test';
$json_datei='https://api.truckyapp.com/v2/traffic/servers';
$mysqli=mysqli_connect('localhost', 'root', '', 'db');
if (mysqli_connect_errno())  die ("Connect failed: " . mysqli_connect_error());	 
mysqli_set_charset($mysqli, "utf8");  
//	 /* von hier auskommentieren nach ersten aufruf

//   bis hier auskkommentieren oder löschen*/
$query = "UPDATE `$dbname` SET  (`name`, `url`, `short`, `game`)
VALUES ('%s','%s','%s', '%s')";

$json = json_decode(file_get_contents($json_datei),true);
$queries = array();
foreach ($json['response'] as $data){
	$name=mysqli_real_escape_string($mysqli, $data['name']);
	$url=mysqli_real_escape_string($mysqli, $data['url']);
	$short=mysqli_real_escape_string($mysqli, $data['short']);
	$game=mysqli_real_escape_string($mysqli, $data['game']);
	$queries[] = sprintf($query, $name, $url, $short, $game);
}
$menge=count($queries);
if (mysqli_multi_query($mysqli, implode(";", $queries))){
	echo  "$menge Datensätze erfolgreich importiert<br>";
}else{
	echo "errors<br>";
} 
echo "";
$mysqli->close();
?>

https://prnt.sc/sqjsnc

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...