dt246813579 2015-04-21 20:12 采纳率: 0%
浏览 11

带HTTP / 1.1的SQL获取

This was reposted from dba.stackexchange.

Sorry for such an amateur question, but I have no idea why this does not work. I have a "add.php" to connect to the MySQL server.

/add.php

<?php
    include("connect.php");

    $link=Connection();

    $ID1=$_POST["ID1"];
    $ID2=$_POST["ID2"];
    $ID3=$_POST["ID3"];
    $ID4=$_POST["ID4"];
    $ID5=$_POST["ID5"];

    $query = "INSERT INTO Battery (ID01, ID02, ID03, ID04, ID05) 
        VALUES ('".$ID1."','".$ID2.",'".$ID3.",'".$ID4."','".$ID5."')"; 

    mysql_query($query,$link);
    mysql_close($link);

    header("Location: index.php");
?>

connect.php

<?php

    function Connection(){
        $server="mysql.randomserver.com";
        $user="random";
        $pass="1234";
        $db="random_1234";

        $connection = mysql_connect($server, $user, $pass);

        if (!$connection) {
            die('MySQL ERROR: ' . mysql_error());
        }

        mysql_select_db($db) or die( 'MySQL ERROR: '. mysql_error() );

        return $connection;
    }
?>

I use a simple HTTP 1.1 protocol:

GET /add.php?ID1=1int&ID2=2char&ID3=3char&ID4=4int&ID5=2015-04-13 01:00:00 HTTP/1.1 myhost Content-Type: application/x-www-form-urlencoded Connection:close

where ID1,ID4 is int; ID2, ID3 char; ID5 Datetime (SQL)

The host throw me this error:

+IPD,168:<html>
<head><title>400 Bad Request</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<hr><center>hosting</center>
</body>
</html>

If anyone have any idea for me to try out, I would be grateful! I'm really clueless...

Fixed: (moskito-x) '".$ID3.", to '".$ID3."',

UPDATE:2015.04.22 13:56

Ok, I tried this and it works on my Main page: Index.php (just copy the whole /add.php code into /index.php)

$query= "INSERT INTO  Battery (ID01,ID02,ID03,ID04,ID05) 
        VALUES ('1int','2char','3char','4int','2015-04-22 17:20:28')";
$result = mysqli_query($link, $query)

But if I replace it into the add.php, no row is inserted.

I changed add.php as other have suggested:

add.php

<?php
    $link=Connection();
        $server="mysql.myhost.com";
        $user="randomUser";
        $pass="randomPwd";
        $db="radomdb";

    $link=mysqli_connect($server, $user, $pass, $db);


    $query = "INSERT INTO  Battery (ID01,ID02,ID03,ID04,ID05)
        VALUES ('1int','2char','3char','4int','2015-04-22 17:20:28')";
    mysqli_query($link, $query)
             mysqli_close($link);
    header("Location: index.php");
?>
  • 写回答

2条回答 默认 最新

  • dongzheng3113 2015-04-21 20:35
    关注

    Important part $link=Connection();

    • We can not see code of Connection();
    • your query is wrong
    • mentioned in comments -> you using a http GET so $ID1=$_POST["ID1"]; is wrong to.

    '".$ID1."','".$ID2.",'".$ID3.",'".$ID4."','".$ID5."')";
    //                  |         |
    //                  |_________|____ here forget ' 
    
    $query = "INSERT INTO Battery (ID01, ID02, ID03, ID04, ID05) 
        VALUES ('".$ID1."','".$ID2."','".$ID3."','".$ID4."','".$ID5."')"; 
    

    EDIT

    Now we can see in your Edited question

    where ID1,ID4 is int; ID2, ID3 char; ID5 TimeStamp

    Query should be (assume TimeStamp ="20150421225300") string format.

    $query = "INSERT INTO Battery (ID01, ID02, ID03, ID04, ID05) 
        VALUES (".$ID1.",'".$ID2."','".$ID3."',".$ID4.",'".$ID5."')"; 
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题