Jump to content

The data records are always re-entered instead of updated


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 post
Share on other sites

An INSERT query always creates a new record. If you want to update a record, you need to use an UPDATE query.

Link to post
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 post
Share on other sites

You will need to specify a unique key in your table. Which of those fields has to be unique?

Link to post
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 post
Share on other sites
Posted (edited)

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 post
Share on other sites

Use mysqli_error() to find out what went wrong with the query.

It looks like you forgot to add a VALUES() section to your INSERT query.

Link to post
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 post
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 post
Share on other sites

You're mixing object-oriented mysqli with procedural mysql and your INSERT query is missing the VALUES section.

Link to post
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 post
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 post
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 post
Share on other sites

You have to determine why it did not work and then solve that problem.

Link to post
Share on other sites

What error are you getting and what does your new code look like?

Link to post
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 post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...