Elton Posted May 28, 2020 Share Posted May 28, 2020 <?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 More sharing options...
Ingolme Posted May 28, 2020 Share Posted May 28, 2020 An INSERT query always creates a new record. If you want to update a record, you need to use an UPDATE query. Link to comment Share on other sites More sharing options...
Elton Posted May 28, 2020 Author Share Posted May 28, 2020 $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 More sharing options...
Ingolme Posted May 28, 2020 Share Posted May 28, 2020 You will need to specify a unique key in your table. Which of those fields has to be unique? Link to comment Share on other sites More sharing options...
Elton Posted May 28, 2020 Author Share Posted May 28, 2020 It doesn't really matter, does it? but there are still mistakes https://prnt.sc/spkco6 Link to comment Share on other sites More sharing options...
Ingolme Posted May 28, 2020 Share Posted May 28, 2020 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 More sharing options...
Elton Posted May 28, 2020 Author Share Posted May 28, 2020 (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 May 28, 2020 by Elton Link to comment Share on other sites More sharing options...
Ingolme Posted May 28, 2020 Share Posted May 28, 2020 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 comment Share on other sites More sharing options...
Elton Posted May 28, 2020 Author Share Posted May 28, 2020 https://prnt.sc/spmxte Link to comment Share on other sites More sharing options...
Ingolme Posted May 28, 2020 Share Posted May 28, 2020 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 More sharing options...
Elton Posted May 28, 2020 Author Share Posted May 28, 2020 <?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 More sharing options...
Ingolme Posted May 28, 2020 Share Posted May 28, 2020 You're mixing object-oriented mysqli with procedural mysql and your INSERT query is missing the VALUES section. Link to comment Share on other sites More sharing options...
Elton Posted May 28, 2020 Author Share Posted May 28, 2020 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 More sharing options...
Ingolme Posted May 28, 2020 Share Posted May 28, 2020 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 More sharing options...
Elton Posted May 29, 2020 Author Share Posted May 29, 2020 (edited) I ve already read through but I do not come across the problem Edited May 29, 2020 by Elton Link to comment Share on other sites More sharing options...
Ingolme Posted May 29, 2020 Share Posted May 29, 2020 Look carefully for differences between your SQL and the SQL Syntax shown in this tutorial page: https://www.w3schools.com/php/php_mysql_insert.asp Link to comment Share on other sites More sharing options...
Elton Posted May 29, 2020 Author Share Posted May 29, 2020 https://prnt.sc/sq96pa . https://prnt.sc/sq96ug whats wrong Link to comment Share on other sites More sharing options...
Ingolme Posted May 29, 2020 Share Posted May 29, 2020 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 More sharing options...
Elton Posted May 29, 2020 Author Share Posted May 29, 2020 Yes the problem is I tried it with update but that does not work Link to comment Share on other sites More sharing options...
Ingolme Posted May 29, 2020 Share Posted May 29, 2020 You have to determine why it did not work and then solve that problem. Link to comment Share on other sites More sharing options...
Elton Posted May 29, 2020 Author Share Posted May 29, 2020 That's why I'm writing here Link to comment Share on other sites More sharing options...
Ingolme Posted May 29, 2020 Share Posted May 29, 2020 What error are you getting and what does your new code look like? Link to comment Share on other sites More sharing options...
Elton Posted May 30, 2020 Author Share Posted May 30, 2020 <?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 More sharing options...
Ingolme Posted May 30, 2020 Share Posted May 30, 2020 That's not the correct syntax for an UPDATE query. The correct syntax is: (https://www.w3schools.com/php/php_mysql_update.asp) UPDATE table_name SET column1=value, column2=value2,... WHERE some_column=some_value Link to comment Share on other sites More sharing options...
Elton Posted May 30, 2020 Author Share Posted May 30, 2020 $query = "UPDATE `$dbname` SET name=%s, url=%s, short=%s, game=%s"; Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now