简体   繁体   English

通过PHP插入SQL数据库

[英]Insert into SQL database via PHP

My Java code produces link like this: 我的Java代码产生这样的链接:

addscore.php&name=Namedd&score=0&hash=b61249d0207e4734879578733a86c3d6

A PHP file(addscore.php) should work with these informations and insert it into database with an sql query. 一个PHP文件(addscore.php)应该可以处理这些信息,并通过sql查询将其插入数据库。

addscore.php: addscore.php:

<?php
         $db = mysql_connect("host", "root", "password") or die('Could not connect: ' . mysql_error());
         mysql_select_db("db1") or die('Could not select database');

         $score = (int)$_GET['score'];
         $hash = $_GET['hash'];
         $name = mysql_real_escape_string($_GET['name'], $db);
         $timestamp = date("Y-m-d H:i:s");
         $secretKey="mySecretKey";

         $real_hash = md5($score . $hash . $secretKey);

         if($real_hash == $hash) {
             $query = "INSERT INTO highscores (id, date, score, name) VALUES (NULL, '$timestamp', '$score', '$name')";
             $result = mysql_query($query) or die('Query failed: ' . mysql_error());
         }
?>

Java code gives an error message, could not submit score to server. Java代码给出错误消息,无法将分数提交给服务器。 If I try to open given url(see above) I get a Not found error. 如果我尝试打开给定的url(请参阅上文),则会收到“ Not found错误。

UPDATE: Java code: 更新: Java代码:

        String hash = "";
        String name = nameText.getText();
        try{
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update((name + score + HASH_SALT).getBytes());
            byte byteData[] = md.digest();

            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < byteData.length; i++) {
                sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
            }
            hash = sb.toString();
        } catch (Exception e) {
            hash = "";
        }


        try {
            String urlParameters = PARAM_NAME + name + PARAM_SCORE + score + PARAM_HASH + hash;
            URL url = new URL(HIGHSCORES_ADD + urlParameters);
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.getInputStream();
            urlConnection.disconnect();
        } catch (Exception e){
            Gdx.app.log( "HighScores", "Could not submit score" + e.getMessage());
        } finally {
            ((Game)Gdx.app.getApplicationListener()).setScreen(new MainMenu());
        }

Your Address should be 您的地址应该是

addscore.php?name=Namedd&score=0&hash=b61249d0207e4734879578733a86c3d6

note to first & in the URL 注意第一个& URL

In your Java code make sure to have ? 在您的Java代码中确保具有? between HIGHSCORES_ADD and urlParameters HIGHSCORES_ADDurlParameters之间

when you fixed that if still have error in save data A good Idea is save your $query in a text or log file and try to check query in phpmyadmin 当您修复了如果在保存数据时仍然有错误时,一个好主意是将$ query保存到文本或日志文件中,并尝试在phpmyadmin中检查查询

Another Thing you must note is Your insert is via php not with java ! 您还必须注意的另一件事是您的插入是通过php而不是java进行的! update your question subject 更新您的问题主题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM